Deutsch   English   Français   Italiano  
<mailman.44.1725738290.2917.python-list@python.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!2.eu.feeder.erje.net!3.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: Karsten Hilbert <Karsten.Hilbert@gmx.net>
Newsgroups: comp.lang.python
Subject: Re: psycopg2: proper positioning of .commit() within try: except:
 blocks
Date: Sat, 7 Sep 2024 21:44:36 +0200
Lines: 69
Sender: <karsten.hilbert@gmx.net>
Message-ID: <mailman.44.1725738290.2917.python-list@python.org>
References: <Ztx1sZMGTnA3eLP1@hermes.hilbert.loc>
 <7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com>
 <ZtytJMhyvtExPxfF@hermes.hilbert.loc>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de xoeeCbNxFhxxwcCrVkKvrQdKn4id9nDEo+cD1+N1aGqQ==
Cancel-Lock: sha1:sqPFge3q0XiS1kbdqsj+lobFSLE= sha256:Ly5nxvWmeljC3EpxfncVXgZ07gBudVePfeIYpUg3XG8=
Return-Path: <karsten.hilbert@gmx.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
 reason="2048-bit key; unprotected key"
 header.d=gmx.net header.i=karsten.hilbert@gmx.net
 header.b=R+q/+DlJ; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.017
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'hosting': 0.07;
 'received:212.227': 0.07; 'wrong.': 0.07; 'cc:addr:python-list':
 0.09; 'concurrent': 0.09; 'fails': 0.09; 'gpg': 0.09; 'indeed.':
 0.09; 'karsten': 0.09; 'schrieb': 0.09; '1713': 0.16; '2024':
 0.16; 'commit': 0.16; 'connection.': 0.16; 'idiom': 0.16;
 'subject:() ': 0.16; 'subject:proper': 0.16; 'subject:try': 0.16;
 'termination': 0.16; 'tries': 0.16; 'python': 0.16; 'probably':
 0.17; 'cc:addr:python.org': 0.20; 'closed': 0.22; 'sat,': 0.22;
 'code': 0.23; 'cc:2**1': 0.23; 'run': 0.23; 'matter.': 0.26;
 'else': 0.27; 'fact': 0.28; 'thinking': 0.28; 'wondering': 0.31;
 'sep': 0.32; 'unless': 0.32; 'but': 0.32; 'particular': 0.33;
 'header:In-Reply-To:1': 0.34; 'running': 0.34; 'failed': 0.35;
 'runs': 0.35; 'yes,': 0.35; 'close': 0.35; 'thanks,': 0.36; 'put':
 0.38; 'otherwise': 0.39; 'use': 0.39; 'still': 0.40; 'best': 0.61;
 'connection': 0.61; 'above': 0.62; 'mental': 0.62; 'received:212':
 0.62; 'transaction': 0.64; 'upon': 0.64; 'back': 0.67; 'matter':
 0.68; 'and,': 0.69; 'closing': 0.69; 'within': 0.69; 'care': 0.71;
 'issued': 0.76; 'database': 0.80; 'subject:within': 0.84;
 'succeeded': 0.84; 'transactions': 0.84; 'url:connection': 0.84;
 'safely': 0.91; 'ongoing': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net;
 s=s31663417; t=1725738277; x=1726343077; i=karsten.hilbert@gmx.net;
 bh=wCUETbtRwcUWzwkz33ymBKOwyZdaAsxwzS2RrGOomGg=;
 h=X-UI-Sender-Class:Date:From:To:Cc:Subject:Message-ID:References:
 MIME-Version:Content-Type:In-Reply-To:Content-Transfer-Encoding:
 cc:content-transfer-encoding:content-type:date:from:message-id:
 mime-version:reply-to:subject:to;
 b=R+q/+DlJcSaXR/9pVb8Jn3ZZ/LLKdORWlZoccTXce2qB3Zdix01knkXAI/63pKLY
 XNtirO0EPlkT9acziBtjDWcYZC4+0QLt79gMKiUmD+Weh8d+EAsM1gSyGLERCPmRy
 UQLGwc17hrw72UEEPiTEqwudwIGFuNJuVbUva2+OZF1fcZSghp21k2RUsS1Tt0/Cg
 rQiS/LxfM+lXqwfTo50juKtA7eaJw/Xny2PUQWhngb4UFzZGMYnv191q7FoD22MUQ
 26F+ZsuihUGH7XyAVU7jz2y/12KdqSGpegH4AyDjNgRX3AK+h5PuE3auQH0MwTjHL
 r+AzlMHWtnt3w8Qygw==
X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a
Content-Disposition: inline
In-Reply-To: <7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com>
Ma_X_il-Followup-to: d
Re_X_turn-receipt-to: Karsten.Hilbert@gmx.net
Di_X_sposition-Notification-To: Karsten.Hilbert@gmx.net
X-Confi_X_rm-Reading-To: Karsten.Hilbert@gmx.net
X-Pri_X_ority: 2 (High)
X-Provags-ID: V03:K1:Yvu5+EO2oBxOOPUV5oi/hAuISjxfaKwBU2u/c0TTUGr+ZAkKCWM
 0IIg3JDw46uoj1GHiqH23kYoiqagimr2n/XYOUMuIHBe3PQEgW9Oh5sGziUlIcY0d1pQu3j
 jDu/q5mGOU4I9VzZ4JVhw4beUmTgXlhBZmPigvkX1VbRl+m/RMdPp+sbsOCYmVRMC6Ao3Tm
 xstu1WiqRqF9BGUpmtoMw==
X-Spam-Flag: NO
UI-OutboundReport: notjunk:1;M01:P0:sDl2TmaTtU4=;/b8zDZDHUkHLXjYZZV4UZVXstDP
 7x54aAxDVnL2zmyxyKSKluy7roZYVZWuONQU7u+ULY2xRYfm/tftwKvNarhC6r5RsCo8pNihj
 I4hmXzQRjaLIecVCGY3O6TO5gsfSLZX10dH8XCua6TAjStB6BzZ47I8HG/enaLGEh0fEMvns4
 +kVv4rw8XrU+kd5068O2ULhpTr2eC7KsAx/UfQAtnm1Og6rx1j0u4ZiqYFaijHlNFdlK+d9rD
 g30ztdDgbZaEUGAg+11e4EnvrB/5ZveSMiHniVnzBhHMeC1j12M9X+z05wJHAgkQWMej1LXBE
 1hVcs+qO4rHHs6bB3qE+gIxltdaG2PgdEy2KtwwbelGGawbTc0FuInJ92tCcVTFZ2NMpb09gz
 0JB/mFQ9auP0xZZh0H4fjBDPuXkkH2nChcx6dKkYCzlusrS4gp2wi4AF6YgDleTkisFPYV5vl
 BZaoDAacphrOK1PCbxzF3x/MfwdumlFSJnmNyHmeUMWMblbuauaGMT+lFNtXsclJnRNMIjJou
 ahsUKyWYKC1RbqP1DMmLJ9BT0suNpuuF9W1JPsG2bWcQwsSR9vQ/FJgRTpKGvAeea6altz59d
 4IvUBg5eKus99TlODc5PmW8c9QnzpL5ECi+K3j0nDhTGYLs77qstUdD0XMjLZS990fjBzrRem
 KZeA2Z7v9+bprTykmm7uPAfohULhojY3QfuBiEbecoH+kKhVJFkKY2NTF887ZjFL8pBPvxGrf
 I7j/DQi2lCKqRb9Xdi90I8NM62xywCKW8FdHjukutSGaA0H+6vDyy31nVwL8qjBue0ZURT1jx
 f3YvQ9ousBLGpuP/7i2tBjMQ==
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
 <python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
 <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
 <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <ZtytJMhyvtExPxfF@hermes.hilbert.loc>
X-Mailman-Original-References: <Ztx1sZMGTnA3eLP1@hermes.hilbert.loc>
 <7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com>
Bytes: 8011

Am Sat, Sep 07, 2024 at 09:46:03AM -0700 schrieb Adrian Klaver:

> >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 =3D psycopg2.connection(...)
>
> In the above do you have:
>
> https://www.psycopg.org/docs/extensions.html#psycopg2.extensions.ISOLATI=
ON_LEVEL_SERIALIZABLE
>
> psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE

I do indeed.

> Or is that in some other concurrent transaction?

In fact in that codebase all transactions -- running
concurrently or not -- are set to SERIALIZABLE.

They are not psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT,
for that matter.

> >	curs =3D 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: ?

No I don't but - to my understanding - an ongoing transaction
is being closed upon termination of the hosting connection.
Unless .commit() is explicitely being issued somewhere in the
code that closing of a transaction will amount to a ROLLBACK.

In case of SQL having failed within a given transaction a
COMMIT will fail-but-rollback, too (explicit ROLLBACK would
succeed while a COMMIT would fail and, in-effect, roll back).

IOW, when SOME_SQL has failed it won't matter that I close
the connection with conn.commit() and it won't matter that
conn.commit() runs a COMMIT on the database -- an open
transaction having run that failed SQL will still roll back
as if ROLLBACK had been issued. Or else my mental model is
wrong.

	https://www.psycopg.org/docs/connection.html#connection.close

In the particular case I was writing about the SQL itself
succeeded but then the COMMIT failed due to serialization. I
was wondering about where to best place any needed
conn.commit(). My knee-jerk reaction was to then put it last
in the try: block...

All this is probably more related to Python than to PostgreSQL.

Thanks,
Karsten
=2D-
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B