django - transaction.atomic() 的失败模式有哪些?

标签 django transactions

面对事务中的数据库序列化错误,我不清楚 Django 的确切行为。

文档 transaction.atomic() docs据我所知,不要指定这种行为。

如果 DB 在提交事务时遇到一致性错误(例如,另一个事务更新了在当前事务中读取的值),则读取 django.db.transaction.py ,看起来事务将回滚,并且 DatabaseError 将被提升到调用代码(例如 transaction.atomic() 上下文管理器)。这样对吗?

而且,更重要的是,是否存在事务可以在没有 transaction.atomic 包装器收到异常的情况下回滚的情况?

(请注意,我不是在询问上下文管理器内引发的 DatabaseErrors,因为文档清楚地解释了它们发生了什么。我只询问在事务提交期间发生的数据库错误,该错误发生在退出时上下文管理器。)

最佳答案

If the DB hits a consistency error while committing a transaction ... it looks like the transaction will rollback, and the DatabaseError will be raised to the calling code (e.g. the transaction.atomic() context manager). Is this correct?



是的,正是。

Are there cases when the transaction could be rolled back without the transaction.atomic wrapper receiving an exception?



不。您可以从 transaction.py 中的代码中验证这一点。启动回滚的唯一时间是 if DatabaseError被抛出。这在documentation中也得到了证实。您链接到:

When exiting an atomic block, Django looks at whether it’s exited normally or with an exception to determine whether to commit or roll back.

关于django - transaction.atomic() 的失败模式有哪些?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37576397/

相关文章:

sql - DB2 SQL 脚本 : how to start a transaction and rollback on error

django - 我无法在 Django 的 block 内使用静态模板命令

python - 如何在模板中显示数据库中的数据

java - Postgres - 杀死未提交的事务

java - Spring 内部事务中的 UnexpectedRollBackException

PHP 事务和 mysqli_insert_id

python - 如何在 Django 中下载临时文件?

django - 流式 HTTP 响应,刷新到浏览器

当我只尝试编辑时,Django Admin save_model() 创建一个新对象

c# - 在一个事务中对多个相关表使用 SqlBulkCopy