Warning: mysqli::__construct(): (HY000/1203): User howardkn already has more than 'max_user_connections' active connections in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\includes\artfuncs.php on line 21
Failed to connect to MySQL: (1203) User howardkn already has more than 'max_user_connections' active connections
Warning: mysqli::query(): Couldn't fetch mysqli in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\index.php on line 66
Article <mailman.50.1725804833.2917.python-list@python.org>
Deutsch   English   Français   Italiano  
<mailman.50.1725804833.2917.python-list@python.org>

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

Path: ...!feeds.phibee-telecom.net!2.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: Sun, 8 Sep 2024 16:13:49 +0200
Lines: 301
Sender: <karsten.hilbert@gmx.net>
Message-ID: <mailman.50.1725804833.2917.python-list@python.org>
References: <Ztx1sZMGTnA3eLP1@hermes.hilbert.loc>
 <7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com>
 <ZtytJMhyvtExPxfF@hermes.hilbert.loc>
 <5ee80b84-f04b-454d-ab39-45572e0751a1@aklaver.com>
 <Zty7ZCMwKQl4C4Id@hermes.hilbert.loc>
 <4a1b12fc-24b7-4c7e-b1f2-6ec9c5f341c2@aklaver.com>
 <ZtzDubguRDWXwV-t@hermes.hilbert.loc>
 <62133ebd-a4a3-471b-9acc-3a988b4fcbd7@btinternet.com>
 <Zt2xHedGIh1-dL4g@hermes.hilbert.loc>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de T456cBZlhc3vdD1zp+axhA0/tzn/U3QdOpWjh/7e+/YA==
Cancel-Lock: sha1:Y+NT4xy67pw1Nr/H4wta+51A4wg= sha256:DiZg1R1vac8EZnzqY3L4XGh6wRjplmtrj1JO3MW+Noc=
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=jEko3wip; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'despite':
 0.05; 'explicitly': 0.07; 'have,': 0.07; 'received:212.227': 0.07;
 'sun,': 0.07; 'thing.': 0.07; 'wrong.': 0.07; '"""': 0.09; 'elif':
 0.09; 'else:': 0.09; 'finalized': 0.09; 'gpg': 0.09; 'karsten':
 0.09; 'schrieb': 0.09; 'skip:_ 20': 0.09; 'something,': 0.09;
 'though.': 0.09; 'typically': 0.09; 'log': 0.12; "'':": 0.16;
 '(it': 0.16; '1713': 0.16; '2024': 0.16; 'args:': 0.16; 'assert':
 0.16; 'be)': 0.16; 'column': 0.16; 'commit': 0.16; 'consolidate':
 0.16; 'database.': 0.16; 'dicts': 0.16; 'executed': 0.16;
 'failed.': 0.16; 'fetching': 0.16; 'indentation': 0.16; 'lambda':
 0.16; 'none)': 0.16; 'none:': 0.16; 'queries.': 0.16;
 'received:212.227.15': 0.16; 'skip:_ 60': 0.16; 'subject:() ':
 0.16; 'subject:proper': 0.16; 'subject:try': 0.16; 'tuple': 0.16;
 'usefully': 0.16; 'problem': 0.16; 'python': 0.16; 'instead':
 0.17; 'to:addr:python-list': 0.20; 'exception': 0.22; 'returns':
 0.22; 'skip:_ 10': 0.22; 'version': 0.23; 'code': 0.23; 'skip:p
 30': 0.23; 'run': 0.23; 'stuff': 0.25; 'cannot': 0.25; 'seems':
 0.26; 'pattern': 0.26; "isn't": 0.27; 'bit': 0.27; 'function':
 0.27; 'done': 0.28; 'suggest': 0.28; 'whole': 0.30; 'skip:_ 40':
 0.31; 'default': 0.31; 'effect': 0.31; 'raise': 0.31; 'false,':
 0.32; 'privilege': 0.32; 'returning': 0.32; 'sep': 0.32; 'unless':
 0.32; 'but': 0.32; "i'm": 0.33; 'there': 0.33; 'mean': 0.34;
 'skip:" 20': 0.34; 'header:In-Reply-To:1': 0.34; 'running': 0.34;
 'failed': 0.35; 'close': 0.35; 'thanks,': 0.36; 'source': 0.36;
 'missing': 0.37; "skip:' 10": 0.37; "it's": 0.37; 'skip:_ 30':
 0.37; 'put': 0.38; 'quite': 0.39; 'single': 0.39; 'list': 0.39;
 'block': 0.39; 'level.': 0.40; 'place.': 0.40; 'something': 0.40;
 'try': 0.40; 'should': 0.40; 'connection': 0.61; "there's": 0.61;
 'skip:i 20': 0.62; 'received:212': 0.62; 'true': 0.63; 'feel':
 0.63; 'skip:r 20': 0.64; 'full': 0.64; 'skip:g 30': 0.64;
 'transaction': 0.64; 'well': 0.65; 'less': 0.65; 'lead': 0.67;
 'skip:t 30': 0.67; 'right': 0.68; 'del': 0.70;
 'charset:iso-8859-1': 0.73; 'business': 0.77; '1st': 0.78;
 'field': 0.78; '3rd': 0.81; 'queries': 0.81; 'returned': 0.81;
 'extra': 0.84; 'eventually': 0.84; 'exceptions': 0.84;
 'repetitive': 0.84; 'rob': 0.84; 'subject:within': 0.84;
 'transaction.': 0.84; 'true:': 0.84; '"do': 0.91; 'replacing':
 0.91; 'retained': 0.91; 'will,': 0.91; 'skip:q 20': 0.95;
 'positions': 0.96
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net;
 s=s31663417; t=1725804830; x=1726409630; i=karsten.hilbert@gmx.net;
 bh=zlVTvCww1ujSZaYXWBd7ICSmhX3L9VWK7ALBG86s+ts=;
 h=X-UI-Sender-Class:Date:From:To:Subject:Message-ID:References:
 MIME-Version:Content-Type:Content-Transfer-Encoding:In-Reply-To:
 cc:content-transfer-encoding:content-type:date:from:message-id:
 mime-version:reply-to:subject:to;
 b=jEko3wipdJtt/6dgrDKejOpI8ZBHYGKRYbIMpCw3F3Tvl5TJ9nADyVb94LbIFxwR
 yKxUlErBGsp0ufxqqRD5PSvaHk7NKvfQJCqs9r+14+j20RIfY6tIb6o4byL9Ou0RF
 9iH94fotXmEfIEhLwpKHhKC4Cnu3x9w7ff570k35a8rJjP2kX1bANUu72pxgTWENA
 X4AcThdDXYBfxJIKAlJ+ulcVoizZ32Sm4dDSSjKbSnJZN1IxELdd0eOIwyPW22n+C
 1LZYutmIl5LgMPRtDRPmJaPOV2H9EsJd4VSLlF+XZmtZ0CMuuxH+2RQ/X97578OcS
 hSGSt0ugR85x7QOLrQ==
X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a
Content-Disposition: inline
In-Reply-To: <62133ebd-a4a3-471b-9acc-3a988b4fcbd7@btinternet.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:ux0uNH2lgMJzfEAVAvzw0wURuYq2qhHQoM8dYJIA2OwtbJkSepq
 2z3DJ7Dj1LxMxa20UHIGo8t4Cn1LyV1f++/YpTFL+7uNQMgI5u6IxMkBcupZYmOjSiZoYz5
 CiE4n7abO/L++c7BngoUaT0yN0LQTwdC04hkjrDWlEhQWlgsFJePimpT/2SPscekCunbAxW
 6qL8c9/hmMYD4+M0cBlmw==
X-Spam-Flag: NO
UI-OutboundReport: notjunk:1;M01:P0:icBKUh5YZis=;JFqsNok+bj3fwpso/zZcQp9XsCf
 ddFNoMEKQPiLxKvypNtUFNrZ/zH0iwLQyRI6Vr4DUoOyKaNqAePTyFb8RPOY3Hv/58iCZPK/q
 oGqjVGGWPZyJc74FxG9Y+12OnWqKtF9NzZXTyt8bGPOMJdUgAdHqvoDns9fjSKcqT171JcZeP
 G8fM+vJYBbOtE8el+FLvgoJFdmTWqUa5AALlBCIqs0Qp05ezWiSCcnKysSnoPkeWkATyU0k/p
 bA2GcOqjBxLUkSGBXBaMjN6ePcd7W3c/ZOod4Su3sL0Alc+DyD88xgkO3mbG1LyAlMLubL2hc
 HtJMCpZmkKRlIr35OGpmx55n7I/wlWWiPh0MOaMPg0rUVdratYCg0FjslYoo73vu/11gMewIL
 wYsMlfMdiC7UFW4opw9pi2fjf2CPWVcvNXD0jMywDnzjpXSukV1F4IWvwB7Efmze+1WjkGHWz
 +ME5gKsczROHskoBsDxV1nMU7S58T0hsbvsXMSMWqgbr6R7hCoiNQsMwLRwGrjrSwTxPoLddX
 b1SIIUHThqNjauu0iWHNkYVIsQn50dN7v10rdlEN53RizergFXyUSOfcuO2sF30jONL5yRoEW
 dSnRvIFTnY55i7KSQQu3j5yflI0iJ9g9Jc9Lh0tUXpX6Z2mto96PiIkljm1QvW+z1Lye7tm+/
 e93vcM2Cg0Z7s9iwJnAy/7OP/Fq7fWDHye0LdOErHCHrH0rg8W3GFdhOPnSEZecnmnw0AvQzJ
 96GRmrDrjhQ1R1PUidQgMmbKRhVlUMP1POQLNJsOeXHA32CqwacrSJP1fe2/Xj0grZ6QeZHAg
 1eRW3wegEtm5S2F6e1wdgTaw==
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: <Zt2xHedGIh1-dL4g@hermes.hilbert.loc>
X-Mailman-Original-References: <Ztx1sZMGTnA3eLP1@hermes.hilbert.loc>
 <7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com>
 <ZtytJMhyvtExPxfF@hermes.hilbert.loc>
 <5ee80b84-f04b-454d-ab39-45572e0751a1@aklaver.com>
 <Zty7ZCMwKQl4C4Id@hermes.hilbert.loc>
 <4a1b12fc-24b7-4c7e-b1f2-6ec9c5f341c2@aklaver.com>
 <ZtzDubguRDWXwV-t@hermes.hilbert.loc>
 <62133ebd-a4a3-471b-9acc-3a988b4fcbd7@btinternet.com>
Bytes: 16225

Am Sun, Sep 08, 2024 at 02:58:03PM +0100 schrieb Rob Cliffe via Python-lis=
t:

> >Ugly:
> >
> >	try:
> >		do something
> >	except:
> >		log something
> >	finally:
> >		try:
> >			.commit()
> >		except:
> >			log some more
> >
> >Fair but not feeling quite safe:
> >
> >	try:
> >		do something
> >		.commit()
> >	except:
> >		log something
> >
> >Boring and repetitive and safe(r):
> >
> >	try:
> >		do something
> >	except:
> >		log something
> >	try:
> >		.commit()
> >	except:
> >		log something
> >
> >I eventually opted for the last version, except for factoring
> >out the second try: except: block.

> Unless I'm missing something, the 1st & 3rd versions always do the commi=
t() even after
> the first bit fails, which seems wrong.

Well, it does run a Python function called "commit". That
function will call "COMMIT" on the database. The end result
will be correct (at the SQL level) because the COMMIT will
not effect a durable commit of data when the SQL in "do
something" had failed.

We have, however, elicited that there may well be other
things belonging into the running business level transaction
which may fail and which should lead to data not being
committed despite being technically correct at the SQL level.

> I suggest the 1st version but replacing "finally" by "else".=A0 Then the=
 try-commit-except
> will not be executed if the "something" fails.

The whole point was to consolidate the commit into one place.
It is unfortunately named, though. It should be called
"close_transaction".

> Perhaps the extra indentation of the second try block is a bit ugly, but=
 it is more
> important that it does the right thing.
> If it is convenient (it may not be) to put the whole thing in a function=
, you may feel
> that the follwing is less ugly:

The whole thing does reside inside a function but the exit-early pattern
========== REMAINDER OF ARTICLE TRUNCATED ==========