xact
is a recipe for handling transactions sensibly in Django applications on PostgreSQL.Note: As of Django 1.6, the functionality of xact
will be merged into the Django core as the atomic
decorator.
Code that uses xact
should be able to be migrated to atomic
with just a search-and-replace. atomic
works on
databases other than PostgreSQL, is thread-safe, and has other nice features; switch to it when you can!
The goals are:
The bits of the recipe are:
xact()
decorator / context manager below, using it like you would the commit_on_success()
decorator.The quick reasons behind each step:
xact()
decorator will set up the connection so that a transaction is started in the relevant block, which is what we want for database-modifying operations.This recipe a few other nice features:
xact()
operates like commit_on_success()
, in that it will issue a rollback if an exception escapes from the block or function it is wrapping.xact()
ignores the dirty flag on the Django connection. Since we're deliberately wrapping stuff that modifies the database with it, the chance of it being dirty is near 100%, and a commit on a transaction that did not modify the database is no more expensive in PostgreSQL than a rollback. It also means you can do raw SQL inside an xact()
block without the foot-gun of forgetting to call set_dirty
.with
statement.xact()
can be nested, giving us nested transactions! If it sees that there is already a transaction open when it starts a new block, it will use a savepoint to set up a nested transaction block. (PostgreSQL does not have nested transactions as such, but you can use savepoints to get 99.9% of the way there.)xact()
works around an outstanding bug in Django's transaction handling on psycopg2.xact()
also supports the using
parameter for multiple databases.
Rollback
is a supplied exception class designed for situations in which you want to trigger an exception to cause a rollback, but do not want that exception
to propagate beyond the scope of the xact
block. xact
will perform the
rollback, then swallow the exception.
Of course, a few caveats:
xact()
requires the postgresql_psycopg2
backend, and PostgreSQL 8.2 or higher. It's possible it can be hacked to work on other backends that support savepoints.xact()
works just the way you want if it is nested inside a commit_on_success()
block (it will properly create a savepoint insted of a new transaction). However, a commit_on_success()
block nested inside of xact()
will commit or rollback the entire transaction, somewhat defeating the outer xact()
. To the extent possible, use only xact()
in code you write.xact()
block; otherwise, the automatic rollback will be defeated. Allow the exception to escape the xact()
block, and then catch it. (Of course, if the intention is to always commit and to defeat the rollback, by all means catch the exception inside the block.)To use, just drop the source (one class definition, one function) into a file somewhere in your Django project (such as the omni-present utils
application every Django project seems to have), and include it.
Examples:
from utils.transaction import xact
@xact
def my_view_function1(request):
# Everything here will be in a transaction.
# It'll roll back if an exception escapes, commits otherwise.
def my_view_function2(request):
# This stuff won't be in a transaction, so don't modify the database here.
with xact():
# This stuff will be, and will commit on normal completion, roll back on a exception
def my_view_function3(request):
with xact():
# Modify the database here (let's call it "part 1").
try:
with xact():
# Let's call this "part 2."
# This stuff will be in its own savepoint, and can commit or
# roll back without losing the whole transaction.
except:
# Part 2 will be rolled back, but part 1 will still be available to
# be committed or rolled back. Of course, if an exception
# inside the "part 2" block is not caught, both part 2 and
# part 1 will be rolled back.