try: cursor.execute("INSERT INTO table VALUES (%s, %s)", (a, b)) obj.setValues(a, b) except: cursor.rollback() raise
Beware - there is a subtle bug here which cost me a few hours of debugging time. If the rollback raises an exception then the first exception is silently ignored. For example if the database connection has been lost (triggering the first exception) the cursor.rollback() will also fail, but all you will see is an exception arising from the rollback. Better to log something about the first exception before trying the rollback.
some usefull exception-handling routine here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52215# Denis