@@ -196,31 +196,6 @@ def __init__(self, driver, session_state, session, tx_mode):
196196 self .session = session
197197 self ._prev_stream = None
198198
199- def __enter__ (self ) -> "BaseQueryTxContext" :
200- """
201- Enters a context manager and returns a transaction
202-
203- :return: A transaction instance
204- """
205- return self
206-
207- def __exit__ (self , * args , ** kwargs ):
208- """
209- Closes a transaction context manager and rollbacks transaction if
210- it is not finished explicitly
211- """
212- self ._ensure_prev_stream_finished ()
213- if self ._tx_state ._state == QueryTxStateEnum .BEGINED :
214- # It's strictly recommended to close transactions directly
215- # by using commit_tx=True flag while executing statement or by
216- # .commit() or .rollback() methods, but here we trying to do best
217- # effort to avoid useless open transactions
218- logger .warning ("Potentially leaked tx: %s" , self ._tx_state .tx_id )
219- try :
220- self .rollback ()
221- except issues .Error :
222- logger .warning ("Failed to rollback leaked tx: %s" , self ._tx_state .tx_id )
223-
224199 @property
225200 def session_id (self ) -> str :
226201 """
@@ -304,12 +279,6 @@ def _execute_call(
304279 _apis .QueryService .ExecuteQuery ,
305280 )
306281
307- def _ensure_prev_stream_finished (self ) -> None :
308- if self ._prev_stream is not None :
309- for _ in self ._prev_stream :
310- pass
311- self ._prev_stream = None
312-
313282 def _move_to_beginned (self , tx_id : str ) -> None :
314283 if self ._tx_state ._already_in (QueryTxStateEnum .BEGINED ):
315284 return
@@ -323,6 +292,37 @@ def _move_to_commited(self) -> None:
323292
324293
325294class QueryTxContextSync (BaseQueryTxContext ):
295+ def __enter__ (self ) -> "BaseQueryTxContext" :
296+ """
297+ Enters a context manager and returns a transaction
298+
299+ :return: A transaction instance
300+ """
301+ return self
302+
303+ def __exit__ (self , * args , ** kwargs ):
304+ """
305+ Closes a transaction context manager and rollbacks transaction if
306+ it is not finished explicitly
307+ """
308+ self ._ensure_prev_stream_finished ()
309+ if self ._tx_state ._state == QueryTxStateEnum .BEGINED :
310+ # It's strictly recommended to close transactions directly
311+ # by using commit_tx=True flag while executing statement or by
312+ # .commit() or .rollback() methods, but here we trying to do best
313+ # effort to avoid useless open transactions
314+ logger .warning ("Potentially leaked tx: %s" , self ._tx_state .tx_id )
315+ try :
316+ self .rollback ()
317+ except issues .Error :
318+ logger .warning ("Failed to rollback leaked tx: %s" , self ._tx_state .tx_id )
319+
320+ def _ensure_prev_stream_finished (self ) -> None :
321+ if self ._prev_stream is not None :
322+ with self ._prev_stream :
323+ pass
324+ self ._prev_stream = None
325+
326326 def begin (self , settings : Optional [base .QueryClientSettings ] = None ) -> "QueryTxContextSync" :
327327 """WARNING: This API is experimental and could be changed.
328328
0 commit comments