Django嵌套的事务-"with transaction.atomic()" [英] Django nested transactions - “with transaction.atomic()”

查看:1013
本文介绍了Django嵌套的事务-"with transaction.atomic()"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我是否有这样的东西:

I would like to know if I have something like this:

def functionA():
    with transaction.atomic():
        #save something
        functionB()

def functionB():
    with transaction.atomic():
        #save another thing

有人知道会发生什么?如果functionB失败,functionA也将回滚吗?

Someone knows what will happen? If functionB fails, functionA will rollback too?

谢谢!

推荐答案

是的,它将.不管嵌套如何,如果原子块被异常退出

Yes, it will. Regardless of nesting, if an atomic block is exited by an exception it will roll back:

如果代码块成功完成,则更改将提交给数据库.如果有异常,则更改将回滚.

If the block of code is successfully completed, the changes are committed to the database. If there is an exception, the changes are rolled back.

还请注意,外部块中的异常将导致内部块回滚,并且可以捕获内部块中的异常以防止外部块回滚.该文档解决了这些问题. (或参见此处,以获取有关嵌套交易的更全面的后续问题).

Note also that an exception in an outer block will cause the inner block to roll back, and that an exception in an inner block can be caught to prevent the outer block from rolling back. The documentation addresses these issues. (Or see here for a more comprehensive follow-up question on nested transactions).

这篇关于Django嵌套的事务-"with transaction.atomic()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆