gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:46:42 28 4
gpt4 key购买 nike

面对事务中的数据库序列化错误,我不清楚 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com