async def rollback(self): try: await self.transaction.rollback() except asyncpg.exceptions._base.InterfaceError as exc: raise TransactionManagementError(exc) current_transaction_map[self.connection_name].set( self._old_context_value)
async def commit(self) -> None: if self._finalized: raise TransactionManagementError('Transaction already finalised') self._finalized = True await self._connection.commit() current_transaction_map[self.connection_name].set( self._old_context_value)
async def retry_connection_(self, *args): try: return await func(self, *args) except ( asyncpg.PostgresConnectionError, asyncpg.ConnectionDoesNotExistError, asyncpg.ConnectionFailureError, asyncpg.InterfaceError, ): # Here we assume that a connection error has happened # Re-create connection and re-try the function call once only. if getattr(self, "transaction", None): self._finalized = True raise TransactionManagementError( "Connection gone away during transaction") logging.info("Attempting reconnect") try: await self._close() await self.create_connection(with_db=True) logging.info("Reconnected") except Exception as e: logging.info("Failed to reconnect: %s", str(e)) raise return await func(self, *args)
async def rollback(self): if self._finalized: raise TransactionManagementError("Transaction already finalised") self._finalized = True await self.transaction.rollback() current_transaction_map[self.connection_name].set( self._old_context_value)
async def retry_connection_(self, *args): try: return await func(self, *args) except ( RuntimeError, pymysql.err.OperationalError, pymysql.err.InternalError, pymysql.err.InterfaceError, ): # Here we assume that a connection error has happened # Re-create connection and re-try the function call once only. if getattr(self, "_finalized", None) is False: raise TransactionManagementError( "Connection gone away during transaction") await self._lock.acquire() logging.info("Attempting reconnect") try: await self._close() await self.create_connection(with_db=True) logging.info("Reconnected") except Exception as e: raise DBConnectionError("Failed to reconnect: %s", str(e)) finally: self._lock.release() return await func(self, *args)
async def start(self) -> None: if self._connection: self._transaction = self._connection.transaction() await self._transaction.start() else: raise TransactionManagementError( "No connection is established. You need to call acquire() first" )
async def rollback(self) -> None: if self._in_progress: await self._connection.rollback() self._in_progress = False else: raise TransactionManagementError( "No transaction is in progress. You need to call start() first." )
async def start(self) -> None: if self._connection: await self._connection.begin() self._in_progress = True else: raise TransactionManagementError( "No connection is established. You need to call acquire() first" )
async def commit(self) -> None: if self._transaction: await self._transaction.commit() self._transaction = None else: raise TransactionManagementError( "No transaction is in progress. You need to call start() first." )
async def start(self) -> None: try: await self._connection.commit() await self._connection.execute("BEGIN") except sqlite3.OperationalError as exc: # pragma: nocoverage raise TransactionManagementError(exc) self._in_progress = True
async def start(self): try: await self._connection.commit() await self._connection.execute('BEGIN') except sqlite3.OperationalError as exc: # pragma: nocoverage raise TransactionManagementError(exc) self._old_context_value = current_transaction.get() current_transaction.set(self)
async def rollback(self): if self._finalized: raise TransactionManagementError('Transaction already finalised') self._finalized = True await self._connection.rollback() if self._pool: await self._pool.release(self._connection) self._connection = None current_transaction.set(self._old_context_value)
async def translate_exceptions_(self, *args): try: return await func(self, *args) except asyncpg.SyntaxOrAccessError as exc: raise OperationalError(exc) except asyncpg.IntegrityConstraintViolationError as exc: raise IntegrityError(exc) except asyncpg.InvalidTransactionStateError as exc: # pragma: nocoverage raise TransactionManagementError(exc)
async def rollback(self): try: await self.transaction.rollback() except asyncpg.exceptions._base.InterfaceError as exc: raise TransactionManagementError(exc) if self._pool: await self._pool.release(self._connection) self._connection = None current_transaction.set(self._old_context_value)
async def rollback(self) -> None: if self._finalized: raise TransactionManagementError("Transaction already finalised") await self.transaction.rollback() self._finalized = True
async def commit(self) -> None: if self._finalized: raise TransactionManagementError("Transaction already finalised") await self._connection.commit() self._finalized = True
async def acquire(self) -> None: if self._pool: self._connection = await self._pool.acquire() else: raise TransactionManagementError( "You need to call create_connection() first.")
async def commit(self): if self._finalized: raise TransactionManagementError("Transaction already finalised") await self.transaction.commit() await self.finalize()