psycopg2: proper positioning of .commit() within try: except: blocks

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Sep 8 21:33:24 EDT 2024


On 8/09/24 11:03 pm, Jon Ribbens wrote:
> On 2024-09-08, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>>     try:
>>       do something
>>       .commit()
>>     except:
>>       log something
>>       .rollback()
> 
> What if there's an exception in your exception handler? I'd put the
> rollback in the 'finally' handler, so it's always called.

Good point. Putting the rollback first would be safer/

-- 
Greg


More information about the Python-list mailing list