Django では、データベースのトランザクションをコントロールする方法が提供されています。
Django のデフォルトの動作は、オートコミットモードで実行することです。トランザクションがアクティブでない限り、各クエリは即座にデータベースにコミットされます。詳しくは下記を参照してください。
Django は、自動的にトランザクションやセーブポイントを使い、特に delete() と update() クエリにおいて、複数のクエリを要求する ORM 操作の信頼性を担保します。
Django の TestCase
クラスは、パフォーマンス向上のため、各テストをトランザクションでラップします。
ウェブ上でトランザクションを扱う一般的な方法は、各リクエストをトランザクションでラップすることです。この動作を有効化したい各データベースの設定で、ATOMIC_REQUESTS
を True
にセットしてください。
これは、次のように動作します。まず、ビュー関数を呼び出す前に、Django はトランザクションを開始します。レスポンスが問題なく生成された場合は、Django はトランザクションをコミットします。もしビューが例外を生成した場合は、Django はトランザクションをロールバックします。
ビューのコード (通常は atomic()
コンテキストマネージャー) の中で、セーブポイントを使ったサブトランザクションを扱うことができます。ただし、ビューの最後では、すべての変更がコミットされるか、何もコミットされないかのどちらかです。
警告
このトランザクションモデルは簡潔ではありますが、トラフィックが増加するときには非効率となります。全てのビューでトランザクションを扱うとオーバーヘッドが増加します。パフォーマンスへの影響は、アプリケーションのクエリパターンと、どれだけうまくデータベースがロッキングを扱うかに依存します。
リクエストごとのトランザクションとストリーミングレスポンス
ビューが StreamingHttpResponse
を返すとき、レスポンスの内容読み出しが内容を生成するためのコードを実行することがあります。ビューはすでに返されているので、このコードはトランザクションの外で走ります。
一般的に言って、ストリーミングレスポンスが生成されている間はデータベースに書き込みすることは推奨されません。レスポンスを送信開始した後にエラーを扱う適切な方法が存在しないからです。
In practice, this feature wraps every view function in the atomic()
decorator described below.
あなたのビューの実行だけがトランザクションで閉じられることに注意してください。ミドルウェアはトランザクションの外で実行し、テンプレートレスポンスのレンダリングを実行します。
ATOMIC_REQUESTS
が有効な場合、ビューがトランザクション内で実行するのを防ぐことができます。
non_atomic_requests
(using=None)[ソース]¶このデコレータは、与えられたビューのために ATOMIC_REQUESTS
を無効化します。
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()
ビュー自身に適用される場合にのみ作動します。
Django は、データベーストランザクションをコントロールするための一つの API を提供しています。
atomic
(using=None, savepoint=True, durable=False)[ソース]¶Atomicityは、データベーストランザクションの定義プロパティです。atomic
は、データベースの atomicity が保証されるコードブロックを作成することを可能にします。 コードブロックが正常に完了すると、変更はデータベースにコミットされます。 例外がある場合、変更はロールバックされます。
atomic
のブロックはネスト可能です。この場合、内側のブロックが成功裏に完了しても、その効果は後で外側のブロックで例外が発生した場合にロールバック可能となっています。
It is sometimes useful to ensure an atomic
block is always the
outermost atomic
block, ensuring that any database changes are
committed when the block is exited without errors. This is known as
durability and can be achieved by setting durable=True
. If the
atomic
block is nested within another it raises a RuntimeError
.
atomic
は、decorator として使用することも:
from django.db import transaction
@transaction.atomic
def viewfunc(request):
# This code executes inside a transaction.
do_stuff()
context manager として使用することも可能です:
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()
try/except ブロック内で atomic
をラップすると、integrity error を自然な形で処理できます:
from django.db import IntegrityError, transaction
@transaction.atomic
def viewfunc(request):
create_parent()
try:
with transaction.atomic():
generate_relationships()
except IntegrityError:
handle_exception()
add_children()
In this example, even if generate_relationships()
causes a database
error by breaking an integrity constraint, you can execute queries in
add_children()
, and the changes from create_parent()
are still
there and bound to the same transaction. 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.
atomic
の内部で例外をキャッチしない!
atomic
ブロックの処理を終える際、Django は通常の終了なのか例外を伴うのかを見て、コミットするかロールバックするかを決定します。atomic
ブロック内で例外をキャッチしてハンドする場合、Django に対して問題が発生したことを隠すことになります。これは予期しない動作の原因となります。
この挙動は、DatabaseError
およびそのサブクラス (IntegrityError
など) に対する懸念となります。こうしたエラーが起きた場合、Djangoは atomic
ブロックの最後にロー路バックを実施します。ロールバックが発生する前にデータベースクエリを実行しようとすると、Django は TransactionManagementError
を送出します。ORM 関連のシグナルハンドラが例外を送出した際にも同様の挙動となります。
データベースエラーをキャッチする正しい方法は、上記で示した通りの atomic
ブロックです。必要に応じてさらに atomic
ブロックを追加してください。このパターンはもう一つのメリットがあります: 例外が発生した場合にどの操作がロールバックされるかを限定できることです。
素の SQL クエリによって発生した例外をキャッチした場合、Django の挙動は決まっておらず、データベースに依存します。
You may need to manually revert model state when rolling back a transaction.
The values of a model's fields won't be reverted when a transaction rollback happens. This could lead to an inconsistent model state unless you manually restore the original field values.
For example, given MyModel
with an active
field, this snippet
ensures that the if obj.active
check at the end uses the correct
value if updating active
to True
fails in the transaction:
from django.db import DatabaseError, transaction
obj = MyModel(active=False)
obj.active = True
try:
with transaction.atomic():
obj.save()
except DatabaseError:
obj.active = False
if obj.active:
...
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.
atomic
takes a using
argument which should be the name of a
database. If this argument isn't provided, Django uses the "default"
database.
Under the hood, Django's transaction management code:
atomic
block;atomic
block;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.
You may use atomic
when autocommit is turned off. It will only use
savepoints, even for the outermost block.
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 if you're using atomic()
in long-running
processes, outside of Django's request / response cycle.
警告
django.test.TestCase
disables the durability check to allow
testing durable atomic blocks in a transaction for performance reasons. Use
django.test.TransactionTestCase
for testing durability.
The durable
argument was added.
In the SQL standards, each SQL query starts a transaction, unless one is already active. Such transactions must then be explicitly 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 and no transaction is active, each SQL query gets wrapped in its own transaction. In other words, not only does each such query start a 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 be initially turned off. Django overrides this default and turns autocommit on.
To avoid this, you can deactivate the transaction management, but it isn't recommended.
You can totally disable Django's transaction management for a given database
by setting 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.
Sometimes you need to perform an action related to the current database transaction, but only if the transaction successfully commits. Examples might include a Celery task, an email notification, or a cache invalidation.
Django provides the on_commit()
function to register callback functions
that should be executed after a transaction is successfully committed:
Pass any function (that takes no arguments) to on_commit()
:
from django.db import transaction
def do_something():
pass # send a mail, invalidate a cache, fire off a Celery task, etc.
transaction.on_commit(do_something)
You can also wrap your function in a lambda:
transaction.on_commit(lambda: some_celery_task.delay('arg1'))
The function you pass in will be called immediately after a hypothetical
database write made where on_commit()
is called would be successfully
committed.
If you call on_commit()
while there isn't an active transaction, the
callback will be executed immediately.
If that hypothetical database write is instead rolled back (typically when an
unhandled exception is raised in an atomic()
block), your function will
be discarded and never called.
Savepoints (i.e. nested atomic()
blocks) are handled correctly. That is,
an on_commit()
callable registered after a savepoint (in a nested
atomic()
block) will be called after the outer transaction is committed,
but not if a rollback to that savepoint or any previous savepoint occurred
during the transaction:
with transaction.atomic(): # Outer atomic, start a new transaction
transaction.on_commit(foo)
with transaction.atomic(): # Inner atomic block, create a savepoint
transaction.on_commit(bar)
# foo() and then bar() will be called when leaving the outermost block
On the other hand, when a savepoint is rolled back (due to an exception being raised), the inner callable will not be called:
with transaction.atomic(): # Outer atomic, start a new transaction
transaction.on_commit(foo)
try:
with transaction.atomic(): # Inner atomic block, create a savepoint
transaction.on_commit(bar)
raise SomeError() # Raising an exception - abort the savepoint
except SomeError:
pass
# foo() will be called, but not bar()
On-commit functions for a given transaction are executed in the order they were registered.
If one on-commit function within a given transaction raises an uncaught
exception, no later registered functions in that same transaction will run.
This is the same behavior as if you'd executed the functions sequentially
yourself without on_commit()
.
Your callbacks are executed after a successful commit, so a failure in a
callback will not cause the transaction to roll back. They are executed
conditionally upon the success of the transaction, but they are not part of
the transaction. For the intended use cases (mail notifications, Celery tasks,
etc.), this should be fine. If it's not (if your follow-up action is so
critical that its failure should mean the failure of the transaction itself),
then you don't want to use the on_commit()
hook. Instead, you may want
two-phase commit such as the psycopg Two-Phase Commit protocol support and the optional Two-Phase Commit Extensions in the
Python DB-API specification.
Callbacks are not run until autocommit is restored on the connection following the commit (because otherwise any queries done in a callback would open an implicit transaction, preventing the connection from going back into autocommit mode).
When in autocommit mode and outside of an atomic()
block, the function
will run immediately, not on commit.
On-commit functions only work with autocommit mode
and the atomic()
(or ATOMIC_REQUESTS
) transaction API. Calling on_commit()
when
autocommit is disabled and you are not within an atomic block will result in an
error.
Django's TestCase
class wraps each test in a transaction
and rolls back that transaction after each test, in order to provide test
isolation. This means that no transaction is ever actually committed, thus your
on_commit()
callbacks will never be run.
You can overcome this limitation by using
TestCase.captureOnCommitCallbacks()
. This captures your
on_commit()
callbacks in a list, allowing you to make assertions on them,
or emulate the transaction committing by calling them.
Another way to overcome the limitation is to use
TransactionTestCase
instead of
TestCase
. This will mean your transactions are committed,
and the callbacks will run. However
TransactionTestCase
flushes the database between tests,
which is significantly slower than TestCase
's isolation.
A rollback hook is harder to implement robustly than a commit hook, since a variety of things can cause an implicit rollback.
For instance, if your database connection is dropped because your process was killed without a chance to shut down gracefully, your rollback hook will never run.
But there is a solution: instead of doing something during the atomic block
(transaction) and then undoing it if the transaction fails, use
on_commit()
to delay doing it in the first place until after the
transaction succeeds. It's a lot easier to undo something you never did in the
first place!
警告
Always prefer 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.
Django provides an API in the django.db.transaction
module to manage the
autocommit state of each database connection.
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
commit()
or a rollback()
, before turning autocommit back on.
Django will refuse to turn autocommit off when an atomic()
block is
active, because that would break atomicity.
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 set_autocommit()
.
Once you're in a transaction, you can choose either to apply the changes
you've performed until this point with commit()
, or to cancel them with
rollback()
. These functions are defined in django.db.transaction
.
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 atomic()
block is
active, because that would break atomicity.
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, 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.
Savepoints aren't especially useful if you are using autocommit, the default
behavior of Django. However, once you open a transaction with 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()
.
When the atomic()
decorator is nested, it creates a savepoint to allow
partial commit or rollback. You're strongly encouraged to use atomic()
rather than the functions described below, but they're still part of the
public API, and there's no plan to deprecate them.
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.
Savepoints are controlled by three functions in django.db.transaction
:
savepoint
(using=None)[ソース]¶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
).
savepoint_commit
(sid, using=None)[ソース]¶Releases savepoint sid
. The changes performed since the savepoint was
created become part of the transaction.
These functions do nothing if savepoints aren't supported or if the database is in autocommit mode.
In addition, there's a utility function:
The following example demonstrates the use of savepoints:
from django.db import transaction
# open a transaction
@transaction.atomic
def viewfunc(request):
a.save()
# transaction now contains a.save()
sid = transaction.savepoint()
b.save()
# transaction now contains a.save() and b.save()
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()
Savepoints may be used to recover from a database error by performing a partial
rollback. If you're doing this inside an 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.
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.
While SQLite supports savepoints, a flaw in the design of the sqlite3
module makes them hardly usable.
When autocommit is enabled, savepoints don't make sense. When it's disabled,
sqlite3
commits implicitly before savepoint statements. (In fact, it
commits before any statement other than SELECT
, INSERT
, UPDATE
,
DELETE
and REPLACE
.) This bug has two consequences:
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.
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.
注釈
This section is relevant only if you're implementing your own transaction
management. This problem cannot occur in Django's default mode and
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". While the basic 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
force_insert/force_update flag, or invoking custom SQL.
There are several ways to recover from this sort of error.
The first option is to roll back the entire transaction. For example:
a.save() # Succeeds, but may be undone by transaction rollback
try:
b.save() # Could throw exception
except IntegrityError:
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.
You can use 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:
a.save() # Succeeds, and never undone by savepoint rollback
sid = transaction.savepoint()
try:
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.
2022年6月01日