Christian Brink
Christian Brink

Reputation: 663

How does Django determine whether my database supports transactions?

On my development machine I can use transaction.commit_on_success and transaction.commit_manually, but on my production machine the decorators are ignored and Django behaves as if my database doesn't support transactions. This happens both in my view functions and in trivial test functions I write in the Django shell:

>>> from django.db import transaction
>>> from myapp.models import *
>>> @transaction.commit_on_success
... def blah():
...   MyModel.objects.create(name="wazzup")
...   raise Exception
... 
>>> blah()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/opt/local/python-environments/myenv/lib/python2.6/site-packages/django/db/transaction.py", line 209, in inner
    return func(*args, **kwargs)
  File "<console>", line 4, in blah
Exception
>>> MyModel.objects.all()       
[<MyModel: wazzup>]

The same thing happens if I instantiate model objects and then call save on them rather than just calling create on the manager. commit_manually fails in the same way. In sum, it appears that autocommit is stuck in the on position.

But only on the production machine. Everything works fine on my MacBook Pro.

Both environments are using MySQL with all tables running on InnoDB. Django 1.4.3, Python 2.6.6.

I would appreciate any thoughts about what I might be doing wrong in settings.py, in my MySQL configuration, or anywhere else -- I am banging my head against the wall and am open to any suggestion at all.

Upvotes: 2

Views: 635

Answers (1)

Rune Kaagaard
Rune Kaagaard

Reputation: 6798

It's done like this:

from django.db import connection
if connection.features.supports_transactions:
    # We _do_ have transaction support.
else:
    # We _don't_ have transaction support. 

Upvotes: 1

Related Questions