"The outermost 'atomic' block cannot use savepoint = False when autocommit is off." error after upgrading to Django 1.8

549 views
Skip to first unread message

Matt Woodward

unread,
Apr 2, 2015, 2:42:26 PM4/2/15
to django...@googlegroups.com
Hi all -- I just upgraded one of my projects to Django 1.8 from 1.7.7 and in a view function where we're doing manual transaction management (Postgres) I'm now getting the error in the subject line.

Basic gist of the code involved:

def foo(request):
  try:
    transaction.set_autocommit(False)

    ... do stuff involving multiple database actions ...
  except (DatabaseError, Exception) as e:
    transaction.rollback()

This worked fine in 1.7.7 so I'm not sure what I need to change to get it working with 1.8.

I did read the docs on the atomic() functionality and took a few runs at that but just wound up with different errors, so I thought I'd start with what I originally had and see what I need to change to make Django 1.8 happy. But if using the atomic() stuff is preferable any tips around that would also be appreciated; I can post the errors I'm getting when trying things that way as needed.

Appreciate any ideas anyone has. Thanks.

Simon Charette

unread,
Apr 2, 2015, 3:09:59 PM4/2/15
to django...@googlegroups.com
Hi Matt,

I think it would be preferable to use atomic() here.

From reading the documentation it looks like you might already be in a transaction when calling `set_autocommit` but it's hard to tell without the full traceback.

What kind of errors do you get from:

def foo(request):
   
with transaction.atomic():
        # ... do stuff involving multiple database actions ...

Note that you should never catch `DatabaseError` within an `atomic()` context. You should wrap the context managed block with a try/except instead:

Wrong
with transaction.atomic():
   
try:
       
# Do database stuff possibly raising database errors
   
except DatabaseError:
       
pass

Right

try:
   
with transaction.atomic():
        # Do database stuff possibly raising database errors
except DatabaseError:
    pass

Simon

Matt Woodward

unread,
Apr 3, 2015, 11:02:06 AM4/3/15
to django...@googlegroups.com
Thanks so much for the quick and thorough reply!

I think I'm set now with the "Right" example below -- I was getting errors (the most cryptic of which was just "__exit__") when I was trying various incarnations of atomic but it seems to be working now using the example you provided.

The last thing I need to verify is the behavior change around assigning unsaved objects to relations since in this particular case I'm creating a new object and then saving other objects that have the first one as a FK, but that's looking good thus far in testing.

Thanks again for your help -- much appreciated!

Matt

Reply all
Reply to author
Forward
0 new messages