[storm] recovering from a transactionrollbackerror

Martin DeMello martindemello at gmail.com
Fri May 15 08:44:54 BST 2009


On Thu, May 14, 2009 at 11:05 AM, Stuart Bishop <stuart at stuartbishop.net> wrote:
>
> The general pattern is:
>
> success = False
> while not success:
>    try:
>        do_stuff()
>        store.commit()
>        transaction_succeeded = True
>    except retry_exceptions:
>        pass
>
> def do_stuff():
>    try:
>        [do something]
>    except:
>        [cleanup non-DB stuff and revert non-DB changes]
>        raise

Thanks, Stuart. I've adapted that a bit to work with our current app:

try:
  do non-db stuff
  if success:
    try:
      do db stuff
    except retry_exceptions:
      redo db stuff
except:
  cleanup

Since the non-db stuff is often not revertible if it has succeeded.
Does python offer anything to help with this pattern? e.g. in ruby I
could define an ensure_commit method and then pass it the db changes I
want to make:

def do_stuff
  begin
    do non_db stuff
    ensure_commit {
       do db_stuff
    }
  rescue
    cleanup non_db stuff
  end
end

Where ensure_commit would once and for all capture the pattern of
retrying db commits till they succeeded or passed the retry cutoff and
logged a failure.

Incidentally, to get the app working here-and-now while we make the
more sweeping changes, I've taken to committing as often as possible,
before and after every block of code that writes to the db, to keep
transactions small. It seems to be working, though of course it's not
the 100% reliable way to do it, and we will need to do the rewrite.

martin

p.s. between the storm list, the storm docs and the postgres manuals,
i've finally understood what serializable transactions imply and how
to deal with them. will write something up for the wiki when i get a
free moment.



More information about the storm mailing list