Re: psycopg2: proper positioning of .commit() within try: except: blocks - Mailing list psycopg

From Adrian Klaver
Subject Re: psycopg2: proper positioning of .commit() within try: except: blocks
Date
Msg-id 7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com
Whole thread Raw
In response to psycopg2: proper positioning of .commit() within try: except: blocks  (Karsten Hilbert <Karsten.Hilbert@gmx.net>)
Responses Re: psycopg2: proper positioning of .commit() within try: except: blocks
List psycopg
On 9/7/24 08:48, Karsten Hilbert wrote:
> Dear all,
> 
> unto now I had been thinking this is a wise idiom (in code
> that needs not care whether it fails to do what it tries to
> do^1):
> 
>     conn = psycopg2.connection(...)

In the above do you have:

https://www.psycopg.org/docs/extensions.html#psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE

psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE

Or is that in some other concurrent transaction?

>     curs = conn.cursor()
>     try:
>         curs.execute(SOME_SQL)
>     except PSYCOPG2-Exception:
>         some logging being done, and, yes, I
>         can safely inhibit propagation^1
>     finally:
>         conn.commit()        # will rollback, if SOME_SQL failed

It will if you use with conn:, otherwise it up to you to do the rollback()

Are you are doing a rollback() in except PSYCOPG2-Exception: ?



-- 
Adrian Klaver
adrian.klaver@aklaver.com




psycopg by date:

Previous
From: Karsten Hilbert
Date:
Subject: psycopg2: proper positioning of .commit() within try: except: blocks
Next
From: Karsten Hilbert
Date:
Subject: Re: psycopg2: proper positioning of .commit() within try: except: blocks