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

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Karsten Hilbert <Karsten(dot)Hilbert(at)gmx(dot)net>, psycopg(at)lists(dot)postgresql(dot)org
Cc: python-list(at)python(dot)org
Subject: Re: psycopg2: proper positioning of .commit() within try: except: blocks
Date: 2024-09-07 16:46:03
Message-ID: 7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: 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(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Karsten Hilbert 2024-09-07 19:44:36 Re: psycopg2: proper positioning of .commit() within try: except: blocks
Previous Message Karsten Hilbert 2024-09-07 15:48:01 psycopg2: proper positioning of .commit() within try: except: blocks