10

In Django 1.5.x, I have a long running management command where select queries are returning stale data. I suspect this is due to the fact that they are running within a transaction that are started earlier on the db connnection. Is there a way a tell if a query runs within a transaction or it is in autocommit mode?

(this is somewhat more focused version of an earlier question I posted at https://stackoverflow.com/questions/18540099/orm-does-not-return-recent-database-changes)

4

3 回答 3

24

从 Django 1.7 开始,Connection.in_atomic_block将告诉您连接是否在事务中。似乎没有记录,但它适用于我的机器:

from django.db import transaction
cxn = transaction.get_connection()
if cxn.in_atomic_block:
    print "We're inside a transaction!"
于 2017-02-13T23:51:01.743 回答
3

从 Django 1.6 开始,您可以通过transaction.get_autocommit API判断您是否处于自动提交模式。

from django.db import transaction

if transaction.get_autocommit():
    pass  # We are in autocommit
于 2016-11-12T12:06:37.270 回答
2

您可以通过检查来检查您是否在交易中is_managed

if transaction.is_managed():
    print "tutsi frutsi!"
于 2013-08-31T03:05:06.203 回答