Changeset 2793
- Timestamp:
- 04/29/06 13:53:03 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/docs/transactions.txt
r2714 r2793 8 8 ===================================== 9 9 10 The default behavior of Django is to commit on special model functions. If you 11 call ``model.save()`` or ``model.delete()``, that change will be committed immediately. 10 Django's default behavior is to commit automatically when any built-in, 11 data-altering model function is called. For example, if you call 12 ``model.save()`` or ``model.delete()``, the change will be committed 13 immediately. 12 14 13 This is much like the auto-commit setting for most databases : as soon as you15 This is much like the auto-commit setting for most databases. As soon as you 14 16 perform an action that needs to write to the database, Django produces the 15 insert/update/delete statements and then does the commit. There is no implicit 16 rollback in Django.17 ``INSERT``/``UPDATE``/``DELETE`` statements and then does the ``COMMIT``. 18 There's no implicit ``ROLLBACK``. 17 19 18 20 Tying transactions to HTTP requests 19 21 =================================== 20 22 21 A useful way to handle transactions is to tie them to the request and response 22 phases.23 The recommended way to handle transactions in Web requests is to tie them to 24 the request and response phases via Django's ``TransactionMiddleware``. 23 25 24 When a request starts, you start a transaction. If the response is produced 25 without problems, any transactions are committed. If the view function produces 26 and exception, a rollback happens. This is one of the more intuitive ways to 27 handle transactions. To activate this feature, just add the 28 ``TransactionMiddleware`` middleware to your stack:: 26 It works like this: When a request starts, Django starts a transaction. If the 27 response is produced without problems, Django commits any pending transactions. 28 If the view function produces an exception, Django rolls back any pending 29 transactions. 30 31 To activate this feature, just add the ``TransactionMiddleware`` middleware to 32 your ``MIDDLEWARE_CLASSES`` setting:: 29 33 30 34 MIDDLEWARE_CLASSES = ( … … 35 39 ) 36 40 37 The order is quite important : the transaction middleware will be relevant not38 only for the view functions called, but for all middleware modules that come 39 after it. So if you use the session middleware after the transaction middleware, 40 sessioncreation will be part of the transaction.41 The order is quite important. The transaction middleware applies not only to 42 view functions, but also for all middleware modules that come after it. So if 43 you use the session middleware after the transaction middleware, session 44 creation will be part of the transaction. 41 45 42 The cache middleware isn't affected, as it uses its own database cursor (which 43 is mapped to its own database connection internally), and only the44 database-based cache is affected.46 An exception is ``CacheMiddleware``, which is never affected. The cache 47 middleware uses its own database cursor (which is mapped to its own database 48 connection internally). 45 49 46 50 Controlling transaction management in views 47 51 =========================================== 48 52 49 For m any people, implicit request-based transactions will work wonderfully.50 However, if you need to control the way that transactions are managed, 51 you can use decorators that you can apply to a function to change the way 52 transactions are handled.53 For most people, implicit request-based transactions work wonderfully. However, 54 if you need more fine-grained control over how transactions are managed, you 55 can use Python decorators to change the way transactions are handled by a 56 particular view function. 53 57 54 58 .. note:: … … 57 61 decorators can be applied to non-view functions as well. 58 62 59 `` autocommit``60 -------------- 63 ``django.db.transaction.autocommit`` 64 ------------------------------------ 61 65 62 You can use the ``autocommit`` decorator to switch a view function to the 63 default commit behavior of Django, regardless of the global setting. Just use 64 the decorator like this:: 66 Use the ``autocommit`` decorator to switch a view function to Django's default 67 commit behavior, regardless of the global transaction setting. 68 69 Example:: 65 70 66 71 from django.db import transaction … … 70 75 .... 71 76 72 Within ``viewfunc ``transactions will be committed as soon as you call77 Within ``viewfunc()``, transactions will be committed as soon as you call 73 78 ``model.save()``, ``model.delete()``, or any other function that writes to the 74 79 database. 75 80 76 `` commit_on_success``77 --------------------- 81 ``django.db.transaction.commit_on_success`` 82 ------------------------------------------- 78 83 79 You can use the ``commit_on_success`` decorator to use a single transaction for84 Use the ``commit_on_success`` decorator to use a single transaction for 80 85 all the work done in a function:: 81 86 … … 86 91 .... 87 92 88 If the function returns successfully, then all work done will be committed. If an89 exception is raised beyond the function, however, the transaction will be rolled 90 back.93 If the function returns successfully, then Django will commit all work done 94 within the function at that point. If the function raises an exception, though, 95 Django will roll back the transaction. 91 96 92 `` commit_manually``93 ------------------- 97 ``django.db.transaction.commit_manually`` 98 ----------------------------------------- 94 99 95 Sometimes you need full control over your transactions. In that case, you can use the 96 ``commit_manually`` decorator, which tells Django you'll be managing the transaction 97 on your own. 100 Use the ``commit_manually`` decorator if you need full control over 101 transactions. It tells Django you'll be managing the transaction on your own. 98 102 99 If you don't commit or rollback and did change data (so that the current 100 transaction is marked as dirty), you'll get a ``TransactionManagementError`` 101 exception. 103 If your view changes data and doesn't ``commit()`` or ``rollback()``, Django 104 will raise a ``TransactionManagementError`` exception. 102 105 103 Manual transaction management looks like ::106 Manual transaction management looks like this:: 104 107 105 108 from django.db import transaction … … 122 125 ..admonition:: An important note to users of earlier Django releases: 123 126 124 The database ``connection.commit `` and ``connection.rollback`` functions125 ( also called ``db.commit`` and ``db.rollback`` in 0.91 and earlier), no126 longer exist and have been replaced by the ``transaction.commit`` and127 ``transaction.rollback `` commands.127 The database ``connection.commit()`` and ``connection.rollback()`` methods 128 (called ``db.commit()`` and ``db.rollback()`` in 0.91 and earlier) no longer 129 exist. They've been replaced by ``transaction.commit()`` and 130 ``transaction.rollback()``. 128 131 129 132 How to globally deactivate transaction management … … 133 136 ``DISABLE_TRANSACTION_MANAGEMENT`` to ``True`` in the Django settings file. 134 137 135 If you do this, there will be no management whatsoever. The middleware will no136 longer implicitly commit transactions, and you'll need to roll management 137 you rself. This even will require you to commit changes done by middleware138 somewhere else.138 If you do this, Django won't provide any automatic transaction management 139 whatsoever. Middleware will no longer implicitly commit transactions, and 140 you'll need to roll management yourself. This even requires you to commit 141 changes done by middleware somewhere else. 139 142 140 Thus, this is best used in situations where you want to run your own transaction141 controlling middleware or do something really strange. In almost all situations, 142 you'll be better off using the default behavior or the transaction middleware143 and only modify selected functions as needed.143 Thus, this is best used in situations where you want to run your own 144 transaction-controlling middleware or do something really strange. In almost 145 all situations, you'll be better off using the default behavior, or the 146 transaction middleware, and only modify selected functions as needed.
