How to tell if your select query is within a transaction or not?


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


Since Django 1.7, Connection.in_atomic_block will tell you if a connection is inside a transaction or not. It doesn’t seem like this is documented, but it Works On My Machine:

from django.db import transaction
cxn = transaction.get_connection()
if cxn.in_atomic_block:
    print "We're inside a transaction!"

Answered By – David Wolever

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published