SuF*_*uFi 4 python mysql django
transaction.commit_unless_managed()在python中有什么用?
我不太了解交易。在我的代码中我看到了一个函数 transaction.commit_unless_managed(),但我不知道有什么用commit_unless_managed()
transaction.commit_unless_managed()
Run Code Online (Sandbox Code Playgroud)
请解释一下有什么用commit_unless_managed()?
和正常commit和之间的区别commit_unless_managed()
commit_unless_managed是一个函数,它执行它所谈论的内容。如果代码在非托管事务块中,它会发出事务提交。nb commit_unless_managed在 Django 1.8 中被移除。
你为什么要使用它?如果您曾经有一个可以在托管事务块和非托管事务块中调用的通用函数,则可以使用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()
Run Code Online (Sandbox Code Playgroud)
在此示例中,test将在调用 from 时发出提交,unmanaged但不会在调用 from时发出提交managed。