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

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

Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail
From: Karsten Hilbert <Karsten.Hilbert@gmx.net>
Newsgroups: comp.lang.python
Subject: psycopg2: proper positioning of .commit() within try: except: blocks
Date: Sat, 7 Sep 2024 17:48:01 +0200
Lines: 156
Sender: <karsten.hilbert@gmx.net>
Message-ID: <mailman.42.1725724089.2917.python-list@python.org>
References: <Ztx1sZMGTnA3eLP1@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 hTTFg9SV7xIkfIfted8KaQqEgk8yGLKxaZjo5Gy24dFw==
Cancel-Lock: sha1:tZAAUt1O2ME3zm4IlHY1iiZpEvc= sha256:sCtTDoupfytQpN5uYLHMPHG8es40nde385NnU89gQlg=
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=EjTXL9Ik; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'pfxlen:0': 0.03;
 'traceback': 0.04; '(most': 0.05; 'error:': 0.05; 'last):': 0.05;
 'debug': 0.07; 'received:212.227': 0.07; 'cc:addr:python-list':
 0.09; 'dependencies': 0.09; 'fails': 0.09; 'gpg': 0.09; 'karsten':
 0.09; 'skip:# 30': 0.09; 'skip:^ 10': 0.09; 'supplied': 0.09;
 'cc:no real name:2**0': 0.14; '1713': 0.16; '248,': 0.16; 'args:':
 0.16; 'canceled': 0.16; 'commit': 0.16; 'cursor:': 0.16;
 'database.': 0.16; 'defaults': 0.16; 'idiom': 0.16; 'insights,':
 0.16; 'read/write': 0.16; 'received:212.227.15': 0.16; 'subject:()
 ': 0.16; 'subject:proper': 0.16; 'subject:try': 0.16; 'tries':
 0.16; 'reached': 0.17; 'cc:addr:python.org': 0.20; 'all,': 0.20;
 'option': 0.20; 'exception': 0.22; 'code': 0.23; 'skip:p 30':
 0.23; 'cc:2**0': 0.25; 'creating': 0.27; 'thinking': 0.28;
 'default': 0.31; 'raise': 0.31; 'particular': 0.33; 'failed':
 0.35; 'yes,': 0.35; 'file': 0.38; 'could': 0.38; 'thanks': 0.38;
 'list': 0.39; 'skip:u 20': 0.39; 'learn': 0.40; 'should': 0.40;
 'today': 0.61; 'dear': 0.62; 'received:212': 0.62; 'skip:g 30':
 0.64; 'transaction': 0.64; 'among': 0.65; 'well': 0.65; 'skip:t
 20': 0.66; 'now,': 0.67; 'during': 0.69; 'and,': 0.69; 'type:':
 0.69; 'care': 0.71; 'free': 0.72; 'head': 0.73; 'placement': 0.81;
 '"")': 0.84; '-1)': 0.84; 'detail:': 0.84; 'skip:" 40': 0.84;
 'skip:" 50': 0.84; 'skip:( 80': 0.84; 'subject:within': 0.84;
 'transactions': 0.84; 'worries,': 0.84; 'safely': 0.91; 'skip:(
 50': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net;
 s=s31663417; t=1725724082; x=1726328882; i=karsten.hilbert@gmx.net;
 bh=0Q5/l5bSI8LXl7473qHEKdGrpPg0HEfcq9VuANZfoto=;
 h=X-UI-Sender-Class:Date:From:To:Cc:Subject:Message-ID:
 MIME-Version:Content-Type:Content-Transfer-Encoding:cc:
 content-transfer-encoding:content-type:date:from:message-id:
 mime-version:reply-to:subject:to;
 b=EjTXL9IkPD6zNKMQd5efVKUY82EuTers+uP5CKXW5VH3dGlQ5cJfapTH/7MZbU1i
 wWBNUWj9nEVc34i9hVSnlfj4cyLasbSioo5t4WwWRI57XVyhGdhp8ven4JsVh6ERc
 s+edzP2Hdnf7U++t0weBw7nU71ay09+h0CT33UwiYldj+8DOrtt2lf8vzyIPZACwn
 d+S4heCZ8aCbJGY7daqLBYXH7u5vSmWT48W4lZYGnCHBggZYqzVP193xdO3kDYrAx
 LgkSRAoPxny9zDFx2tLqGjATyu0M/8z58zveyMs9uRIvcIWpaB7Qz37ULA815Ps3t
 1rB+dMo6n7LdaN0yMQ==
X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a
Content-Disposition: inline
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:ZX+6omuxzxN/xxC32FOQEQOawxD2Vc+klAxNC9JpbZ1vKRM8t1r
 n0h28bB8Hmz37eYWa708vW4yF+ABqk88YIeOv0zsTTzNyeLOx95ZwVHbI3wfJ8cXsa423xI
 raSd7vbe+peRUkUHvjKSRwBIezKPzZIatZslQedrAfdlbcvn+/n+9UwK8LKJf7qHtsceru8
 Ht3VY7eJUQAudr165NioA==
X-Spam-Flag: NO
UI-OutboundReport: notjunk:1;M01:P0:bb/XNY3cwoY=;Ty1rhNWkK82jYLrDgZy0SUt9Ne9
 V80hKynnk+mLm8RWebamOSHFO01WmsWHLh5w6NVj36aA00JTZToXuk8/JIGEReISLEoRL3DwH
 2epDJ2PzljhUsgX3RboQqRf8/HhQODeWQ9fyTNfP+7odvsPYuX9RplsC/n+OQ0atOUDh51FAc
 C+4/ansKIpxSJKghzsfqlUV4zmVHver2RzCnXitCMz5Rh/yfVQXqUm/mevGtgysKmYhhbQEEr
 6kHHrINeY/tvMvQY90/BUzy+3u6seUU0/bHnpxbT/WZF8jITj8jTlRoYLcR9kdmLBjrxjndB0
 u9f8j9+M2zfpxNT/wMMtu6mKw0kizGYkkKwYEE5wFwj8jLPenaCJ3/ca1zI+OkFnXBLpaQfI/
 ube3XAIPcHWcYN09kEoIEYMKOpgU15jHrRjf94PGyo42LOLhkZgvb83PNWoRdHYv5+5Ueus/r
 j1oqDrybQmTwwM/Tfv03MbTq3hH9AxScOP1AcwllW3EsUc9eDxL6yZvZLP58S9oL91fmIuzho
 K1NqljwvZiv/xEhcb208lF0AQbyz9NrxOB9bZeEjI4XORklG8b1/xrpvZH4O04n+8Zr+DQhS9
 D/kQ5q5e/t3QzqPDLXCVbkLPQNZXSzni2n7O0AAQK0TlgEu5YxOatej9d2nZMj9np95WnTOpt
 5QfWYtAcVhsQVS/OFIeQvPMqze1EiRsM+0cJjZ7p3azVDKjhWFyBDFLcM7YtrSmIXjS1MEzbo
 Rh8fDN2Gu+mmf9KQ500MvlZbO13JQ7tQaRjKtvAC71MMxrel6JUpyo5wz8HERKHV9Nxu+wSnZ
 55I4c22TlCc6xlnDTQjJ6ITA==
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: <Ztx1sZMGTnA3eLP1@hermes.hilbert.loc>
Bytes: 12214

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 =3D psycopg2.connection(...)
	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
		conn.close()

So today I head to learn that conn.commit() may very well
raise a DB related exception, too:

	psycopg2.errors.SerializationFailure: could not serialize access due to r=
ead/write dependencies among transactions
	DETAIL:  Reason code: Canceled on identification as a pivot, during commi=
t attempt.
	TIP:  The transaction might succeed if retried.

Now, what is the proper placement of the .commit() ?

(doing "with ... as conn:" does not free me of committing appropriately)

Should I

	try:
		curs.execute(SOME_SQL)
		conn.commit()
	except PSYCOPG2-Exception:
		some logging being done, and, yes, I
		can safely inhibit propagation^1
	finally:
		conn.close()			# which should .rollback() automagically in case we had n=
ot reached to .commit()

?

Thanks for insights,
Karsten

#-------------------------------
^1:

	This particular code is writing configuration defaults
	supplied in-code when no value is yet to be found in the
	database. If it fails, no worries, the supplied default
	is used by follow-on code and storing it is re-tried next
	time around.

#-------------------------------
Exception details:

	Traceback (most recent call last):
	  File "/usr/share/gnumed/Gnumed/wxpython/gmGuiMain.py", line 3472, in On=
Init
	    frame =3D gmTopLevelFrame(None, id =3D -1, title =3D _('GNUmed client=
'), size =3D (640, 440))
	            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^=
^^^^^^^^^^^^^^^^
	  File "/usr/share/gnumed/Gnumed/wxpython/gmGuiMain.py", line 191, in __i=
nit__
	    self.LayoutMgr =3D gmHorstSpace.cHorstSpaceLayoutMgr(self, -1)
	                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	  File "/usr/share/gnumed/Gnumed/wxpython/gmHorstSpace.py", line 215, in =
__init__
	    self.top_panel =3D gmTopPanel.cTopPnl(self, -1)
	                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	  File "/usr/share/gnumed/Gnumed/wxpython/gmTopPanel.py", line 52, in __i=
nit__
	    wxgTopPnl.wxgTopPnl.__init__(self, *args, **kwargs)
	  File "/usr/share/gnumed/Gnumed/wxGladeWidgets/wxgTopPnl.py", line 33, i=
n __init__
	    self._TCTRL_patient_selector =3D cActivePatientSelector(self, wx.ID_A=
NY, "")
	                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^=
^^^^^
	  File "/usr/share/gnumed/Gnumed/wxpython/gmPatSearchWidgets.py", line 12=
95, in __init__
	    cfg.get2 (
	  File "/usr/share/gnumed/Gnumed/pycommon/gmCfg.py", line 248, in get2
	    self.set (
	  File "/usr/share/gnumed/Gnumed/pycommon/gmCfg.py", line 367, in set
	    rw_conn.commit()		# will rollback if transaction failed
	    ^^^^^^^^^^^^^^^^
	psycopg2.errors.SerializationFailure: could not serialize access due to r=
ead/write dependencies among transactions
	DETAIL:  Reason code: Canceled on identification as a pivot, during commi=
t attempt.
	TIP:  The transaction might succeed if retried.

	2024-08-20 22:17:04  INFO      gm.cfg        [140274204403392 UpdChkThrea=
d-148728]  (/usr/share/gnumed/Gnumed/pycommon/gmCfg.py::get2() #148): crea=
ting option [horstspace.update.consider_latest_branch] with default [True]
	2024-08-20 22:17:04  DEBUG     gm.db_pool    [140274459512896 MainThread]=
========== REMAINDER OF ARTICLE TRUNCATED ==========