Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
 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"
         )
Exemplo n.º 7
0
 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."
         )
Exemplo n.º 8
0
 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"
         )
Exemplo n.º 9
0
 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."
         )
Exemplo n.º 10
0
    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
Exemplo n.º 11
0
 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)
Exemplo n.º 12
0
 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)
Exemplo n.º 13
0
 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)
Exemplo n.º 14
0
 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)
Exemplo n.º 15
0
 async def rollback(self) -> None:
     if self._finalized:
         raise TransactionManagementError("Transaction already finalised")
     await self.transaction.rollback()
     self._finalized = True
Exemplo n.º 16
0
 async def commit(self) -> None:
     if self._finalized:
         raise TransactionManagementError("Transaction already finalised")
     await self._connection.commit()
     self._finalized = True
Exemplo n.º 17
0
 async def acquire(self) -> None:
     if self._pool:
         self._connection = await self._pool.acquire()
     else:
         raise TransactionManagementError(
             "You need to call create_connection() first.")
Exemplo n.º 18
0
 async def commit(self):
     if self._finalized:
         raise TransactionManagementError("Transaction already finalised")
     await self.transaction.commit()
     await self.finalize()