Mark Ramm wrote:
> So, we're finally getting around to handling transactions in
> middleware in tg2, and Alberto and I have been playing with different
> variations on the theme, and ultimately I think it might be
> good if we had a transaction middleware that provided a bit more
> configurability about when it rolls back transactions.


> Alberto added something which checked the HTTP status code for error
> codes and rolled back when the app indicated that an error had
> occurred (useful when there's middleware that catches and handles
> python exceptions), and I was playing with the idea of setting some
> kind of semi-standard parameter in the environ that signals that
> there's been an error somewhere and the transaction should be rolled
> back.

There is an existing pattern for this if you can get a hold of the transaction 
object itself in app code:  transaction.doom()

If that's not convenient, how about a way to define a "commit veto" hook?  Then 
you could do whatever you liked with the environ, status, or response headers 
determine that you shouldn't commit. An implementation might be:

def commit_veto(environ, status, response_headers):
     if not status.startswith('2'):
         return True

It would be called by the middleware just before it's about to commit.  If the 
commit veto hook return True, it would mean "don't commit".  Tthe middleware 
would capture the status and header output and do:

if transaction.isDoomed() or self.commit_veto(environ, status, headers):

The commit_veto function would be passed as an argument to the 
middleware constructor, so you'd create a TM via:

txn_managed_app = repoze.tm2.TM(app, commit_veto)

- C

Repoze-dev mailing list

Reply via email to