
If the response returned with no exceptions, Django would commit the transaction but if your view function threw an error, ROLLBACK would be called. So every time you called something like model.save() or model.update(), Django generated the appropriate SQL statements and committed the transaction.Īlso in Django 1.5 and earlier, it was recommended that you used the TransactionMiddleware to bind transactions to HTTP requests. In Django 1.5 and earlier, Django basically ran with an open transaction and auto-committed that transaction when you wrote data to the database.

Django also has something to say about transaction management. Client libraries must mirror what happens within the database, but since they are not allowed to turn AUTOCOMMIT on by default, they simply wrap your SQL statements in a transaction, just like the database.However, according to PEP 249, this should not happen.SQL statements always have to run in a transaction, which the database generally opens for you via AUTOCOMMIT.This clearly conflicts with what’s happening within the database: While it may make for some slightly dry reading, an important take away is that PEP 249 states that the database AUTOCOMMIT should be OFF by default. That standard, DB API 2.0, is described in PEP 249.
#TRANSACTION MANAGEMENT HOW TO#
Such libraries follow a set of standards for how to access and query the databases. Then there’s the Python client libraries like sqlite3 and mysqldb, which allow Python programs to interface with the databases themselves. However, the take away here is that the AUTOCOMMIT setting applies an implicit commit after each statement. Of course you can manually call something like START_TRANSACTION which will temporarily suspend the AUTOCOMMIT until you call COMMIT_TRANSACTION or ROLLBACK. This AUTOCOMMIT wraps every statement in a transaction that is immediately committed if the statement succeeds. Most databases have an AUTOCOMMIT setting, which is usually set to True as a default. DatabasesĮvery statement in a database has to run in a transaction, even if the transaction includes only one statement. In order to fully answer this question, we must address how transactions are dealt with in the database, client libraries, and within Django. Remove ads What’s wrong with transaction management prior to Django 1.6? Likewise, when rolled back, all the statements get rolled back together.

In other words, all the SQL statements are executed and committed together. What’s right about transaction management in Django 1.6?Īnd then dealing with a detailed example:Īccording to SQL-92, “An SQL-transaction (sometimes simply called a “transaction”) is a sequence of executions of SQL-statements that is atomic with respect to recovery”.

What’s wrong with transaction management prior to Django 1.6?.And we will get to those in just a second. You really need to only know about a couple functions now. Fortunately, with Django 1.6 that all goes out the door. There were a plethora of decorators to work with, like commit_on_success, commit_manually, commit_unless_managed, rollback_unless_managed, enter_transaction_management, leave_transaction_management, just to name a few. In the past, the documentation provided quite a bit of depth, but understanding only came through building and experimenting. If you ever devoted much time to Django database transaction management, you know how confusing it can get.
