Comment 3 for bug 149335

Revision history for this message
Free Ekanayaka (free.ekanayaka) wrote :

Hi,

the following might be not entirely related to the bug, but it seems that the transaction module has support for something along these lines. One can write:

for attempt in transaction.attempts(attempts):
    with attempt as txn:
        # run application code here, like publish(request)

where the code of Attempt.__exit__ in the transaction module reads:

    def __exit__(self, t, v, tb):
        if v is None:
            self.manager.commit()
        else:
            retry = self.manager._retryable(t, v)
            self.manager.abort()
            return retry

in turn Manager._retryable will simply loop through the data managers and invoke their "should_retry" method, if available, a return True if any of them does:

    def _retryable(self, error_type, error):
        if issubclass(error_type, TransientError):
            return True

        for dm in self.get()._resources:
            should_retry = getattr(dm, 'should_retry', None)
            if (should_retry is not None) and should_retry(error):
                return True

So we could at least make retries very easy when using Storm with the transaction module, by providing a stock should_retry implementation, that could be possibly overridden by application code with some API.