Django嵌套Transaction.atomic [英] Django nested Transaction.atomic
问题描述
通过阅读文档: https://docs.djangoproject.com/en/dev/topics/db/transactions/#django.db.transaction.atomic
我知道
atomic blocks can be nested. In this case, when an inner block completes successfully, its effects can still be rolled back if an exception is raised in the outer block at a later point.
但是,我的问题是对于如下代码结构:
However, my question is that for a code structure like following:
@transaction.atomic
def A():
## something
B()
C()
## something
@transaction.atomic
def B():
## something
@transaction.atomic
def C():
## something
如果B和C都成功了,而A在他们之后出了错,那么B和C会马上回来,对吗?
If B and C both succeed, and A goes wrong after them, then B and C will right back, right?
如果B成功了,但是C搞砸了,B会回滚怎么办?
What if B succeed, but C messed up, will B get roll back?
关于维护此回滚功能的内存使用情况,上面的内容与下面的内容之间是否有任何区别:
And about the memory usage for maintaining this roll back functionality, is there any difference between the one above and the one following:
@transaction.atomic
def A():
B()
C()
def B():
## something
def C():
## something
我知道这两种结构处理不同的情况.我只是问,假设它们都成功(完全),那么在内存使用级别上有什么区别?
I know these two structure handle different case. I am just asking, assume they both succeed (completely), what is the difference at the memory usage level?
先谢谢了.
推荐答案
如果B和C都成功了,而A在他们之后错了,那么B和C会马上回来,对吗?
If B and C both succeed, and A goes wrong after them, then B and C will right back, right?
是的,整个交易A都将回滚,包括B和C.
Yes, the entire transaction A would be rolled back including B and C.
如果B成功了,但是C搞砸了,B会回滚怎么办?
What if B succeed, but C messed up, will B get roll back?
同样,整个交易A将被回滚,包括B和C.
Again, the entire transaction A would be rolled back, including B and C.
我不能说内存使用量是多少.这将取决于您的数据库引擎和您正在运行的代码.除非您使用非常大的数据集,否则我不会担心.如果您要处理非常大的数据集,请尝试!
I can't say what the memory usage would be. It would depend on your database engine and the code you are running. Unless you are working with very large datasets, I wouldn't worry about it. If you are working with very large datasets, then experiment!
这篇关于Django嵌套Transaction.atomic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!