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

Liste des GroupesRevenir à cl python 
Sujet : Re: psycopg2: proper positioning of .commit() within try: except: blocks
De : Karsten.Hilbert (at) *nospam* gmx.net (Karsten Hilbert)
Groupes : comp.lang.python
Date : 08. Sep 2024, 13:13:37
Autres entêtes
Message-ID : <mailman.48.1725794019.2917.python-list@python.org>
References : 1 2 3 4 5 6 7 8 9 10
Am Sun, Sep 08, 2024 at 12:48:50PM +1200 schrieb Greg Ewing via Python-list:

On 8/09/24 9:20 am, Karsten Hilbert wrote:
try:
do something
except:
log something
finally:
.commit()
>
cadence is fairly Pythonic and elegant in that it ensures the
the .commit() will always be reached regardless of exceptions
being thrown or not and them being handled or not.
>
That seems wrong to me. I would have thought the commit should only
be attempted if everything went right.

It is only attempted when "everything" went right. The fault
in my thinking was what the "everything" might encompass.
When some SQL fails it won't matter whether I say
conn.commit() or conn.rollback() or, in fact, nothing at all
-- the (DB !) transaction will be rolled back in any case.

However, that reasoning missed this:

What if there's a problem in your code that causes a non-SQL-related
exception when some but not all of the SQL statements in the
transaction bave been [-- even successfully --] issued?

Still, in this code pattern:

  try:
    do something
    .commit()
  except:
    log something
it doesn't technically matter whether I say .commit or .rollback here:
    .rollback()

... but ...

Doing an explicit rollback ensures that the transaction is always
rolled back if it is interrupted for any reason.

explicit is better than implicit ;-)

Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B

Date Sujet#  Auteur
7 Sep 24 * Re: psycopg2: proper positioning of .commit() within try: except: blocks25Karsten Hilbert
8 Sep 24 `* Re: psycopg2: proper positioning of .commit() within try: except: blocks24Greg Ewing
8 Sep 24  +* Re: psycopg2: proper positioning of .commit() within try: except: blocks20Jon Ribbens
8 Sep 24  i+- Re: psycopg2: proper positioning of .commit() within try: except: blocks1Stefan Ram
8 Sep 24  i+* Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)17Lawrence D'Oliveiro
9 Sep 24  ii`* Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)16Jon Ribbens
9 Sep 24  ii `* Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)15Lawrence D'Oliveiro
9 Sep 24  ii  `* Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)14Jon Ribbens
9 Sep 24  ii   +- Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)1Karsten Hilbert
9 Sep 24  ii   +* Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)2Karsten Hilbert
9 Sep 24  ii   i`- Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)1Jon Ribbens
9 Sep 24  ii   `* Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)10Lawrence D'Oliveiro
9 Sep 24  ii    `* Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)9Jon Ribbens
9 Sep 24  ii     `* Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)8Lawrence D'Oliveiro
10 Sep 24  ii      `* Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)7Jon Ribbens
10 Sep 24  ii       +* Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)2Karsten Hilbert
10 Sep 24  ii       i`- Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)1Jon Ribbens
10 Sep 24  ii       `* Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)4Lawrence D'Oliveiro
11 Sep 24  ii        `* Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)3Jon Ribbens
11 Sep 24  ii         `* Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)2Lawrence D'Oliveiro
11 Sep 24  ii          `- Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)1Jon Ribbens
9 Sep 24  i`- Re: psycopg2: proper positioning of .commit() within try: except: blocks1Greg Ewing
8 Sep 24  +- Re: psycopg2: proper positioning of .commit() within try: except: blocks1Karsten Hilbert
8 Sep 24  +- Re: psycopg2: proper positioning of .commit() within try: except: blocks1Karsten Hilbert
13 Sep 24  `- Re: psycopg2 positioning of .commit() (Posting On Python-List Prohibited)1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal