- Notifications
You must be signed in to change notification settings - Fork1.1k
PYTHON-4542 Improved sessions API#2335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Conversation
- Via context variable.
Uh oh!
There was an error while loading.Please reload this page.
@@ -222,6 +224,7 @@ def __init__( | |||
) | |||
self._default_transaction_options = default_transaction_options | |||
self._snapshot = snapshot | |||
self._bind = bind |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
We have to bind/unbind the session inClientSession.__enter__/__exit__
. That way the stack of sessions is managed correctly (ie we call_SESSION.reset(token)
). Think about how nested cases will work:
session1=client.start_session(bind=True)withsession1:session2=client.start_session(bind=True)withsession2:coll.find_one()# uses session2coll.find_one()# uses session1coll.find_one()# uses implicit session
test/asynchronous/test_session.py Outdated
with session2: | ||
coll.find_one() # uses session2 | ||
coll.find_one() # uses session1 | ||
coll.find_one() # uses implicit session |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This test has to actually verify the correct sessions are used via command monitoring events.
pymongo/asynchronous/mongo_client.py Outdated
bind = opts._bind | ||
session = client_session.AsyncClientSession(self, server_session, opts, implicit) | ||
if bind: | ||
_SESSION.set(session) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This needs to be removed. We should only bind in__enter__
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Done in3c68a70
@@ -545,9 +548,12 @@ def _check_ended(self) -> None: | |||
raise InvalidOperation("Cannot use ended session") | |||
async def __aenter__(self) -> AsyncClientSession: | |||
self._token = _SESSION.set(self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
We should only do this if the bind option is set to True.