Transactions in Django
March 05, 2006 |
co.mments
Nice. Django gets transactions. Txn support is called in via Python decorators, so it's nice and clean on the code. Java folks used to XDoclet and attributes will appreciate the idiom:
There are 3 decorators - autocommit, commit_on_success, commit_manually. You decorate on the view code:
from django.db.transaction import autocommit
@transaction.autocommit
def doSomething(request):
# do work
@transaction.commit_on_success
def doSomethingElse(request):
# do work
@transaction.commit_manually
def doSomethingElseAgain(request):
try:
# do work
except:
transaction.rollback()
else:
transaction.commit()
To back out of the transaction, the view code needs to throw an exception, otherwise Django will complete. If you have view code which swallows exceptions, you might want to clean that up in preparation for this feature.
If you think you're hard enough, you can set DISABLE_TRANSACTION_MANAGEMENT=True in your settings.py file.
via Peter Harkins, who says the magic-removal branch is getting folded in in a month or so. Outstanding.
March 5, 2006 03:52 PM
Comments
Post a comment
Trackback Pings
TrackBack URL for this entry:
http://www.dehora.net/mt/mt-tb.cgi/1768