Vous avez des améliorations (ou des corrections) à proposer pour ce document :
je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.
Begin a nested transaction (i.e. SAVEPOINT) and return a transaction
handle that controls the scope of the SAVEPOINT.
E.g.::
with engine.begin() as connection:
with connection.begin_nested():
connection.execute(table.insert(), {"username": "sandy"})
The returned object is an instance of
:class:`_engine.NestedTransaction`, which includes transactional
methods :meth:`_engine.NestedTransaction.commit` and
:meth:`_engine.NestedTransaction.rollback`; for a nested transaction,
these methods correspond to the operations "RELEASE SAVEPOINT <name>"
and "ROLLBACK TO SAVEPOINT <name>". The name of the savepoint is local
to the :class:`_engine.NestedTransaction` object and is generated
automatically. Like any other :class:`_engine.Transaction`, the
:class:`_engine.NestedTransaction` may be used as a context manager as
illustrated above which will "release" or "rollback" corresponding to
if the operation within the block were successful or raised an
exception.
Nested transactions require SAVEPOINT support in the underlying
database, else the behavior is undefined. SAVEPOINT is commonly used to
run operations within a transaction that may fail, while continuing the
outer transaction. E.g.::
from sqlalchemy import exc
with engine.begin() as connection:
trans = connection.begin_nested()
try:
connection.execute(table.insert(), {"username": "sandy"})
trans.commit()
except exc.IntegrityError: # catch for duplicate username
trans.rollback() # rollback to savepoint
# outer transaction continues
connection.execute(...)
If :meth:`_engine.Connection.begin_nested` is called without first
calling :meth:`_engine.Connection.begin` or
:meth:`_engine.Engine.begin`, the :class:`_engine.Connection` object
will "autobegin" the outer transaction first. This outer transaction
may be committed using "commit-as-you-go" style, e.g.::
with engine.connect() as connection: # begin() wasn't called
with connection.begin_nested(): # will auto-"begin()" first
connection.execute(...)
# savepoint is released
connection.execute(...)
# explicitly commit outer transaction
connection.commit()
# can continue working with connection here
.. versionchanged:: 2.0
:meth:`_engine.Connection.begin_nested` will now participate
in the connection "autobegin" behavior that is new as of
2.0 / "future" style connections in 1.4.
.. seealso::
:meth:`_engine.Connection.begin`
:ref:`session_begin_nested` - ORM support for SAVEPOINT
Améliorations / Corrections
Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.
Emplacement :
Description des améliorations :