views:

68

answers:

1

Hello

I'm trying to use Django's ORM in my non-HTTP part of project.

In one function I need to make bulk inserts of data, so commit_on_success decorator is what I need.

But I've found that this decorator doesn't do what supposed (didn't rollback transaction on error).

I've go into this decorator with debugger, I've fount thar reason is in this code:

if is_dirty():
    rollback()

Is dirty allways return true, so Django thought that transaction is allways clean.

And I'm geting "InternalError: current transaction is aborted, commands ignored until end of transaction block"

What can I do with this issue?

+1  A: 

The decorator worksforme.

is_dirty being true sounds like the request is ... dirty (would be logical to issue a rollback only if it's dirty).

Hence the rollback seems to happen every time. Is your database transaction-aware (eg not myIsam tables) ? Do you do any commit() within your method ? If yes, it cannot be rolled back of course.

You can of course do it manually, by issuing a commit at the end of the bulk import (or, better, each chunk of N values inserted + 1 at the end if you don't mind have your data half-imported) and wrapping the whole block in a

try : 
  do_whatever_inserts
except : 
  db.rollback()
  raise
else : 
  db.commit()
makapuf