Fixed #21836 -- Improved transaction docs about autocommit mode

Clarified that queries in autocommit mode are committed immediately
only if a transaction has not already been started. Added to the
main transaction docs that Django's TestCase class implicitly wraps
its tests in transactions.
This commit is contained in:
Chris Jerdonek 2014-01-14 04:41:52 -08:00 committed by Aymeric Augustin
parent 7dd87aa305
commit 798fd59fad
1 changed files with 11 additions and 6 deletions

View File

@ -13,14 +13,17 @@ Django's default transaction behavior
------------------------------------- -------------------------------------
Django's default behavior is to run in autocommit mode. Each query is Django's default behavior is to run in autocommit mode. Each query is
immediately committed to the database. :ref:`See below for details immediately committed to the database, unless a transaction is active.
<autocommit-details>`. :ref:`See below for details <autocommit-details>`.
Django uses transactions or savepoints automatically to guarantee the Django uses transactions or savepoints automatically to guarantee the
integrity of ORM operations that require multiple queries, especially integrity of ORM operations that require multiple queries, especially
:ref:`delete() <topics-db-queries-delete>` and :ref:`update() :ref:`delete() <topics-db-queries-delete>` and :ref:`update()
<topics-db-queries-update>` queries. <topics-db-queries-update>` queries.
Django's :class:`~django.test.TestCase` class also wraps each test in a
transaction for performance reasons.
.. versionchanged:: 1.6 .. versionchanged:: 1.6
Previous version of Django featured :ref:`a more complicated default Previous version of Django featured :ref:`a more complicated default
@ -231,13 +234,15 @@ Why Django uses autocommit
-------------------------- --------------------------
In the SQL standards, each SQL query starts a transaction, unless one is 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. already active. Such transactions must then be explicitly committed or rolled
back.
This isn't always convenient for application developers. To alleviate this This isn't always convenient for application developers. To alleviate this
problem, most databases provide an autocommit mode. When autocommit is turned 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 on and no transaction is active, each SQL query gets wrapped in its own
transaction is not only automatically started, but also automatically transaction. In other words, not only does each such query starts a
committed. transaction, but the transaction also gets automatically committed or rolled
back, depending on whether the query succeeded.
:pep:`249`, the Python Database API Specification v2.0, requires autocommit to :pep:`249`, the Python Database API Specification v2.0, requires autocommit to
be initially turned off. Django overrides this default and turns autocommit be initially turned off. Django overrides this default and turns autocommit