ATOMIC_REQUESTS setting not working

288 views
Skip to first unread message

EJ

unread,
Sep 10, 2015, 6:15:39 PM9/10/15
to Django users
Hi All,

I have ATOMIC_REQUESTS set to True in my database configuration:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'abcd',
'USER': 'user',
'PASSWORD': '',
'HOST': '',
'PORT': '',
'ATOMIC_REQUESTS': True,
}
}

But I started getting the following error when attempting to use select_for_update() in one particular view:

select_for_update cannot be used outside of a transaction.

As the error suggests, it turns out that database queries inside this particular view are not executing inside a transaction, even though ATOMIC_REQUESTS is set to True.  I tested this essentially by performing an insert within the same view and then throwing an exception -- the change was indeed committed.  

I'm not sure why this is occurring.   Other views in my application respect the ATOMIC_REQUESTS setting, and I'm able to use select_for_update() successfully elsewhere.  There is nothing special about this particular view that I can see.  It's just processing a form and inserting some records.  I can paste the view code if it helps, but there is really nothing remarkable about it, which leads me to believe that the problem lies elsewhere.

I have no idea how to debug this.  I would like all requests in my application to be wrapped in transactions, as indicated by the ATOMIC_REQUESTS setting.  Any help is appreciated.

Thanks.

EJ

unread,
Sep 10, 2015, 9:14:34 PM9/10/15
to Django users
Small update: Wrapping the view function with @transaction.atomic fixes the issue -- the queries execute in a transaction as expected.

So I guess my immediate problem is resolved.

If anyone has an idea, I'd still love to know why the view is ignoring the top-level TRANSACTION_ATOMIC setting.   In case it's happening elsewhere that I'm not aware of.
Reply all
Reply to author
Forward
0 new messages