class documentation

class Transaction(TransactionalContext):

Known subclasses: sqlalchemy.engine.base.NestedTransaction, sqlalchemy.engine.base.RootTransaction, sqlalchemy.engine.base.MarkerTransaction

View In Hierarchy

Represent a database transaction in progress.

The .Transaction object is procured by calling the _engine.Connection.begin method of _engine.Connection:

from sqlalchemy import create_engine
engine = create_engine("postgresql://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 .rollback and .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 _engine.Connection.begin method:

with connection.begin():
    connection.execute(text("insert into x (a, b) values (1, 2)"))

The Transaction object is not threadsafe.

See Also

_engine.Connection.begin

_engine.Connection.begin_twophase

_engine.Connection.begin_nested

Method __init__ Undocumented
Method ​_do​_close Undocumented
Method ​_do​_commit Undocumented
Method ​_do​_deactivate do whatever steps are necessary to set this transaction as "deactive", however leave this transaction object in place as far as the connection's state.
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
Method close Close this .Transaction.
Method commit Commit this .Transaction.
Method rollback Roll back this .Transaction.
Class Variable __slots__ 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.
Property is​_valid Undocumented

Inherited from TransactionalContext:

Class Method ​_trans​_ctx​_check Undocumented
Method __enter__ Undocumented
Method __exit__ Undocumented
Instance Variable ​_outer​_trans​_ctx Undocumented
Instance Variable ​_trans​_subject Undocumented
def _do_deactivate(self):

do whatever steps are necessary to set this transaction as "deactive", however leave this transaction object in place as far as the connection's state.

for a "real" transaction this should roll back the transaction and ensure this transaction is no longer a reset agent.

this is used for nesting of marker transactions where the marker can set the "real" transaction as rolled back, however it stays in place.

for 2.0 we hope to remove this nesting feature.

def _get_subject(self):
def _rollback_can_be_called(self):

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.

New in version 1.4.28.
def _transaction_is_active(self):
def _transaction_is_closed(self):
def close(self):

Close this .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):

Commit this .Transaction.

The implementation of this may vary based on the type of transaction in use:

  • For a simple database transaction (e.g. .RootTransaction), it corresponds to a COMMIT.
  • For a .NestedTransaction, it corresponds to a "RELEASE SAVEPOINT" operation.
  • For a .TwoPhaseTransaction, DBAPI-specific methods for two phase transactions may be used.
def rollback(self):

Roll back this .Transaction.

The implementation of this may vary based on the type of transaction in use:

  • For a simple database transaction (e.g. .RootTransaction), it corresponds to a ROLLBACK.
  • For a .NestedTransaction, it corresponds to a "ROLLBACK TO SAVEPOINT" operation.
  • For a .TwoPhaseTransaction, DBAPI-specific methods for two phase transactions may be used.
_is_root: bool =

Undocumented

@property
_deactivated_from_connection =
True if this transaction is totally deactivated from the connection and therefore can no longer affect its state.
@property
is_valid =

Undocumented