@@ -246,7 +246,7 @@ class TransactionContext(Generic[T_conn]):
246246 def __init__ (self , connection : Any ) -> None :
247247 self .connection = connection
248248 self .connection_name = connection .connection_name
249- self .lock = getattr ( connection , " _trxlock" , None )
249+ self .lock = connection . _trxlock
250250
251251 async def ensure_connection (self ) -> None :
252252 if not self .connection ._connection :
@@ -255,21 +255,23 @@ async def ensure_connection(self) -> None:
255255
256256 async def __aenter__ (self ) -> T_conn :
257257 await self .ensure_connection ()
258- await self .lock .acquire () # type:ignore
258+ await self .lock .acquire ()
259259 self .token = connections .set (self .connection_name , self .connection )
260260 await self .connection .start ()
261261 return self .connection
262262
263263 async def __aexit__ (self , exc_type : Any , exc_val : Any , exc_tb : Any ) -> None :
264- if not self .connection ._finalized :
265- if exc_type :
266- # Can't rollback a transaction that already failed.
267- if exc_type is not TransactionManagementError :
268- await self .connection .rollback ()
269- else :
270- await self .connection .commit ()
271- connections .reset (self .token )
272- self .lock .release () # type:ignore
264+ try :
265+ if not self .connection ._finalized :
266+ if exc_type :
267+ # Can't rollback a transaction that already failed.
268+ if exc_type is not TransactionManagementError :
269+ await self .connection .rollback ()
270+ else :
271+ await self .connection .commit ()
272+ finally :
273+ connections .reset (self .token )
274+ self .lock .release ()
273275
274276
275277class TransactionContextPooled (TransactionContext ):
@@ -287,16 +289,18 @@ async def __aenter__(self) -> T_conn:
287289 return self .connection
288290
289291 async def __aexit__ (self , exc_type : Any , exc_val : Any , exc_tb : Any ) -> None :
290- if not self .connection ._finalized :
291- if exc_type :
292- # Can't rollback a transaction that already failed.
293- if exc_type is not TransactionManagementError :
294- await self .connection .rollback ()
295- else :
296- await self .connection .commit ()
297- if self .connection ._parent ._pool :
298- await self .connection ._parent ._pool .release (self .connection ._connection )
299- connections .reset (self .token )
292+ try :
293+ if not self .connection ._finalized :
294+ if exc_type :
295+ # Can't rollback a transaction that already failed.
296+ if exc_type is not TransactionManagementError :
297+ await self .connection .rollback ()
298+ else :
299+ await self .connection .commit ()
300+ finally :
301+ if self .connection ._parent ._pool :
302+ await self .connection ._parent ._pool .release (self .connection ._connection )
303+ connections .reset (self .token )
300304
301305
302306class NestedTransactionContext (TransactionContext ):
@@ -313,11 +317,11 @@ async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
313317
314318class NestedTransactionPooledContext (TransactionContext ):
315319 async def __aenter__ (self ) -> T_conn :
316- await self .lock .acquire () # type:ignore
320+ await self .lock .acquire ()
317321 return self .connection
318322
319323 async def __aexit__ (self , exc_type : Any , exc_val : Any , exc_tb : Any ) -> None :
320- self .lock .release () # type:ignore
324+ self .lock .release ()
321325 if not self .connection ._finalized :
322326 if exc_type :
323327 # Can't rollback a transaction that already failed.
0 commit comments