Deutsch English Français Italiano |
<mailman.48.1725794019.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: Re: psycopg2: proper positioning of .commit() within try: except: blocks Date: Sun, 8 Sep 2024 13:13:37 +0200 Lines: 50 Sender: <karsten.hilbert@gmx.net> Message-ID: <mailman.48.1725794019.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> <mailman.46.1725744071.2917.python-list@python.org> <lk4ajkFbbnrU1@mid.individual.net> <Zt2G4e7cwTbP3K_b@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 iSr9Izy2/3IPcIwjVXZJVQLQQI3lwGjSHmwYUPmWF5JQ== Cancel-Lock: sha1:Q9ZVRE5otadAQxvFRfrRhY4PZgM= sha256:jg9lNSr+AKjfyb+d+6Ii5UtOE5RCG22WCeWDxBEdkRM= 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=t+xxQnGL; dkim-adsp=pass; dkim-atps=neutral X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; 'fairly': 0.05; ';-)': 0.07; 'received:212.227': 0.07; 'sun,': 0.07; 'fact,': 0.09; 'fails': 0.09; 'fault': 0.09; 'gpg': 0.09; 'implicit': 0.09; 'karsten': 0.09; 'reasoning': 0.09; 'schrieb': 0.09; 'log': 0.12; '1713': 0.16; '2024': 0.16; 'commit': 0.16; 'ensures': 0.16; 'explicit': 0.16; 'greg': 0.16; 'hilbert': 0.16; 'interrupted': 0.16; 'missed': 0.16; 'pattern:': 0.16; 'pythonic': 0.16; 'reason.': 0.16; 'right.': 0.16; 'subject:() ': 0.16; 'subject:proper': 0.16; 'subject:try': 0.16; 'wrote:': 0.16; 'problem': 0.16; 'reached': 0.17; 'to:addr:python-list': 0.20; 'exception': 0.22; 'code': 0.23; 'seems': 0.26; 'thinking': 0.28; 'wrong': 0.28; 'am,': 0.31; "doesn't": 0.32; 'sep': 0.32; 'but': 0.32; 'header:In-Reply-To:1': 0.34; 'case.': 0.40; 'something': 0.40; 'should': 0.40; "there's": 0.61; 'me.': 0.62; 'received:212': 0.62; 'everything': 0.63; 'transaction': 0.64; 'your': 0.64; 'back': 0.67; 'matter': 0.68; '....': 0.76; 'exceptions': 0.84; 'handled': 0.84; 'subject:within': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net; s=s31663417; t=1725794017; x=1726398817; i=karsten.hilbert@gmx.net; bh=pzWfQV3m3tpEO36Tt+SUnJXJj1+EUjOYYk557jG9liQ=; h=X-UI-Sender-Class:Date:From:To: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=t+xxQnGL1Qki4TQcQElXJp3cNZBgqnoey5sPZVMP8xocdDST2B3J6iqYwxR8iYD+ Q7d5m61FAk7WMs4GCyID5kB+TkC33dBK+v3SQLpxvZYfccNOUpUrxEquanec1wP+y 0TMtu2CIFKzFIUQDkX87JvpVszyLLwYI5nmAOmfDoM1TunGr/Bf8zHz7PkXU56gxc pZpJuQQelYgImVusNA/kZf7sByIqpqKEtR5rMWtBWTKfwuaiNvTjMz5ogQqyk44YH ZCiOJvfJJym3S5OZ6lHy5Kr0+ME5EuMisTmRiptoJAdMTyjr3dmBvfQ2Cw1Ekulmt NAtnJL91pF9uhN5dJQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Content-Disposition: inline In-Reply-To: <lk4ajkFbbnrU1@mid.individual.net> 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:9iAgHzHLHbiAL7ME3rN5jV8rUrxyGs9NC5N8YeoFe24uQ+pFmOH rQzsYXHMQRdPKhQIhIymLpzQtZpfqQhxXApmWgVX95HvfJyY5D6Mxg2KJvoNOdbAEc14mid QouTbBA87wEOF7LbNSNzKXc5wU8bL1VCXZltYf6sZwPPlx0Mlk2lo9ZwqBRy6rhsxLKuFjS PyTVJcHJWoqYwYbfdfDZA== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:zSdovzJbhog=;X+2TY6ZH3XEZ5NjhUtRkRWfhFcE Q88swJdHy8ZazOpbQNzH4NJL2dkVxNeLSWVGIfj/w3eK+V/KC7rkXonJH91uAh4iBb1Lz8XFt TkXgKKRPe+zXWtwbtZB+OZVC5clx+6U+u9AWMa6Fv6nB2pKi5kY34gnyKL5bYnjsEsC1tzW1v JisERndwkSFdCU7ZwtRUgC6mZekYCr4IbjbCTEo/cRg9znzQXes7aWInHQjPMWJAQrsex8Kn3 5QKDqv6gBSD1CGdnXORA3wLvgyBN9FzMY1LdA8ngWQIrjytIMyvk6BafBXXpzF+W/e24cXyaI zC7Uze6NA7GN/HViJ6GXCpxDSpyXyKNl8O4lvXSOs/D6P63ciw+NV4DliK/zq5VZw7oWGA5Rz iKTlDIexienaYsx3uIjZbPPeWwH0v7hsbLWp3gLaKxN7oyoNoAXQ3Z5vZA02xQj7SKLBLbRZ9 gvlblydlgvy+XMfS/uK7QnzXqAEUdEdIfbXZJxXdqfCPg5TmdGHExEseBMBZc6F7Qjbb+70zD xWAT27GEEebBEQztX8fzyJpvIC2+p6VnxRPXiOP0B2L0qtOjyvcZmUSpPJ2NuHNpJJW5q6LtK DJxDAHocKEdTkJjnfEu2IqWKj87+mITnFgfmWpIpkOH88oiYgasRrEo2A2jkiRrkQHjfzPNRg emB8KPVOycI7jUqZvjyiDjkIl+Ygn+bsgoOrbWP0M1lQ9TuJOA8TXtnHhVzZanujFWi35HNVr xG2t0PHMhZSuivy5PMsL0+v0CrsJeH0clvMV8YXzAkQcYV7WQKzVEOLBsN/izqZfoUPJhikDe MfhDM35F6hfO6f32yr1A48Xg== 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: <Zt2G4e7cwTbP3K_b@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> <mailman.46.1725744071.2917.python-list@python.org> <lk4ajkFbbnrU1@mid.individual.net> Bytes: 7578 Am Sun, Sep 08, 2024 at 12:48:50PM +1200 schrieb Greg Ewing via Python-lis= t: > 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 =2D- 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 =2D- GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B