[ZODB-Dev] How does automatic retrying work?

Alexandre Garel alex.garel at tarentis.com
Fri Mar 30 08:30:38 UTC 2012


Le 29/03/2012 20:28, Claudiu Saftoiu a écrit :
> Hello all,
>
> I have an HTTP request which has roughly two parts to it. One part
> requires a lot of processing with a small chance of a DatabaseConflict,
> while the other part requires little processing with a high chance of
> a DatabaseConflict. I'd like to do something like this:
>
> def my_view(request):
>     transaction.begin()
>     slow_no_conflict()
>     transaction.commit()
>     for avar in something:
>         transaction.begin()
>         fast_yes_conflict(avar)
>         transaction.commit()
>
> My question is: how will automatic retrying work? Most of my views are 
> simply:
>
> def regular_view(request):
>     do_stuff()
>
> and, if something conflicts, the whole thing is just retried. In 
> `my_view`,
> what will happen if the `slow_no_conflict()` function has a conflict?
> What will happen if the `fast_yes_conflict(avar)` function has a conflict?
> What if its in the first iteration of the loop, or the last? I'm not 
> quite sure
> how to properly think about these things yet.

Hello retry shall be made on a transaction basis.

I would say, that while for the regular_view retry may be handled by a 
middleware or such (the publisher in Zope2 ?), you may want to manage it 
in my_view around your transaction.

in my_view you do transaction commit, so you won't undo the 
slow_no_conflict part if you fail in the second part (if it was not your 
intent you shall use subtransactions).

So you may do something like :

from ZODB.POSException import ConflictError

def my_view(request):

     transaction.begin()
     slow_no_conflict()
     transaction.commit()

     do_retry = True
     while do_retry:
        try:
           transaction.begin()
           fast_yes_conflict(avar)
           transaction.commit()
           do_retry = False
        except Exception, e:
           transaction.abort()
           do_retry = isinstance(e, ConflictError)


Hope this helps !

Alex

-- 
Alexandre Garel
06 78 33 15 37



More information about the ZODB-Dev mailing list