class documentation

Represent a database transaction in progress. The :class:`.Transaction` object is procured by calling the :meth:`_engine.Connection.begin` method of :class:`_engine.Connection`:: from sqlalchemy import create_engine engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/test") connection = engine.connect() trans = connection.begin() connection.execute(text("insert into x (a, b) values (1, 2)")) trans.commit() The object provides :meth:`.rollback` and :meth:`.commit` methods in order to control transaction boundaries. It also implements a context manager interface so that the Python ``with`` statement can be used with the :meth:`_engine.Connection.begin` method:: with connection.begin(): connection.execute(text("insert into x (a, b) values (1, 2)")) The Transaction object is **not** threadsafe. .. seealso:: :meth:`_engine.Connection.begin` :meth:`_engine.Connection.begin_twophase` :meth:`_engine.Connection.begin_nested` .. index:: single: thread safety; Transaction

Method __init__ Undocumented
Method close Close this :class:`.Transaction`.
Method commit Commit this :class:`.Transaction`.
Method rollback Roll back this :class:`.Transaction`.
Class Variable __slots__ Undocumented
Class Variable connection Undocumented
Class Variable is_active Undocumented
Property is_valid Undocumented
Method _do_close Undocumented
Method _do_commit Undocumented
Method _do_rollback Undocumented
Method _get_subject Undocumented
Method _rollback_can_be_called indicates the object is in a state that is known to be acceptable for rollback() to be called.
Method _transaction_is_active Undocumented
Method _transaction_is_closed Undocumented
Class Variable _is_root Undocumented
Property _deactivated_from_connection True if this transaction is totally deactivated from the connection and therefore can no longer affect its state.

Inherited from TransactionalContext:

Method __enter__ Undocumented
Method __exit__ Undocumented
Class Method _trans_ctx_check Undocumented
Instance Variable _outer_trans_ctx Undocumented
Instance Variable _trans_subject Undocumented
def close(self): (source)

Close this :class:`.Transaction`. If this transaction is the base transaction in a begin/commit nesting, the transaction will rollback(). Otherwise, the method returns. This is used to cancel a Transaction without affecting the scope of an enclosing transaction.

def commit(self): (source)

Commit this :class:`.Transaction`. The implementation of this may vary based on the type of transaction in use: * For a simple database transaction (e.g. :class:`.RootTransaction`), it corresponds to a COMMIT. * For a :class:`.NestedTransaction`, it corresponds to a "RELEASE SAVEPOINT" operation. * For a :class:`.TwoPhaseTransaction`, DBAPI-specific methods for two phase transactions may be used.

def rollback(self): (source)

Roll back this :class:`.Transaction`. The implementation of this may vary based on the type of transaction in use: * For a simple database transaction (e.g. :class:`.RootTransaction`), it corresponds to a ROLLBACK. * For a :class:`.NestedTransaction`, it corresponds to a "ROLLBACK TO SAVEPOINT" operation. * For a :class:`.TwoPhaseTransaction`, DBAPI-specific methods for two phase transactions may be used.

Undocumented

def _rollback_can_be_called(self) -> bool: (source)

indicates the object is in a state that is known to be acceptable for rollback() to be called. This does not necessarily mean rollback() will succeed or not raise an error, just that there is currently no state detected that indicates rollback() would fail or emit warnings. It also does not mean that there's a transaction in progress, as it is usually safe to call rollback() even if no transaction is present. .. versionadded:: 1.4.28

def _transaction_is_active(self) -> bool: (source)
def _transaction_is_closed(self) -> bool: (source)
@property
_deactivated_from_connection: bool = (source)

True if this transaction is totally deactivated from the connection and therefore can no longer affect its state.