@@ -514,6 +514,7 @@ def __init__(
514
514
self ._implicit = implicit
515
515
self ._transaction = _Transaction (None , client )
516
516
self ._attached_to_cursor = False
517
+ self ._leave_alive = False
517
518
518
519
async def end_session (self ) -> None :
519
520
"""Finish this session. If a transaction has started, abort it.
@@ -536,7 +537,7 @@ async def _end_session(self, lock: bool) -> None:
536
537
537
538
def _end_implicit_session (self ) -> None :
538
539
# Implicit sessions can't be part of transactions or pinned connections
539
- if self ._server_session is not None :
540
+ if not self . _leave_alive and self ._server_session is not None :
540
541
# print(f"Ending session {self}, implicit: {self._implicit}, attached: {self._attached_to_cursor}")
541
542
self ._client ._return_server_session (self ._server_session )
542
543
self ._server_session = None
@@ -606,6 +607,18 @@ def attached_to_cursor(self) -> bool:
606
607
def attached_to_cursor (self , value : bool ) -> None :
607
608
self ._attached_to_cursor = value
608
609
610
+ @property
611
+ def leave_alive (self ) -> bool :
612
+ """Whether to leave this session alive when it is
613
+ no longer in use.
614
+ Typically used for implicit sessions that are used for multiple operations within a single larger operation.
615
+ """
616
+ return self ._leave_alive
617
+
618
+ @leave_alive .setter
619
+ def leave_alive (self , value : bool ) -> None :
620
+ self ._leave_alive = value
621
+
609
622
def _inherit_option (self , name : str , val : _T ) -> _T :
610
623
"""Return the inherited TransactionOption value."""
611
624
if val :
0 commit comments