Django transaction.commit_on_success没有回滚事务 [英] Django transaction.commit_on_success not rolling back transaction
问题描述
我试图在MySQL上使用Django事务,使用 commit_on_success
decorator 。根据文档,如果函数引发异常,Django会回滚事务。但是,这对我来说似乎不起作用:
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
>>>
我很确定MySQL支持交易;我需要使用不同的表格类型吗?
I'm pretty sure that MySQL supports transactions; do I need to use a different table type or something?
推荐答案
从 http://docs.djangoproject.com/en/dev/ref/databases/ :
默认引擎是MyISAM [1],MyISAM的主要缺点是它目前不支持事务或外键,另外,它是目前唯一支持全文的引擎索引和搜索。
"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.
InnoDB引擎是完全事务性的,并支持外键引用。
"The InnoDB engine is fully transactional and supports foreign key references."
这篇关于Django transaction.commit_on_success没有回滚事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!