python中transaction.commit_unless_managed()
有什么用?
我对交易了解不多。在我的代码中我看到了一个函数transaction.commit_unless_managed()
,但是我不知道commit_unless_managed()
有什么用
transaction.commit_unless_managed()
请解释commit_unless_managed()
有什么用?
以及普通 commit
和 commit_unless_managed()
最佳答案
commit_unless_managed
是一个函数,它的作用与它所说的一样。如果代码在非托管事务 block 中,它会发出事务提交。注意commit_unless_managed
在 Django 1.8 中被移除。
你为什么要用它?如果您有一个可以在托管事务 block 和非托管事务 block 中调用的通用函数,您可以使用 commit_unless_managed
以便非托管事务代码路径发出提交。例如:
from django.db import transaction
@transaction.commit_manually()
def managed():
test()
@transaction.autocommit()
def unmanaged():
test()
def test():
# process some db commands
transaction.commit_unless_managed()
在此示例中,test
将在从 unmanaged
调用时发出提交,但在从 managed
调用时不会发出提交。
关于python - transaction.commit_unless_managed() python有什么用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41053985/