def __check_state_base(self, opname): if self._state is TransactionState.COMMITTED: raise errors.InterfaceError( f'cannot {opname}; the transaction is already committed') if self._state is TransactionState.ROLLEDBACK: raise errors.InterfaceError( f'cannot {opname}; the transaction is already rolled back') if self._state is TransactionState.FAILED: raise errors.InterfaceError( f'cannot {opname}; the transaction is in error state')
async def __aenter__(self): if self._managed: raise errors.InterfaceError( 'cannot enter context: already in an `async with` block') self._managed = True await self.start() return self
async def _ensure_transaction(self): if not self._managed: raise errors.InterfaceError( "Only managed retriable transactions are supported. " "Use `async with transaction:`") if not self.__started: self.__started = True if self._connection.is_closed(): await self._connection.connect( single_attempt=self.__iteration != 0) await self.start()
def _make_start_query(self): self.__check_state_base('start') if self._state is TransactionState.STARTED: raise errors.InterfaceError( 'cannot start; the transaction is already started') return self._make_start_query_inner()
def __check_state(self, opname): if self._state is not TransactionState.STARTED: if self._state is TransactionState.NEW: raise errors.InterfaceError( f'cannot {opname}; the transaction is not yet started') self.__check_state_base(opname)
async def rollback(self) -> None: if self._managed: raise errors.InterfaceError( 'cannot manually rollback from within an `async with` block') await self._rollback()
async def commit(self) -> None: if self._managed: raise errors.InterfaceError( 'cannot manually commit from within an `async with` block') await self._commit()