class Transaction(TransactionalContext):
Known subclasses: sqlalchemy.engine.base.NestedTransaction
, sqlalchemy.engine.base.RootTransaction
, sqlalchemy.engine.base.MarkerTransaction
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 |
sqlalchemy.engine.base.NestedTransaction
, sqlalchemy.engine.base.RootTransaction
, sqlalchemy.engine.base.MarkerTransaction
Undocumented
sqlalchemy.engine.base.NestedTransaction
, sqlalchemy.engine.base.RootTransaction
, sqlalchemy.engine.base.MarkerTransaction
Undocumented
sqlalchemy.engine.base.NestedTransaction
, sqlalchemy.engine.base.RootTransaction
, sqlalchemy.engine.base.MarkerTransaction
Undocumented
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.
sqlalchemy.engine.base.NestedTransaction
, sqlalchemy.engine.base.RootTransaction
, sqlalchemy.engine.base.MarkerTransaction
Undocumented
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.
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.
Commit this .Transaction
.
The implementation of this may vary based on the type of transaction in use:
.RootTransaction
),
it corresponds to a COMMIT..NestedTransaction
, it corresponds to a
"RELEASE SAVEPOINT" operation..TwoPhaseTransaction
, DBAPI-specific methods for two
phase transactions may be used.Roll back this .Transaction
.
The implementation of this may vary based on the type of transaction in use:
.RootTransaction
),
it corresponds to a ROLLBACK..NestedTransaction
, it corresponds to a
"ROLLBACK TO SAVEPOINT" operation..TwoPhaseTransaction
, DBAPI-specific methods for two
phase transactions may be used.tuple
=
sqlalchemy.engine.base.NestedTransaction
, sqlalchemy.engine.base.RootTransaction
, sqlalchemy.engine.base.MarkerTransaction
Undocumented
sqlalchemy.engine.base.NestedTransaction
, sqlalchemy.engine.base.RootTransaction
, sqlalchemy.engine.base.MarkerTransaction