I do something like this with a SqlRetry class. It has a SqlRead method which executes the sql, and if it raises a transient error will retry n times. If you pass a function to a Transaction, it wraps it in 'begin..commit' (and rolls back if an exception escapes) and retries triansient errors at the transaction level rather than the statement level. It's worked well for me. I don't think I can release it since I wrote it for work, but it was easy and simple (~300 lines). The only time consuming part is gradually finding out what MySQLdb raises for transient errors and which are permanent (MySQLdb is quite inconsistent and confusing).