Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Q: What's tan and black and looks great on a lawyer? A: A doberman.


comp / comp.lang.python / psycopg2: proper positioning of .commit() within try: except: blocks

SubjectAuthor
* psycopg2: proper positioning of .commit() within try: except: blocksKarsten Hilbert
`- Re: psycopg2: proper positioning of .commit() within try: except: blocksStefan Ram

1
Subject: psycopg2: proper positioning of .commit() within try: except: blocks
From: Karsten Hilbert
Newsgroups: comp.lang.python
Date: Sat, 7 Sep 2024 15:48 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: Karsten.Hilbert@gmx.net (Karsten Hilbert)
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>
View all headers

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(...)
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
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 read/write dependencies among transactions
DETAIL: Reason code: Canceled on identification as a pivot, during commit 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 not 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 OnInit
frame = gmTopLevelFrame(None, id = -1, title = _('GNUmed client'), size = (640, 440))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/share/gnumed/Gnumed/wxpython/gmGuiMain.py", line 191, in __init__
self.LayoutMgr = gmHorstSpace.cHorstSpaceLayoutMgr(self, -1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/share/gnumed/Gnumed/wxpython/gmHorstSpace.py", line 215, in __init__
self.top_panel = gmTopPanel.cTopPnl(self, -1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/share/gnumed/Gnumed/wxpython/gmTopPanel.py", line 52, in __init__
wxgTopPnl.wxgTopPnl.__init__(self, *args, **kwargs)
File "/usr/share/gnumed/Gnumed/wxGladeWidgets/wxgTopPnl.py", line 33, in __init__
self._TCTRL_patient_selector = cActivePatientSelector(self, wx.ID_ANY, "")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/share/gnumed/Gnumed/wxpython/gmPatSearchWidgets.py", line 1295, 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 read/write dependencies among transactions
DETAIL: Reason code: Canceled on identification as a pivot, during commit attempt.
TIP: The transaction might succeed if retried.

2024-08-20 22:17:04 INFO gm.cfg [140274204403392 UpdChkThread-148728] (/usr/share/gnumed/Gnumed/pycommon/gmCfg.py::get2() #148): creating option [horstspace.update.consider_latest_branch] with default [True]
2024-08-20 22:17:04 DEBUG gm.db_pool [140274459512896 MainThread] (/usr/share/gnumed/Gnumed/pycommon/gmConnectionPool.py::exception_is_connection_loss() #667): interpreting: could not serialize access due to read/write dependencies among transactions
DETAIL: Reason code: Canceled on identification as a pivot, during commit attempt.
TIP: The transaction might succeed if retried.

2024-08-20 22:17:04 DEBUG gm.logging [140274459512896 MainThread] (/usr/share/gnumed/Gnumed/pycommon/gmLog2.py::log_stack_trace() #170): exception: could not serialize access due to read/write dependencies among transactions
DETAIL: Reason code: Canceled on identification as a pivot, during commit attempt.
TIP: The transaction might succeed if retried.

2024-08-20 22:17:04 DEBUG gm.logging [140274459512896 MainThread] (/usr/share/gnumed/Gnumed/pycommon/gmLog2.py::log_stack_trace() #171): type: <class 'psycopg2.errors.SerializationFailure'>
2024-08-20 22:17:04 DEBUG gm.logging [140274459512896 MainThread] (/usr/share/gnumed/Gnumed/pycommon/gmLog2.py::log_stack_trace() #172): list of attributes:
2024-08-20 22:17:04 DEBUG gm.logging [140274459512896 MainThread] (/usr/share/gnumed/Gnumed/pycommon/gmLog2.py::log_stack_trace() #178): add_note: <built-in method add_note of SerializationFailure object at 0x7f942a3c9cf0>
2024-08-20 22:17:04 DEBUG gm.logging [140274459512896 MainThread] (/usr/share/gnumed/Gnumed/pycommon/gmLog2.py::log_stack_trace() #178): args: ('could not serialize access due to read/write dependencies among transactions\nDETAIL: Reason code: Canceled on identification as a pivot, during commit attempt.\nTIP: The transaction might succeed if retried.\n',)
2024-08-20 22:17:04 DEBUG gm.logging [140274459512896 MainThread] (/usr/share/gnumed/Gnumed/pycommon/gmLog2.py::log_stack_trace() #178): cursor: None
2024-08-20 22:17:04 DEBUG gm.logging [140274459512896 MainThread] (/usr/share/gnumed/Gnumed/pycommon/gmLog2.py::log_stack_trace() #178): diag: <psycopg2.extensions.Diagnostics object at 0x7f942a2b9e10>
2024-08-20 22:17:04 DEBUG gm.logging [140274459512896 MainThread] (/usr/share/gnumed/Gnumed/pycommon/gmLog2.py::log_stack_trace() #178): pgcode: 40001
2024-08-20 22:17:04 DEBUG gm.logging [140274459512896 MainThread] (/usr/share/gnumed/Gnumed/pycommon/gmLog2.py::log_stack_trace() #178): pgerror: ERROR: could not serialize access due to read/write dependencies among transactions
DETAIL: Reason code: Canceled on identification as a pivot, during commit attempt.
TIP: The transaction might succeed if retried.

2024-08-20 22:17:04 DEBUG gm.logging [140274459512896 MainThread] (/usr/share/gnumed/Gnumed/pycommon/gmLog2.py::log_stack_trace() #178): with_traceback: <built-in method with_traceback of SerializationFailure object at 0x7f942a3c9cf0>

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

Subject: Re: psycopg2: proper positioning of .commit() within try: except: blocks
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Sat, 7 Sep 2024 17:34 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: psycopg2: proper positioning of .commit() within try: except: blocks
Date: 7 Sep 2024 17:34:11 GMT
Organization: Stefan Ram
Lines: 10
Expires: 1 Jul 2025 11:59:58 GMT
Message-ID: <difference-20240907182905@ram.dialup.fu-berlin.de>
References: <mailman.42.1725724089.2917.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de SWT4KXi+QFSblDH/dhM4eAlbSJpXqM79g2Ns284+2+nwtT
Cancel-Lock: sha1:IADySTfxCP+Ej7qklnN8vixitEU= sha256:rkda6d4fUX8SxAwrRUsGKdhBuQ6mwzULUkQF1F4bQng=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
View all headers

Karsten Hilbert <Karsten.Hilbert@gmx.net> wrote or quoted:
>except PSYCOPG2-Exception:

The expression after "except" seems to be calculating a difference.

(Lately, I've been seeing some super long subject lines around here,
so here's a thought on how this one could've been worded shorter:
"Positioning of a database .commit() within a try block")

1

rocksolid light 0.9.8
clearnet tor