简体   繁体   中英

Django transaction.commit_on_success not rolling back transaction

I'm trying to use Django transactions on MySQL with the commit_on_success decorator . According to the documentation, "If the function raises an exception, though, Django will roll back the transaction." However, this doesn't seem to work for me:

>>> @transaction.commit_on_success
... def fails():
...     Site.objects.create(name="New Site", ip_address="127.0.0.1")
...     raise ValueError("oh noes!")
... 
>>> Site.objects.count()   
2
>>> fails()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/site-packages/django/db/transaction.py", line 240, in _commit_on_success
    res = func(*args, **kw)
  File "<stdin>", line 4, in fails
ValueError: oh noes!
>>> Site.objects.count()
3
>>>

I'm pretty sure that MySQL supports transactions; do I need to use a different table type or something?

From http://docs.djangoproject.com/en/dev/ref/databases/ :

"The default engine is MyISAM [1]. The main drawback of MyISAM is that it doesn't currently support transactions or foreign keys. On the plus side, it's currently the only engine that supports full-text indexing and searching.

"The InnoDB engine is fully transactional and supports foreign key references."

Apparently MySQL doesn't support transactions with MyISAM tables, which is the default type of tables. InnoDB tables do support transactions, so I'll recreate the tables and then see whether the transactions work then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM