简体   繁体   English

Django transaction.commit_on_success没有回滚事务

[英]Django transaction.commit_on_success not rolling back transaction

I'm trying to use Django transactions on MySQL with the commit_on_success decorator . 我正在尝试使用commit_on_success装饰器在MySQL上使用Django事务。 According to the documentation, "If the function raises an exception, though, Django will roll back the transaction." 根据文档,“如果该函数引发异常,Django将回滚该事务。” 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; 我很确定MySQL支持交易; do I need to use a different table type or something? 我需要使用不同的表格类型吗?

From http://docs.djangoproject.com/en/dev/ref/databases/ : 来自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. “默认引擎是MyISAM [1] .MyISAM的主要缺点是它目前不支持事务或外键。从好的方面来说,它是目前唯一支持全文索引和搜索的引擎。

"The InnoDB engine is fully transactional and supports foreign key references." “InnoDB引擎是完全事务性的,支持外键引用。”

Apparently MySQL doesn't support transactions with MyISAM tables, which is the default type of tables. 显然MySQL不支持MyISAM表的事务,这是表的默认类型。 InnoDB tables do support transactions, so I'll recreate the tables and then see whether the transactions work then. InnoDB表支持事务,因此我将重新创建表,然后查看事务是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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