class Transaction(TransactionalContext): (source)
Known subclasses: sqlalchemy.engine.base.NestedTransaction
, sqlalchemy.engine.base.RootTransaction
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 |
Undocumented |
Property | is |
Undocumented |
Method | _do |
Undocumented |
Method | _do |
Undocumented |
Method | _do |
Undocumented |
Method | _get |
Undocumented |
Method | _rollback |
indicates the object is in a state that is known to be acceptable for rollback() to be called. |
Method | _transaction |
Undocumented |
Method | _transaction |
Undocumented |
Class Variable | _is |
Undocumented |
Property | _deactivated |
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 |
Undocumented |
Instance Variable | _outer |
Undocumented |
Instance Variable | _trans |
Undocumented |
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.
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.
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.
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