Deutsch English Français Italiano |
<mailman.47.1725793582.2917.python-list@python.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!news.swapon.de!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:06:19 +0200 Lines: 46 Sender: <karsten.hilbert@gmx.net> Message-ID: <mailman.47.1725793582.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> <Zt2FK_zt9csPjhB-@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 utEjq6I/YcX4ftXohZAJnQTHV6xUDUT0oOkJs1ua8Jug== Cancel-Lock: sha1:ciWz8bqObCd+Ss+im0yxqZbYS/0= sha256:WQXt4ygyoqAv0opiARlRv7Skrsw3HYpzHX3KbtlYX8s= 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=lvdyDcOH; dkim-adsp=pass; dkim-atps=neutral X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'fairly': 0.05; 'received:212.227': 0.07; 'sun,': 0.07; ':-)': 0.09; 'gpg': 0.09; 'karsten': 0.09; 'schrieb': 0.09; 'log': 0.12; '1713': 0.16; '2024': 0.16; 'commit': 0.16; 'ensures': 0.16; 'greg': 0.16; 'hilbert': 0.16; 'ignored': 0.16; 'pythonic': 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; 'wrong': 0.28; 'am,': 0.31; "doesn't": 0.32; 'sep': 0.32; 'but': 0.32; 'header:In-Reply-To:1': 0.34; 'possibly': 0.36; 'error,': 0.38; 'data.': 0.40; 'gone': 0.40; 'something': 0.40; 'should': 0.40; "there's": 0.61; 'me.': 0.62; 'received:212': 0.62; 'everything': 0.63; 'skip:r 20': 0.64; 'transaction': 0.64; 'your': 0.64; 'well': 0.65; 'see,': 0.76; 'database': 0.80; '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=1725793580; x=1726398380; i=karsten.hilbert@gmx.net; bh=e2sc6zm0w6XIqkHGQ23TTzSbKZlmC8/y8M3vq0nmwQo=; 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=lvdyDcOH7r5jr+6y45HhLt/Gn2pt8luv7Fv4MZjWv04SY35fqn1W7Cx7OUJ9HndC tTJGiuffzN84wSPPSRgnxNL6Hq4IATu7MrUrygGM0Cbk5ndXXPyIKtLPI8LTWv43O jciEd/3YlSXWhTCHrOIT/WbIyi3dhPJQRdpBqHESblYSd/NQCl1ZA7Xq+1w1H45q2 Tv3XhY9Y4uVk/CwoeeCVQthMTf/mtz8pa6Hc5EfFC0hvze9hD7xf7ESpItTdzA5Vm 2h3w17Zyg31eN/KXgqdpF6J+yH0vKDM6DbnnqsnP++lySeOv4ThcQrGiGLkzHm6uV hGd+irXL0+NgJVhahg== 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:jpNBu5bcziW2im/LUnxmtuDo2P1sYWKNu6T1IwwvXPb4EWrYH8l W/W+oZAIyDY/LL7C7LAZ1pt2vutZbszTCpFViXnl8uVot/ECCJzsktjJw972aUbczOMYbqW dYTnt1K8X0ldOaumXWIJC3e1rY5mwJnQtv4i3WIbg9aBdPEB3JrHMsB9vFF+jw24ft8odQI 7YdjSfnTFcg2eT3GDNl1w== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:v6XDTGl6Sds=;ZjayO9rBMpQ5JX7588jERYTm6Me Rj9Zx6VUUt0IaeKLYnYQhCU91bumf11h1+cXiREGS17bryfx6ijdyVb9qWuOUXGkl++jABF1s njmR0J+gtKjbG2b0RB5a5Gi6MCL7T1Z5H8fUed8ehr/RQjvwJcDZ17jTXigop0QsgUUrjtFni LONPWSuIBJAVj3cx2yTaFJDU/bckTHIxGevGLTCrxSYyUhDSoZsrHfT9PXEFpZdLxmvXVyj8Y ttIB6pdtsdNJjkCJPEBkIkbiXb7j1K5SFgi9vTLvjPUlXZC+02qmqqq0vc1Smdf7HOMsfbr5y +sro0crriqr9GgLTD/NKzd6qJi/0fbktaOgDOrQBkk8t/u+TtcVdFqiAbUJkAMsZXpz6dAfeu wCi6Y1m9gBlroZ0x86IJybja9jKWyDgceIBTb65JVEE1HDPU7Tsqq9SfZsF/901WaH8MPWIdj 1927HhhDUJiCDiRXBRQhdGzj6cAexfhR6sTltsMIpVi41I8H9CPk7koTmolqJN0fHgohi2nGG UnWvrKGjLjTTWRptC9O53Nl+aGK/T42f1bQSIre9bnOxjouLRSQxaGlAKlFCc9WYyYrYv23qY 0QYo3b6IcKUObd66SfBqbbsxRo2rjasuk+5AUewxcRIRCh4SYCfBfWjw8jarQQ1Hi10kxcwv3 P/V5Zal7yToaOZxz3bxys8fTz/VCHpwzGp5dWEILwawBJQ4Ws9YUxDOBGE+NoBxCqcR4aWsQu wU+DGm6XasZDXR3qdfTWlDo5g+Zi8yBZ60qvmIFign1RZhaDW62X/KG8sKlnlA1W7tdyM8JSA dR9vZK6IiYLHbgOxEv38sF8w== 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: <Zt2FK_zt9csPjhB-@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: 7375 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. > > 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 issued? The database doesn't know something > has gone wrong, so it will happily commit a partially-completed > transaction and possibly corrupt your data. A-ha ! try: run_some_SQL_that_succeeds() print(no_such_name) # tongue-in-cheek 1 / 0 # for good measure except SOME_DB_ERROR: print('some DB error, can be ignored for now') finally: commit() which is wrong, given that the failing *Python* statements may very well belong into the *business level* "transaction" which a/the database transaction is part of. See, that's why I was asking in the first place :-) I was overlooking implications. Karsten =2D- GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B