数据库事务

Django 提供多种方式控制数据库事务。

管理数据库事务

Django 默认的事务行为

Django 默认的事务行为是自动提交。除非事务正在执行,每个查询将会马上自动提交到数据库。 详见.

Django 自动使用事务或还原点,以确保需多次查询的 ORM 操作的一致性,特别是 delete()update() 操作.

由于性能原因,Django 的 TestCase 类同样将每个测试用事务封装起来。

连结事务与 HTTP 请求

在 Web 里,处理事务比较常用的方式是将每个请求封装在一个事务中。 在你想启用该行为的数据库中,把配置中的参数 ATOMIC_REQUESTS 设置为 True

它是这样工作的:在调用试图方法前,Django 先生成一个事务。如果响应能正常生成,Django 会提交该事务。而如果视图出现异常,Django 则会回滚该事务。

你可以在你的视图代码中使用还原点执行子事务,一般会使用 :func:`atomic 上下文管理器。但是,在视图结束时,要么所有的更改都被提交,要么所有的更改都不被提交。

警告

While the simplicity of this transaction model is appealing, it also makes itinefficient when traffic increases. Opening a transaction for every view hassome overhead. The impact on performance depends on the query patterns of yourapplication and on how well your database handles locking.

Per-request transactions and streaming responses

When a view returns a StreamingHttpResponse, readingthe contents of the response will often execute code to generate thecontent. Since the view has already returned, such code runs outside ofthe transaction.

Generally speaking, it isn't advisable to write to the database whilegenerating a streaming response, since there's no sensible way to handleerrors after starting to send the response.

In practice, this feature simply wraps every view function in the atomic()decorator described below.

Note that only the execution of your view is enclosed in the transactions.Middleware runs outside of the transaction, and so does the rendering oftemplate responses.

When ATOMIC_REQUESTS is enabled, it'sstill possible to prevent views from running in a transaction.

  1. from django.db import transaction
  2.  
  3. @transaction.non_atomic_requests
  4. def my_view(request):
  5. do_stuff()
  6.  
  7. @transaction.non_atomic_requests(using='other')
  8. def my_other_view(request):
  9. do_stuff_on_the_other_database()

It only works if it's applied to the view itself.

Controlling transactions explicitly

Django provides a single API to control database transactions.

  • atomic(using=None, savepoint=True)[源代码]
  • Atomicity is the defining property of database transactions. atomicallows us to create a block of code within which the atomicity on thedatabase is guaranteed. If the block of code is successfully completed, thechanges are committed to the database. If there is an exception, thechanges are rolled back.

atomic blocks can be nested. In this case, when an inner blockcompletes successfully, its effects can still be rolled back if anexception is raised in the outer block at a later point.

atomic is usable both as a decorator:

  1. from django.db import transaction
  2.  
  3. @transaction.atomic
  4. def viewfunc(request):
  5. # This code executes inside a transaction.
  6. do_stuff()

and as a context manager:

  1. from django.db import transaction
  2.  
  3. def viewfunc(request):
  4. # This code executes in autocommit mode (Django's default).
  5. do_stuff()
  6.  
  7. with transaction.atomic():
  8. # This code executes inside a transaction.
  9. do_more_stuff()

Wrapping atomic in a try/except block allows for natural handling ofintegrity errors:

  1. from django.db import IntegrityError, transaction
  2.  
  3. @transaction.atomic
  4. def viewfunc(request):
  5. create_parent()
  6.  
  7. try:
  8. with transaction.atomic():
  9. generate_relationships()
  10. except IntegrityError:
  11. handle_exception()
  12.  
  13. add_children()

In this example, even if generate_relationships() causes a databaseerror by breaking an integrity constraint, you can execute queries inadd_children(), and the changes from create_parent() are stillthere. Note that any operations attempted in generate_relationships()will already have been rolled back safely when handle_exception() iscalled, so the exception handler can also operate on the database ifnecessary.

Avoid catching exceptions inside atomic!

When exiting an atomic block, Django looks at whether it's exitednormally or with an exception to determine whether to commit or rollback. If you catch and handle exceptions inside an atomic block,you may hide from Django the fact that a problem has happened. Thiscan result in unexpected behavior.

This is mostly a concern for DatabaseError and itssubclasses such as IntegrityError. After such anerror, the transaction is broken and Django will perform a rollback atthe end of the atomic block. If you attempt to run databasequeries before the rollback happens, Django will raise aTransactionManagementError. You mayalso encounter this behavior when an ORM-related signal handler raisesan exception.

The correct way to catch database errors is around an atomic blockas shown above. If necessary, add an extra atomic block for thispurpose. This pattern has another advantage: it delimits explicitlywhich operations will be rolled back if an exception occurs.

If you catch exceptions raised by raw SQL queries, Django's behavioris unspecified and database-dependent.

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 transactionrollback happens. This could lead to an inconsistent model state unlessyou manually restore the original field values.

For example, given MyModel with an active field, this snippetensures that the if obj.active check at the end uses the correctvalue if updating active to True fails in the transaction:

  1. from django.db import DatabaseError, transaction
  2.  
  3. obj = MyModel(active=False)
  4. obj.active = True
  5. try:
  6. with transaction.atomic():
  7. obj.save()
  8. except DatabaseError:
  9. obj.active = False
  10.  
  11. if obj.active:
  12. ...

In order to guarantee atomicity, atomic disables some APIs. Attemptingto commit, roll back, or change the autocommit state of the databaseconnection within an atomic block will raise an exception.

atomic takes a using argument which should be the name of adatabase. If this argument isn't provided, Django uses the "default"database.

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.
    You can disable the creation of savepoints for inner blocks by setting thesavepoint argument to False. If an exception occurs, Django willperform the rollback when exiting the first parent block with a savepointif there is one, and the outermost block otherwise. Atomicity is stillguaranteed by the outer transaction. This option should only be used ifthe overhead of savepoints is noticeable. It has the drawback of breakingthe error handling described above.

You may use atomic when autocommit is turned off. It will only usesavepoints, even for the outermost block.

Performance considerations

Open transactions have a performance cost for your database server. Tominimize this overhead, keep your transactions as short as possible. Thisis especially important if you're using atomic() in long-runningprocesses, outside of Django's request / response cycle.

Autocommit

Why Django uses autocommit

In the SQL standards, each SQL query starts a transaction, unless one isalready active. Such transactions must then be explicitly committed or rolledback.

This isn't always convenient for application developers. To alleviate thisproblem, most databases provide an autocommit mode. When autocommit is turnedon and no transaction is active, each SQL query gets wrapped in its owntransaction. In other words, not only does each such query start atransaction, but the transaction also gets automatically committed or rolledback, depending on whether the query succeeded.

PEP 249, the Python Database API Specification v2.0, requires autocommit tobe initially turned off. Django overrides this default and turns autocommiton.

为了避免这种情况,你可以参考 deactivate the transaction management<deactivate-transaction-management> ,但并不推荐这样做。

停用事务管理

You can totally disable Django's transaction management for a given databaseby setting AUTOCOMMIT to False in itsconfiguration. If you do this, Django won't enable autocommit, and won'tperform any commits. You'll get the regular behavior of the underlyingdatabase library.

This requires you to commit explicitly every transaction, even those startedby Django or by third-party libraries. Thus, this is best used in situationswhere you want to run your own transaction-controlling middleware or dosomething really strange.

提交后

Sometimes you need to perform an action related to the current databasetransaction, but only if the transaction successfully commits. Examples mightinclude a Celery task, an email notification, or a cache invalidation.

Django provides the on_commit() function to register callback functionsthat should be executed after a transaction is successfully committed:

  1. from django.db import transaction
  2.  
  3. def do_something():
  4. pass # send a mail, invalidate a cache, fire off a Celery task, etc.
  5.  
  6. transaction.on_commit(do_something)

你也可以使用 lambda:: 包装函数

  1. transaction.on_commit(lambda: some_celery_task.delay('arg1'))

The function you pass in will be called immediately after a hypotheticaldatabase write made where on_commit() is called would be successfullycommitted.

无任何活动事务时调用 on_commit() ,则回调函数会立即执行。

If that hypothetical database write is instead rolled back (typically when anunhandled exception is raised in an atomic() block), your function willbe discarded and never called.

Savepoints

Savepoints (i.e. nested atomic() blocks) are handled correctly. That is,an on_commit() callable registered after a savepoint (in a nestedatomic() block) will be called after the outer transaction is committed,but not if a rollback to that savepoint or any previous savepoint occurredduring the transaction:

  1. with transaction.atomic(): # Outer atomic, start a new transaction
  2. transaction.on_commit(foo)
  3.  
  4. with transaction.atomic(): # Inner atomic block, create a savepoint
  5. transaction.on_commit(bar)
  6.  
  7. # 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 beingraised), the inner callable will not be called:

  1. with transaction.atomic(): # Outer atomic, start a new transaction
  2. transaction.on_commit(foo)
  3.  
  4. try:
  5. with transaction.atomic(): # Inner atomic block, create a savepoint
  6. transaction.on_commit(bar)
  7. raise SomeError() # Raising an exception - abort the savepoint
  8. except SomeError:
  9. pass
  10.  
  11. # foo() will be called, but not bar()

执行顺序

事务提交后的的回调函数执行顺序与当初注册时的顺序一致。

异常处理

If one on-commit function within a given transaction raises an uncaughtexception, no later registered functions in that same transaction will run.This is, of course, the same behavior as if you'd executed the functionssequentially yourself without on_commit().

Timing of execution

Your callbacks are executed after a successful commit, so a failure in acallback will not cause the transaction to roll back. They are executedconditionally upon the success of the transaction, but they are not part ofthe 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 socritical 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 wanttwo-phase commit such as the psycopg Two-Phase Commit protocol supportand the optional Two-Phase Commit Extensions in the Python DB-APIspecification.

Callbacks are not run until autocommit is restored on the connection followingthe commit (because otherwise any queries done in a callback would open animplicit transaction, preventing the connection from going back into autocommitmode).

When in autocommit mode and outside of an atomic() block, the functionwill run immediately, not on commit.

On-commit functions only work with autocommit modeand the atomic() (or ATOMIC_REQUESTS) transaction API. Calling on_commit() whenautocommit is disabled and you are not within an atomic block will result in anerror.

Use in tests

Django's TestCase class wraps each test in a transactionand rolls back that transaction after each test, in order to provide testisolation. This means that no transaction is ever actually committed, thus youron_commit() callbacks will never be run. If you need to test the resultsof an on_commit() callback, use aTransactionTestCase instead.

为什么没有事务回滚钩子?

事务回滚钩子相比事务提交钩子更难实现,因为各种各样的情况都可能造成隐式回滚。

For instance, if your database connection is dropped because your process waskilled without a chance to shut down gracefully, your rollback hook will neverrun.

解决方法很简单,与其在执行事务时(原子操作)进行某项操作,当事务执行失败后再取消这项操作,不如使用 on_commit() 来延迟该项操作,直到事务成功后再进行操作。毕竟事务成功后你才能确保之后的操作是有意义的。

Low-level APIs

警告

Always prefer atomic() if possible at all. It accounts for theidiosyncrasies of each database and prevents invalid operations.

The low level APIs are only useful if you're implementing your owntransaction management.

Autocommit

Django provides a straightforward API in the django.db.transactionmodule to manage the autocommit state of each database connection.

  • getautocommit(_using=None)[源代码]
  • setautocommit(_autocommit, using=None)[源代码]
  • 这些函数使接受一个 using 参数表示所要操作的数据库。如果未提供,则 Django 使用 "default" 数据库。

自动提交默认为开启,如果你将它关闭,自己承担后果。

一旦你关闭了自动提交, Django 将无法帮助你,数据库将会按照你使用的数据库适配器的默认行为进行操作。虽然适配器的标准经过了 PEP 249 详细规定,但不同适配器的实现方式并不总是一致的。你需要谨慎地查看你所使用的适配器的文档。

在关闭自动提交之前,你必须确保当前没有活动的事务,通常你可以执行 commit() 或者 rollback() 函数以达到该条件。

当一个原子 atomic() 事务处于活动状态时, Django 将会拒绝关闭自动提交的请求,因为这样会破坏原子性。

事务

事务是指具有原子性的一系列数据库操作。即使你的程序崩溃,数据库也会确保这些操作要么全部完成要么全部都未执行。

Django doesn't provide an API to start a transaction. The expected way tostart a transaction is to disable autocommit with set_autocommit().

Once you're in a transaction, you can choose either to apply the changesyou've performed until this point with commit(), or to cancel them withrollback(). These functions are defined in django.db.transaction.

  • commit(using=None)[源代码]
  • rollback(using=None)[源代码]
  • 这些函数使接受一个 using 参数表示所要操作的数据库。如果未提供,则 Django 使用 "default" 数据库。

当一个原子 atomic() 事务处于活动状态时, Django 将会拒绝进行事务提交或者事务回滚,因为这样会破坏原子性。

Savepoints

A savepoint is a marker within a transaction that enables you to roll backpart of a transaction, rather than the full transaction. Savepoints areavailable with the SQLite, PostgreSQL, Oracle, and MySQL (when using the InnoDBstorage engine) backends. Other backends provide the savepoint functions, butthey're empty operations — they don't actually do anything.

Savepoints aren't especially useful if you are using autocommit, the defaultbehavior of Django. However, once you open a transaction with atomic(),you build up a series of database operations awaiting a commit or rollback. Ifyou issue a rollback, the entire transaction is rolled back. Savepointsprovide the ability to perform a fine-grained rollback, rather than the fullrollback that would be performed by transaction.rollback().

When the atomic() decorator is nested, it creates a savepoint to allowpartial commit or rollback. You're strongly encouraged to use atomic()rather than the functions described below, but they're still part of thepublic API, and there's no plan to deprecate them.

Each of these functions takes a using argument which should be the name ofa database for which the behavior applies. If no using argument isprovided 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 isknown to be in a "good" state. Returns the savepoint ID (sid).

  • savepointcommit(_sid, using=None)[源代码]

  • Releases savepoint sid. The changes performed since the savepoint wascreated become part of the transaction.

  • savepointrollback(_sid, using=None)[源代码]

  • Rolls back the transaction to savepoint sid.

These functions do nothing if savepoints aren't supported or if the databaseis in autocommit mode.

In addition, there's a utility function:

  • cleansavepoints(_using=None)[源代码]
  • Resets the counter used to generate unique savepoint IDs.

The following example demonstrates the use of savepoints:

  1. from django.db import transaction
  2.  
  3. # open a transaction
  4. @transaction.atomic
  5. def viewfunc(request):
  6.  
  7. a.save()
  8. # transaction now contains a.save()
  9.  
  10. sid = transaction.savepoint()
  11.  
  12. b.save()
  13. # transaction now contains a.save() and b.save()
  14.  
  15. if want_to_keep_b:
  16. transaction.savepoint_commit(sid)
  17. # open transaction still contains a.save() and b.save()
  18. else:
  19. transaction.savepoint_rollback(sid)
  20. # open transaction now contains only a.save()

Savepoints may be used to recover from a database error by performing a partialrollback. If you're doing this inside an atomic() block, the entire blockwill still be rolled back, because it doesn't know you've handled the situationat a lower level! To prevent this, you can control the rollback behavior withthe following functions.

  • getrollback(_using=None)[源代码]
  • setrollback(_rollback, using=None)[源代码]
  • Setting the rollback flag to True forces a rollback when exiting theinnermost atomic block. This may be useful to trigger a rollback withoutraising an exception.

Setting it to False prevents such a rollback. Before doing that, make sureyou've rolled back the transaction to a known-good savepoint within the currentatomic block! Otherwise you're breaking atomicity and data corruption mayoccur.

Database-specific notes

Savepoints in SQLite

While SQLite supports savepoints, a flaw in the design of the sqlite3module 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, itcommits before any statement other than SELECT, INSERT, UPDATE,DELETE and REPLACE.) This bug has two consequences:

  • The low level APIs for savepoints are only usable inside a transaction ie.inside an atomic() block.
  • It's impossible to use atomic() when autocommit is turned off.

Transactions in MySQL

If you're using MySQL, your tables may or may not support transactions; itdepends on your MySQL version and the table types you're using. (By"table types," we mean something like "InnoDB" or "MyISAM".) MySQL transactionpeculiarities are outside the scope of this article, but the MySQL site hasinformation on MySQL transactions.

If your MySQL setup does not support transactions, then Django will alwaysfunction in autocommit mode: statements will be executed and committed as soonas they're called. If your MySQL setup does support transactions, Djangowill handle transactions as explained in this document.

Handling exceptions within PostgreSQL transactions

注解

This section is relevant only if you're implementing your own transactionmanagement. This problem cannot occur in Django's default mode andatomic() handles it automatically.

Inside a transaction, when a call to a PostgreSQL cursor raises an exception(typically IntegrityError), all subsequent SQL in the same transactionwill fail with the error "current transaction is aborted, queries ignoreduntil end of transaction block". While simple use of save() is unlikelyto raise an exception in PostgreSQL, there are more advanced usage patternswhich might, such as saving objects with unique fields, saving using theforce_insert/force_update flag, or invoking custom SQL.

There are several ways to recover from this sort of error.

Transaction rollback

The first option is to roll back the entire transaction. For example:

  1. a.save() # Succeeds, but may be undone by transaction rollback
  2. try:
  3. b.save() # Could throw exception
  4. except IntegrityError:
  5. transaction.rollback()
  6. c.save() # Succeeds, but a.save() may have been undone

Calling transaction.rollback() rolls back the entire transaction. Anyuncommitted database operations will be lost. In this example, the changesmade by a.save() would be lost, even though that operation raised no erroritself.

Savepoint rollback

You can use savepoints to controlthe extent of a rollback. Before performing a database operation that couldfail, you can set or update the savepoint; that way, if the operation fails,you can roll back the single offending operation, rather than the entiretransaction. For example:

  1. a.save() # Succeeds, and never undone by savepoint rollback
  2. sid = transaction.savepoint()
  3. try:
  4. b.save() # Could throw exception
  5. transaction.savepoint_commit(sid)
  6. except IntegrityError:
  7. transaction.savepoint_rollback(sid)
  8. c.save() # Succeeds, and a.save() is never undone

In this example, a.save() will not be undone in the case whereb.save() raises an exception.