MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / _tmp_session

Method _tmp_session

pymongo/synchronous/mongo_client.py:2292–2324  ·  view source on GitHub ↗

If provided session is None, lend a temporary session.

(
        self, session: Optional[client_session.ClientSession]
    )

Source from the content-addressed store, hash-verified

2290
2291 @contextlib.contextmanager
2292 def _tmp_session(
2293 self, session: Optional[client_session.ClientSession]
2294 ) -> Generator[Optional[client_session.ClientSession], None]:
2295 """If provided session is None, lend a temporary session."""
2296 if session is not None and not isinstance(session, client_session.ClientSession):
2297 raise ValueError(
2298 f"'session' argument must be a ClientSession or None, not {type(session)}"
2299 )
2300
2301 # Check for a bound session. If one exists, treat it as an explicitly passed session.
2302 session = session or self._get_bound_session()
2303 if session:
2304 # Don't call end_session.
2305 yield session
2306 return
2307
2308 s = self._ensure_session(session)
2309 if s:
2310 try:
2311 yield s
2312 except Exception as exc:
2313 if isinstance(exc, ConnectionFailure):
2314 s._server_session.mark_dirty()
2315
2316 # Always call end_session on error.
2317 s.end_session()
2318 raise
2319 finally:
2320 # Call end_session when we exit this scope.
2321 if not s._attached_to_cursor:
2322 s.end_session()
2323 else:
2324 yield None
2325
2326 def _process_response(self, reply: Mapping[str, Any], session: Optional[ClientSession]) -> None:
2327 self._topology.receive_cluster_time(reply.get("$clusterTime"))

Callers 13

_retryable_readMethod · 0.95
_retryable_writeMethod · 0.95
create_collectionMethod · 0.45
aggregateMethod · 0.45
_commandMethod · 0.45
cursor_commandMethod · 0.45
_list_collectionsMethod · 0.45
_create_cursorMethod · 0.45
_commandMethod · 0.45
_list_indexesMethod · 0.45
aggregateMethod · 0.45

Calls 4

_get_bound_sessionMethod · 0.95
_ensure_sessionMethod · 0.95
mark_dirtyMethod · 0.45
end_sessionMethod · 0.45

Tested by

no test coverage detected