2013-03-05 05:17:35 +08:00
|
|
|
=====================
|
|
|
|
Database transactions
|
|
|
|
=====================
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-03-23 04:12:17 +08:00
|
|
|
.. module:: django.db.transaction
|
2010-02-24 21:55:37 +08:00
|
|
|
|
2013-03-03 22:55:11 +08:00
|
|
|
Django gives you a few ways to control how database transactions are managed.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-05 05:17:35 +08:00
|
|
|
Managing database transactions
|
|
|
|
==============================
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Django's default transaction behavior
|
2013-03-05 05:17:35 +08:00
|
|
|
-------------------------------------
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-03 22:55:11 +08:00
|
|
|
Django's default behavior is to run in autocommit mode. Each query is
|
|
|
|
immediately committed to the database. :ref:`See below for details
|
|
|
|
<autocommit-details>`.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-08 18:35:54 +08:00
|
|
|
Django uses transactions or savepoints automatically to guarantee the
|
|
|
|
integrity of ORM operations that require multiple queries, especially
|
|
|
|
:ref:`delete() <topics-db-queries-delete>` and :ref:`update()
|
|
|
|
<topics-db-queries-update>` queries.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
|
|
|
.. versionchanged:: 1.6
|
2013-03-25 13:53:48 +08:00
|
|
|
|
2013-03-03 22:55:11 +08:00
|
|
|
Previous version of Django featured :ref:`a more complicated default
|
2013-03-05 06:26:31 +08:00
|
|
|
behavior <transactions-upgrading-from-1.5>`.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-06 18:12:24 +08:00
|
|
|
.. _tying-transactions-to-http-requests:
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Tying transactions to HTTP requests
|
2013-03-05 05:17:35 +08:00
|
|
|
-----------------------------------
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-06 18:12:24 +08:00
|
|
|
A common way to handle transactions on the web is to wrap each request in a
|
|
|
|
transaction. Set :setting:`ATOMIC_REQUESTS <DATABASE-ATOMIC_REQUESTS>` to
|
|
|
|
``True`` in the configuration of each database for which you want to enable
|
|
|
|
this behavior.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-13 21:47:48 +08:00
|
|
|
It works like this. Before calling a view function, Django starts a
|
|
|
|
transaction. If the response is produced without problems, Django commits the
|
|
|
|
transaction. If the view produces an exception, Django rolls back the
|
|
|
|
transaction.
|
2013-03-06 18:12:24 +08:00
|
|
|
|
|
|
|
You may perfom partial commits and rollbacks in your view code, typically with
|
|
|
|
the :func:`atomic` context manager. However, at the end of the view, either
|
|
|
|
all the changes will be committed, or none of them.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
While the simplicity of this transaction model is appealing, it also makes it
|
|
|
|
inefficient when traffic increases. Opening a transaction for every view has
|
|
|
|
some overhead. The impact on performance depends on the query patterns of your
|
|
|
|
application and on how well your database handles locking.
|
|
|
|
|
|
|
|
.. admonition:: Per-request transactions and streaming responses
|
|
|
|
|
|
|
|
When a view returns a :class:`~django.http.StreamingHttpResponse`, reading
|
|
|
|
the contents of the response will often execute code to generate the
|
|
|
|
content. Since the view has already returned, such code runs outside of
|
|
|
|
the transaction.
|
|
|
|
|
|
|
|
Generally speaking, it isn't advisable to write to the database while
|
|
|
|
generating a streaming response, since there's no sensible way to handle
|
|
|
|
errors after starting to send the response.
|
|
|
|
|
|
|
|
In practice, this feature simply wraps every view function in the :func:`atomic`
|
|
|
|
decorator described below.
|
|
|
|
|
2013-05-01 00:38:59 +08:00
|
|
|
Note that only the execution of your view is enclosed in the transactions.
|
|
|
|
Middleware runs outside of the transaction, and so does the rendering of
|
2013-03-06 18:12:24 +08:00
|
|
|
template responses.
|
|
|
|
|
2013-05-19 23:55:12 +08:00
|
|
|
When :setting:`ATOMIC_REQUESTS <DATABASE-ATOMIC_REQUESTS>` is enabled, it's
|
|
|
|
still possible to prevent views from running in a transaction.
|
|
|
|
|
|
|
|
.. function:: non_atomic_requests(using=None)
|
|
|
|
|
|
|
|
This decorator will negate the effect of :setting:`ATOMIC_REQUESTS
|
|
|
|
<DATABASE-ATOMIC_REQUESTS>` for a given view::
|
|
|
|
|
|
|
|
from django.db import transaction
|
|
|
|
|
|
|
|
@transaction.non_atomic_requests
|
|
|
|
def my_view(request):
|
|
|
|
do_stuff()
|
|
|
|
|
|
|
|
@transaction.non_atomic_requests(using='other')
|
|
|
|
def my_other_view(request):
|
|
|
|
do_stuff_on_the_other_database()
|
|
|
|
|
|
|
|
It only works if it's applied to the view itself.
|
|
|
|
|
2013-03-06 18:12:24 +08:00
|
|
|
.. versionchanged:: 1.6
|
2013-03-25 13:53:48 +08:00
|
|
|
|
2013-03-06 18:12:24 +08:00
|
|
|
Django used to provide this feature via ``TransactionMiddleware``, which is
|
|
|
|
now deprecated.
|
2012-06-30 22:25:51 +08:00
|
|
|
|
2013-03-05 05:17:35 +08:00
|
|
|
Controlling transactions explicitly
|
|
|
|
-----------------------------------
|
|
|
|
|
|
|
|
.. versionadded:: 1.6
|
|
|
|
|
|
|
|
Django provides a single API to control database transactions.
|
|
|
|
|
2013-03-08 22:44:41 +08:00
|
|
|
.. function:: atomic(using=None, savepoint=True)
|
2013-03-05 05:17:35 +08:00
|
|
|
|
2013-05-17 22:12:49 +08:00
|
|
|
Atomicity is the defining property of database transactions. ``atomic``
|
|
|
|
allows us to create a block of code within which the atomicity on the
|
|
|
|
database is guaranteed. If the block of code is successfully completed, the
|
|
|
|
changes are committed to the database. If there is an exception, the
|
|
|
|
changes are rolled back.
|
2013-03-05 05:17:35 +08:00
|
|
|
|
2013-05-17 22:12:49 +08:00
|
|
|
``atomic`` blocks can be nested. In this case, when an inner block
|
|
|
|
completes successfully, its effects can still be rolled back if an
|
|
|
|
exception is raised in the outer block at a later point.
|
2013-03-05 05:17:35 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
``atomic`` is usable both as a `decorator`_::
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
from django.db import transaction
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def viewfunc(request):
|
|
|
|
# This code executes inside a transaction.
|
|
|
|
do_stuff()
|
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
and as a `context manager`_::
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
from django.db import transaction
|
|
|
|
|
|
|
|
def viewfunc(request):
|
|
|
|
# This code executes in autocommit mode (Django's default).
|
|
|
|
do_stuff()
|
|
|
|
|
|
|
|
with transaction.atomic():
|
|
|
|
# This code executes inside a transaction.
|
|
|
|
do_more_stuff()
|
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
.. _decorator: http://docs.python.org/glossary.html#term-decorator
|
|
|
|
.. _context manager: http://docs.python.org/glossary.html#term-context-manager
|
|
|
|
|
2013-03-05 05:17:35 +08:00
|
|
|
Wrapping ``atomic`` in a try/except block allows for natural handling of
|
|
|
|
integrity errors::
|
|
|
|
|
|
|
|
from django.db import IntegrityError, transaction
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def viewfunc(request):
|
2013-05-17 22:12:49 +08:00
|
|
|
create_parent()
|
2013-03-05 05:17:35 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
with transaction.atomic():
|
2013-05-17 22:12:49 +08:00
|
|
|
generate_relationships()
|
2013-03-05 05:17:35 +08:00
|
|
|
except IntegrityError:
|
|
|
|
handle_exception()
|
|
|
|
|
2013-05-17 22:12:49 +08:00
|
|
|
add_children()
|
2013-03-05 05:17:35 +08:00
|
|
|
|
2013-05-17 22:12:49 +08:00
|
|
|
In this example, even if ``generate_relationships()`` causes a database
|
2013-03-05 05:17:35 +08:00
|
|
|
error by breaking an integrity constraint, you can execute queries in
|
2013-05-17 22:12:49 +08:00
|
|
|
``add_children()``, and the changes from ``create_parent()`` are still
|
|
|
|
there. Note that any operations attempted in ``generate_relationships()``
|
|
|
|
will already have been rolled back safely when ``handle_exception()`` is
|
|
|
|
called, so the exception handler can also operate on the database if
|
|
|
|
necessary.
|
2013-03-05 05:17:35 +08:00
|
|
|
|
2013-09-21 03:56:35 +08:00
|
|
|
.. admonition:: Don't catch database exceptions inside ``atomic``!
|
|
|
|
|
|
|
|
If you catch :exc:`~django.db.DatabaseError` or a subclass such as
|
|
|
|
:exc:`~django.db.IntegrityError` inside an ``atomic`` block, you will
|
|
|
|
hide from Django the fact that an error has occurred and that the
|
|
|
|
transaction is broken. At this point, Django's behavior is unspecified
|
|
|
|
and database-dependent. It will usually result in a rollback, which
|
|
|
|
may break your expectations, since you caught the exception.
|
|
|
|
|
|
|
|
The correct way to catch database errors is around an ``atomic`` block
|
|
|
|
as shown above. If necessary, add an extra ``atomic`` block for this
|
|
|
|
purpose -- it's cheap! This pattern is useful to delimit explicitly
|
|
|
|
which operations will be rolled back if an exception occurs.
|
|
|
|
|
2013-03-05 05:17:35 +08:00
|
|
|
In order to guarantee atomicity, ``atomic`` disables some APIs. Attempting
|
|
|
|
to commit, roll back, or change the autocommit state of the database
|
|
|
|
connection within an ``atomic`` block will raise an exception.
|
|
|
|
|
2013-05-17 22:12:49 +08:00
|
|
|
``atomic`` takes a ``using`` argument which should be the name of a
|
|
|
|
database. If this argument isn't provided, Django uses the ``"default"``
|
|
|
|
database.
|
|
|
|
|
2013-03-05 05:17:35 +08:00
|
|
|
Under the hood, Django's transaction management code:
|
|
|
|
|
|
|
|
- opens a transaction when entering the outermost ``atomic`` block;
|
|
|
|
- creates a savepoint when entering an inner ``atomic`` block;
|
|
|
|
- releases or rolls back to the savepoint when exiting an inner block;
|
|
|
|
- commits or rolls back the transaction when exiting the outermost block.
|
|
|
|
|
2013-03-08 22:44:41 +08:00
|
|
|
You can disable the creation of savepoints for inner blocks by setting the
|
|
|
|
``savepoint`` argument to ``False``. If an exception occurs, Django will
|
|
|
|
perform the rollback when exiting the first parent block with a savepoint
|
|
|
|
if there is one, and the outermost block otherwise. Atomicity is still
|
|
|
|
guaranteed by the outer transaction. This option should only be used if
|
|
|
|
the overhead of savepoints is noticeable. It has the drawback of breaking
|
|
|
|
the error handling described above.
|
|
|
|
|
2013-03-13 21:08:32 +08:00
|
|
|
You may use ``atomic`` when autocommit is turned off. It will only use
|
|
|
|
savepoints, even for the outermost block, and it will raise an exception
|
|
|
|
if the outermost block is declared with ``savepoint=False``.
|
|
|
|
|
2013-03-08 06:06:58 +08:00
|
|
|
.. admonition:: Performance considerations
|
|
|
|
|
|
|
|
Open transactions have a performance cost for your database server. To
|
|
|
|
minimize this overhead, keep your transactions as short as possible. This
|
|
|
|
is especially important of you're using :func:`atomic` in long-running
|
|
|
|
processes, outside of Django's request / response cycle.
|
|
|
|
|
2013-03-07 21:42:51 +08:00
|
|
|
Autocommit
|
|
|
|
==========
|
|
|
|
|
|
|
|
.. _autocommit-details:
|
|
|
|
|
|
|
|
Why Django uses autocommit
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
In the SQL standards, each SQL query starts a transaction, unless one is
|
|
|
|
already in progress. Such transactions must then be committed or rolled back.
|
|
|
|
|
|
|
|
This isn't always convenient for application developers. To alleviate this
|
|
|
|
problem, most databases provide an autocommit mode. When autocommit is turned
|
|
|
|
on, each SQL query is wrapped in its own transaction. In other words, the
|
|
|
|
transaction is not only automatically started, but also automatically
|
|
|
|
committed.
|
|
|
|
|
|
|
|
:pep:`249`, the Python Database API Specification v2.0, requires autocommit to
|
|
|
|
be initially turned off. Django overrides this default and turns autocommit
|
|
|
|
on.
|
|
|
|
|
|
|
|
To avoid this, you can :ref:`deactivate the transaction management
|
|
|
|
<deactivate-transaction-management>`, but it isn't recommended.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.6
|
2013-03-25 13:53:48 +08:00
|
|
|
|
2013-03-07 21:42:51 +08:00
|
|
|
Before Django 1.6, autocommit was turned off, and it was emulated by
|
|
|
|
forcing a commit after write operations in the ORM.
|
|
|
|
|
|
|
|
.. _deactivate-transaction-management:
|
|
|
|
|
|
|
|
Deactivating transaction management
|
|
|
|
-----------------------------------
|
|
|
|
|
|
|
|
You can totally disable Django's transaction management for a given database
|
|
|
|
by setting :setting:`AUTOCOMMIT <DATABASE-AUTOCOMMIT>` to ``False`` in its
|
|
|
|
configuration. If you do this, Django won't enable autocommit, and won't
|
|
|
|
perform any commits. You'll get the regular behavior of the underlying
|
|
|
|
database library.
|
|
|
|
|
|
|
|
This requires you to commit explicitly every transaction, even those started
|
|
|
|
by Django or by third-party libraries. Thus, this is best used in situations
|
|
|
|
where you want to run your own transaction-controlling middleware or do
|
|
|
|
something really strange.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.6
|
2013-03-25 13:53:48 +08:00
|
|
|
|
2013-03-07 21:42:51 +08:00
|
|
|
This used to be controlled by the ``TRANSACTIONS_MANAGED`` setting.
|
|
|
|
|
|
|
|
Low-level APIs
|
|
|
|
==============
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Always prefer :func:`atomic` if possible at all. It accounts for the
|
|
|
|
idiosyncrasies of each database and prevents invalid operations.
|
|
|
|
|
|
|
|
The low level APIs are only useful if you're implementing your own
|
|
|
|
transaction management.
|
|
|
|
|
|
|
|
.. _managing-autocommit:
|
|
|
|
|
|
|
|
Autocommit
|
|
|
|
----------
|
|
|
|
|
|
|
|
.. versionadded:: 1.6
|
|
|
|
|
2013-03-13 21:47:48 +08:00
|
|
|
Django provides a straightforward API in the :mod:`django.db.transaction`
|
|
|
|
module to manage the autocommit state of each database connection.
|
2013-03-07 21:42:51 +08:00
|
|
|
|
|
|
|
.. function:: get_autocommit(using=None)
|
|
|
|
|
2013-03-11 22:10:58 +08:00
|
|
|
.. function:: set_autocommit(autocommit, using=None)
|
2013-03-07 21:42:51 +08:00
|
|
|
|
|
|
|
These functions take a ``using`` argument which should be the name of a
|
|
|
|
database. If it isn't provided, Django uses the ``"default"`` database.
|
|
|
|
|
|
|
|
Autocommit is initially turned on. If you turn it off, it's your
|
|
|
|
responsibility to restore it.
|
|
|
|
|
|
|
|
Once you turn autocommit off, you get the default behavior of your database
|
|
|
|
adapter, and Django won't help you. Although that behavior is specified in
|
|
|
|
:pep:`249`, implementations of adapters aren't always consistent with one
|
|
|
|
another. Review the documentation of the adapter you're using carefully.
|
|
|
|
|
|
|
|
You must ensure that no transaction is active, usually by issuing a
|
|
|
|
:func:`commit` or a :func:`rollback`, before turning autocommit back on.
|
|
|
|
|
2013-03-13 21:08:32 +08:00
|
|
|
Django will refuse to turn autocommit off when an :func:`atomic` block is
|
|
|
|
active, because that would break atomicity.
|
2013-03-07 21:42:51 +08:00
|
|
|
|
|
|
|
Transactions
|
|
|
|
------------
|
|
|
|
|
|
|
|
A transaction is an atomic set of database queries. Even if your program
|
|
|
|
crashes, the database guarantees that either all the changes will be applied,
|
|
|
|
or none of them.
|
|
|
|
|
|
|
|
Django doesn't provide an API to start a transaction. The expected way to
|
|
|
|
start a transaction is to disable autocommit with :func:`set_autocommit`.
|
|
|
|
|
|
|
|
Once you're in a transaction, you can choose either to apply the changes
|
|
|
|
you've performed until this point with :func:`commit`, or to cancel them with
|
2013-03-13 21:47:48 +08:00
|
|
|
:func:`rollback`. These functions are defined in :mod:`django.db.transaction`.
|
2013-03-07 21:42:51 +08:00
|
|
|
|
|
|
|
.. function:: commit(using=None)
|
|
|
|
|
|
|
|
.. function:: rollback(using=None)
|
|
|
|
|
|
|
|
These functions take a ``using`` argument which should be the name of a
|
|
|
|
database. If it isn't provided, Django uses the ``"default"`` database.
|
|
|
|
|
|
|
|
Django will refuse to commit or to rollback when an :func:`atomic` block is
|
|
|
|
active, because that would break atomicity.
|
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
.. _topics-db-transactions-savepoints:
|
2011-02-12 21:03:34 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
Savepoints
|
2013-03-07 21:42:51 +08:00
|
|
|
----------
|
2010-10-20 03:38:15 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
A savepoint is a marker within a transaction that enables you to roll back
|
|
|
|
part of a transaction, rather than the full transaction. Savepoints are
|
|
|
|
available with the SQLite (≥ 3.6.8), PostgreSQL, Oracle and MySQL (when using
|
|
|
|
the InnoDB storage engine) backends. Other backends provide the savepoint
|
|
|
|
functions, but they're empty operations -- they don't actually do anything.
|
2010-10-20 03:38:15 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
Savepoints aren't especially useful if you are using autocommit, the default
|
|
|
|
behavior of Django. However, once you open a transaction with :func:`atomic`,
|
|
|
|
you build up a series of database operations awaiting a commit or rollback. If
|
|
|
|
you issue a rollback, the entire transaction is rolled back. Savepoints
|
|
|
|
provide the ability to perform a fine-grained rollback, rather than the full
|
|
|
|
rollback that would be performed by ``transaction.rollback()``.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
.. versionchanged:: 1.6
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-25 13:53:48 +08:00
|
|
|
When the :func:`atomic` decorator is nested, it creates a savepoint to allow
|
|
|
|
partial commit or rollback. You're strongly encouraged to use :func:`atomic`
|
|
|
|
rather than the functions described below, but they're still part of the
|
|
|
|
public API, and there's no plan to deprecate them.
|
2009-03-11 15:06:50 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
Each of these functions takes a ``using`` argument which should be the name of
|
|
|
|
a database for which the behavior applies. If no ``using`` argument is
|
|
|
|
provided then the ``"default"`` database is used.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-07 21:05:32 +08:00
|
|
|
Savepoints are controlled by three functions in :mod:`django.db.transaction`:
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-07 21:05:32 +08:00
|
|
|
.. function:: savepoint(using=None)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-13 21:47:48 +08:00
|
|
|
Creates a new savepoint. This marks a point in the transaction that is
|
|
|
|
known to be in a "good" state. Returns the savepoint ID (``sid``).
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2013-03-07 21:05:32 +08:00
|
|
|
.. function:: savepoint_commit(sid, using=None)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-07 21:05:32 +08:00
|
|
|
Releases savepoint ``sid``. The changes performed since the savepoint was
|
|
|
|
created become part of the transaction.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-07 21:05:32 +08:00
|
|
|
.. function:: savepoint_rollback(sid, using=None)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-07 21:05:32 +08:00
|
|
|
Rolls back the transaction to savepoint ``sid``.
|
|
|
|
|
|
|
|
These functions do nothing if savepoints aren't supported or if the database
|
|
|
|
is in autocommit mode.
|
|
|
|
|
|
|
|
In addition, there's a utility function:
|
|
|
|
|
|
|
|
.. function:: clean_savepoints(using=None)
|
|
|
|
|
|
|
|
Resets the counter used to generate unique savepoint IDs.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
The following example demonstrates the use of savepoints::
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
from django.db import transaction
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
# open a transaction
|
|
|
|
@transaction.atomic
|
|
|
|
def viewfunc(request):
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-25 13:53:48 +08:00
|
|
|
a.save()
|
|
|
|
# transaction now contains a.save()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-25 13:53:48 +08:00
|
|
|
sid = transaction.savepoint()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-25 13:53:48 +08:00
|
|
|
b.save()
|
|
|
|
# transaction now contains a.save() and b.save()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-25 13:53:48 +08:00
|
|
|
if want_to_keep_b:
|
|
|
|
transaction.savepoint_commit(sid)
|
|
|
|
# open transaction still contains a.save() and b.save()
|
|
|
|
else:
|
|
|
|
transaction.savepoint_rollback(sid)
|
|
|
|
# open transaction now contains only a.save()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-06-28 04:19:54 +08:00
|
|
|
.. versionadded:: 1.6
|
|
|
|
|
|
|
|
Savepoints may be used to recover from a database error by performing a partial
|
|
|
|
rollback. If you're doing this inside an :func:`atomic` block, the entire block
|
|
|
|
will still be rolled back, because it doesn't know you've handled the situation
|
|
|
|
at a lower level! To prevent this, you can control the rollback behavior with
|
|
|
|
the following functions.
|
|
|
|
|
|
|
|
.. function:: get_rollback(using=None)
|
|
|
|
|
|
|
|
.. function:: set_rollback(rollback, using=None)
|
|
|
|
|
|
|
|
Setting the rollback flag to ``True`` forces a rollback when exiting the
|
|
|
|
innermost atomic block. This may be useful to trigger a rollback without
|
|
|
|
raising an exception.
|
|
|
|
|
|
|
|
Setting it to ``False`` prevents such a rollback. Before doing that, make sure
|
|
|
|
you've rolled back the transaction to a known-good savepoint within the current
|
|
|
|
atomic block! Otherwise you're breaking atomicity and data corruption may
|
|
|
|
occur.
|
|
|
|
|
2013-03-03 22:55:11 +08:00
|
|
|
Database-specific notes
|
|
|
|
=======================
|
|
|
|
|
2013-03-24 20:08:29 +08:00
|
|
|
.. _savepoints-in-sqlite:
|
|
|
|
|
2013-03-04 22:57:04 +08:00
|
|
|
Savepoints in SQLite
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
While SQLite ≥ 3.6.8 supports savepoints, a flaw in the design of the
|
2013-03-24 20:08:29 +08:00
|
|
|
:mod:`sqlite3` module makes them hardly usable.
|
2013-03-04 22:57:04 +08:00
|
|
|
|
|
|
|
When autocommit is enabled, savepoints don't make sense. When it's disabled,
|
2013-03-13 21:47:48 +08:00
|
|
|
:mod:`sqlite3` commits implicitly before savepoint statements. (In fact, it
|
2013-03-04 22:57:04 +08:00
|
|
|
commits before any statement other than ``SELECT``, ``INSERT``, ``UPDATE``,
|
2013-03-13 21:47:48 +08:00
|
|
|
``DELETE`` and ``REPLACE``.) This bug has two consequences:
|
2013-03-13 21:08:32 +08:00
|
|
|
|
|
|
|
- The low level APIs for savepoints are only usable inside a transaction ie.
|
|
|
|
inside an :func:`atomic` block.
|
|
|
|
- It's impossible to use :func:`atomic` when autocommit is turned off.
|
2013-03-04 22:57:04 +08:00
|
|
|
|
2006-07-24 10:39:50 +08:00
|
|
|
Transactions in MySQL
|
2013-03-03 22:55:11 +08:00
|
|
|
---------------------
|
2006-07-24 10:39:50 +08:00
|
|
|
|
|
|
|
If you're using MySQL, your tables may or may not support transactions; it
|
|
|
|
depends on your MySQL version and the table types you're using. (By
|
|
|
|
"table types," we mean something like "InnoDB" or "MyISAM".) MySQL transaction
|
|
|
|
peculiarities are outside the scope of this article, but the MySQL site has
|
|
|
|
`information on MySQL transactions`_.
|
|
|
|
|
2013-03-13 21:47:48 +08:00
|
|
|
If your MySQL setup does *not* support transactions, then Django will always
|
|
|
|
function in autocommit mode: statements will be executed and committed as soon
|
|
|
|
as they're called. If your MySQL setup *does* support transactions, Django
|
|
|
|
will handle transactions as explained in this document.
|
2006-07-24 10:39:50 +08:00
|
|
|
|
2008-08-15 21:44:43 +08:00
|
|
|
.. _information on MySQL transactions: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-transactions.html
|
2009-05-02 15:40:25 +08:00
|
|
|
|
2009-05-16 14:23:06 +08:00
|
|
|
Handling exceptions within PostgreSQL transactions
|
2013-03-03 22:55:11 +08:00
|
|
|
--------------------------------------------------
|
2009-05-02 15:40:25 +08:00
|
|
|
|
2013-03-13 21:47:48 +08:00
|
|
|
.. note::
|
2013-03-26 08:56:52 +08:00
|
|
|
|
2013-03-13 21:47:48 +08:00
|
|
|
This section is relevant only if you're implementing your own transaction
|
|
|
|
management. This problem cannot occur in Django's default mode and
|
|
|
|
:func:`atomic` handles it automatically.
|
|
|
|
|
|
|
|
Inside a transaction, when a call to a PostgreSQL cursor raises an exception
|
|
|
|
(typically ``IntegrityError``), all subsequent SQL in the same transaction
|
|
|
|
will fail with the error "current transaction is aborted, queries ignored
|
|
|
|
until end of transaction block". Whilst simple use of ``save()`` is unlikely
|
|
|
|
to raise an exception in PostgreSQL, there are more advanced usage patterns
|
|
|
|
which might, such as saving objects with unique fields, saving using the
|
2009-05-16 14:23:06 +08:00
|
|
|
force_insert/force_update flag, or invoking custom SQL.
|
2009-05-02 15:40:25 +08:00
|
|
|
|
2009-05-16 14:23:06 +08:00
|
|
|
There are several ways to recover from this sort of error.
|
2009-05-02 15:40:25 +08:00
|
|
|
|
2009-05-16 14:23:06 +08:00
|
|
|
Transaction rollback
|
2013-03-03 22:55:11 +08:00
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
2009-05-16 14:23:06 +08:00
|
|
|
|
|
|
|
The first option is to roll back the entire transaction. For example::
|
|
|
|
|
|
|
|
a.save() # Succeeds, but may be undone by transaction rollback
|
2009-05-02 15:40:25 +08:00
|
|
|
try:
|
2009-05-16 14:23:06 +08:00
|
|
|
b.save() # Could throw exception
|
2009-05-02 15:40:25 +08:00
|
|
|
except IntegrityError:
|
2009-05-16 14:23:06 +08:00
|
|
|
transaction.rollback()
|
|
|
|
c.save() # Succeeds, but a.save() may have been undone
|
|
|
|
|
|
|
|
Calling ``transaction.rollback()`` rolls back the entire transaction. Any
|
|
|
|
uncommitted database operations will be lost. In this example, the changes
|
|
|
|
made by ``a.save()`` would be lost, even though that operation raised no error
|
|
|
|
itself.
|
|
|
|
|
|
|
|
Savepoint rollback
|
2013-03-03 22:55:11 +08:00
|
|
|
~~~~~~~~~~~~~~~~~~
|
2009-05-16 14:23:06 +08:00
|
|
|
|
2013-03-04 22:57:04 +08:00
|
|
|
You can use :ref:`savepoints <topics-db-transactions-savepoints>` to control
|
|
|
|
the extent of a rollback. Before performing a database operation that could
|
|
|
|
fail, you can set or update the savepoint; that way, if the operation fails,
|
|
|
|
you can roll back the single offending operation, rather than the entire
|
|
|
|
transaction. For example::
|
2009-05-16 14:23:06 +08:00
|
|
|
|
|
|
|
a.save() # Succeeds, and never undone by savepoint rollback
|
|
|
|
try:
|
|
|
|
sid = transaction.savepoint()
|
|
|
|
b.save() # Could throw exception
|
|
|
|
transaction.savepoint_commit(sid)
|
|
|
|
except IntegrityError:
|
|
|
|
transaction.savepoint_rollback(sid)
|
|
|
|
c.save() # Succeeds, and a.save() is never undone
|
|
|
|
|
|
|
|
In this example, ``a.save()`` will not be undone in the case where
|
|
|
|
``b.save()`` raises an exception.
|
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
.. _transactions-upgrading-from-1.5:
|
2009-05-16 14:23:06 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
Changes from Django 1.5 and earlier
|
|
|
|
===================================
|
2009-05-16 14:23:06 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
The features described below were deprecated in Django 1.6 and will be removed
|
|
|
|
in Django 1.8. They're documented in order to ease the migration to the new
|
|
|
|
transaction management APIs.
|
2009-05-16 14:23:06 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
Legacy APIs
|
|
|
|
-----------
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
The following functions, defined in ``django.db.transaction``, provided a way
|
|
|
|
to control transactions on a per-function or per-code-block basis. They could
|
|
|
|
be used as decorators or as context managers, and they accepted a ``using``
|
|
|
|
argument, exactly like :func:`atomic`.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
.. function:: autocommit
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
Enable Django's default autocommit behavior.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
Transactions will be committed as soon as you call ``model.save()``,
|
|
|
|
``model.delete()``, or any other function that writes to the database.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
.. function:: commit_on_success
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
Use a single transaction for all the work done in a function.
|
|
|
|
|
|
|
|
If the function returns successfully, then Django will commit all work done
|
|
|
|
within the function at that point. If the function raises an exception,
|
|
|
|
though, Django will roll back the transaction.
|
|
|
|
|
|
|
|
.. function:: commit_manually
|
|
|
|
|
|
|
|
Tells Django you'll be managing the transaction on your own.
|
|
|
|
|
|
|
|
Whether you are writing or simply reading from the database, you must
|
|
|
|
``commit()`` or ``rollback()`` explicitly or Django will raise a
|
|
|
|
:exc:`TransactionManagementError` exception. This is required when reading
|
|
|
|
from the database because ``SELECT`` statements may call functions which
|
|
|
|
modify tables, and thus it is impossible to know if any data has been
|
|
|
|
modified.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
|
|
|
.. _transaction-states:
|
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
Transaction states
|
|
|
|
------------------
|
|
|
|
|
|
|
|
The three functions described above relied on a concept called "transaction
|
2013-05-19 23:55:12 +08:00
|
|
|
states". This mechanism was deprecated in Django 1.6, but it's still available
|
|
|
|
until Django 1.8.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
|
|
|
At any time, each database connection is in one of these two states:
|
|
|
|
|
|
|
|
- **auto mode**: autocommit is enabled;
|
|
|
|
- **managed mode**: autocommit is disabled.
|
|
|
|
|
|
|
|
Django starts in auto mode. ``TransactionMiddleware``,
|
|
|
|
:func:`commit_on_success` and :func:`commit_manually` activate managed mode;
|
|
|
|
:func:`autocommit` activates auto mode.
|
|
|
|
|
|
|
|
Internally, Django keeps a stack of states. Activations and deactivations must
|
|
|
|
be balanced.
|
|
|
|
|
2013-03-13 21:47:48 +08:00
|
|
|
For example, :func:`commit_on_success` switches to managed mode when entering
|
|
|
|
the block of code it controls; when exiting the block, it commits or
|
|
|
|
rollbacks, and switches back to auto mode.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
So :func:`commit_on_success` really has two effects: it changes the
|
|
|
|
transaction state and it defines an transaction block. Nesting will give the
|
|
|
|
expected results in terms of transaction state, but not in terms of
|
|
|
|
transaction semantics. Most often, the inner block will commit, breaking the
|
|
|
|
atomicity of the outer block.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
:func:`autocommit` and :func:`commit_manually` have similar limitations.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
API changes
|
|
|
|
-----------
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-06 18:12:24 +08:00
|
|
|
Transaction middleware
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2013-05-27 16:14:38 +08:00
|
|
|
In Django 1.6, ``TransactionMiddleware`` is deprecated and replaced by
|
2013-03-06 18:12:24 +08:00
|
|
|
:setting:`ATOMIC_REQUESTS <DATABASE-ATOMIC_REQUESTS>`. While the general
|
2013-05-19 23:55:12 +08:00
|
|
|
behavior is the same, there are two differences.
|
2013-03-06 18:12:24 +08:00
|
|
|
|
2013-05-19 23:55:12 +08:00
|
|
|
With the previous API, it was possible to switch to autocommit or to commit
|
|
|
|
explicitly anywhere inside a view. Since :setting:`ATOMIC_REQUESTS
|
|
|
|
<DATABASE-ATOMIC_REQUESTS>` relies on :func:`atomic` which enforces atomicity,
|
|
|
|
this isn't allowed any longer. However, at the toplevel, it's still possible
|
|
|
|
to avoid wrapping an entire view in a transaction. To achieve this, decorate
|
|
|
|
the view with :func:`non_atomic_requests` instead of :func:`autocommit`.
|
2013-03-06 18:12:24 +08:00
|
|
|
|
|
|
|
The transaction middleware applied not only to view functions, but also to
|
2013-03-13 21:47:48 +08:00
|
|
|
middleware modules that came after it. For instance, if you used the session
|
2013-03-06 18:12:24 +08:00
|
|
|
middleware after the transaction middleware, session creation was part of the
|
|
|
|
transaction. :setting:`ATOMIC_REQUESTS <DATABASE-ATOMIC_REQUESTS>` only
|
|
|
|
applies to the view itself.
|
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
Managing transactions
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
Starting with Django 1.6, :func:`atomic` is the only supported API for
|
|
|
|
defining a transaction. Unlike the deprecated APIs, it's nestable and always
|
|
|
|
guarantees atomicity.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
In most cases, it will be a drop-in replacement for :func:`commit_on_success`.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
During the deprecation period, it's possible to use :func:`atomic` within
|
|
|
|
:func:`autocommit`, :func:`commit_on_success` or :func:`commit_manually`.
|
|
|
|
However, the reverse is forbidden, because nesting the old decorators /
|
|
|
|
context managers breaks atomicity.
|
|
|
|
|
|
|
|
Managing autocommit
|
|
|
|
~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Django 1.6 introduces an explicit :ref:`API for mananging autocommit
|
|
|
|
<managing-autocommit>`.
|
|
|
|
|
|
|
|
To disable autocommit temporarily, instead of::
|
2013-03-03 22:55:11 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
with transaction.commit_manually():
|
|
|
|
# do stuff
|
|
|
|
|
|
|
|
you should now use::
|
|
|
|
|
2013-03-11 22:10:58 +08:00
|
|
|
transaction.set_autocommit(False)
|
2013-03-05 06:26:31 +08:00
|
|
|
try:
|
|
|
|
# do stuff
|
|
|
|
finally:
|
2013-03-11 22:10:58 +08:00
|
|
|
transaction.set_autocommit(True)
|
2013-03-05 06:26:31 +08:00
|
|
|
|
|
|
|
To enable autocommit temporarily, instead of::
|
|
|
|
|
|
|
|
with transaction.autocommit():
|
|
|
|
# do stuff
|
|
|
|
|
|
|
|
you should now use::
|
|
|
|
|
2013-03-11 22:10:58 +08:00
|
|
|
transaction.set_autocommit(True)
|
2013-03-05 06:26:31 +08:00
|
|
|
try:
|
|
|
|
# do stuff
|
|
|
|
finally:
|
2013-03-11 22:10:58 +08:00
|
|
|
transaction.set_autocommit(False)
|
2013-03-05 06:26:31 +08:00
|
|
|
|
2013-05-19 23:55:12 +08:00
|
|
|
Unless you're implementing a transaction management framework, you shouldn't
|
|
|
|
ever need to do this.
|
|
|
|
|
2013-03-06 18:12:24 +08:00
|
|
|
Disabling transaction management
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Instead of setting ``TRANSACTIONS_MANAGED = True``, set the ``AUTOCOMMIT`` key
|
2013-03-13 21:08:32 +08:00
|
|
|
to ``False`` in the configuration of each database, as explained in
|
|
|
|
:ref:`deactivate-transaction-management`.
|
2013-03-06 18:12:24 +08:00
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
Backwards incompatibilities
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
Since version 1.6, Django uses database-level autocommit in auto mode.
|
2013-03-03 22:55:11 +08:00
|
|
|
Previously, it implemented application-level autocommit by triggering a commit
|
|
|
|
after each ORM write.
|
|
|
|
|
2013-03-05 06:26:31 +08:00
|
|
|
As a consequence, each database query (for instance, an ORM read) started a
|
|
|
|
transaction that lasted until the next ORM write. Such "automatic
|
|
|
|
transactions" no longer exist in Django 1.6.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
|
|
|
There are four known scenarios where this is backwards-incompatible.
|
|
|
|
|
|
|
|
Note that managed mode isn't affected at all. This section assumes auto mode.
|
|
|
|
See the :ref:`description of modes <transaction-states>` above.
|
|
|
|
|
|
|
|
Sequences of custom SQL queries
|
2013-03-05 06:26:31 +08:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2013-03-03 22:55:11 +08:00
|
|
|
|
|
|
|
If you're executing several :ref:`custom SQL queries <executing-custom-sql>`
|
|
|
|
in a row, each one now runs in its own transaction, instead of sharing the
|
|
|
|
same "automatic transaction". If you need to enforce atomicity, you must wrap
|
2013-05-19 23:55:12 +08:00
|
|
|
the sequence of queries in :func:`atomic`.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
|
|
|
To check for this problem, look for calls to ``cursor.execute()``. They're
|
2013-03-13 21:47:48 +08:00
|
|
|
usually followed by a call to ``transaction.commit_unless_managed()``, which
|
|
|
|
isn't useful any more and should be removed.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
|
|
|
Select for update
|
2013-03-05 06:26:31 +08:00
|
|
|
~~~~~~~~~~~~~~~~~
|
2013-03-03 22:55:11 +08:00
|
|
|
|
|
|
|
If you were relying on "automatic transactions" to provide locking between
|
|
|
|
:meth:`~django.db.models.query.QuerySet.select_for_update` and a subsequent
|
|
|
|
write operation — an extremely fragile design, but nonetheless possible — you
|
2013-03-05 06:26:31 +08:00
|
|
|
must wrap the relevant code in :func:`atomic`.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
|
|
|
Using a high isolation level
|
2013-03-05 06:26:31 +08:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2013-03-03 22:55:11 +08:00
|
|
|
|
|
|
|
If you were using the "repeatable read" isolation level or higher, and if you
|
|
|
|
relied on "automatic transactions" to guarantee consistency between successive
|
2013-03-05 06:26:31 +08:00
|
|
|
reads, the new behavior might be backwards-incompatible. To enforce
|
|
|
|
consistency, you must wrap such sequences in :func:`atomic`.
|
2013-03-03 22:55:11 +08:00
|
|
|
|
|
|
|
MySQL defaults to "repeatable read" and SQLite to "serializable"; they may be
|
|
|
|
affected by this problem.
|
|
|
|
|
|
|
|
At the "read committed" isolation level or lower, "automatic transactions"
|
|
|
|
have no effect on the semantics of any sequence of ORM operations.
|
|
|
|
|
|
|
|
PostgreSQL and Oracle default to "read committed" and aren't affected, unless
|
|
|
|
you changed the isolation level.
|
|
|
|
|
|
|
|
Using unsupported database features
|
2013-03-05 06:26:31 +08:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2009-05-02 15:40:25 +08:00
|
|
|
|
2013-03-03 22:55:11 +08:00
|
|
|
With triggers, views, or functions, it's possible to make ORM reads result in
|
|
|
|
database modifications. Django 1.5 and earlier doesn't deal with this case and
|
|
|
|
it's theoretically possible to observe a different behavior after upgrading to
|
2013-03-05 06:26:31 +08:00
|
|
|
Django 1.6 or later. In doubt, use :func:`atomic` to enforce integrity.
|