Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sn1eu-000TY7-Qb for psycopg@arkaria.postgresql.org; Sat, 07 Sep 2024 20:03:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sn1eu-003UQC-8N for psycopg@arkaria.postgresql.org; Sat, 07 Sep 2024 20:03:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sn1es-003UQ2-SH for psycopg@lists.postgresql.org; Sat, 07 Sep 2024 20:03:48 +0000 Received: from fout5-smtp.messagingengine.com ([103.168.172.148]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sn1ej-0002OK-Mh for psycopg@lists.postgresql.org; Sat, 07 Sep 2024 20:03:45 +0000 Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfout.phl.internal (Postfix) with ESMTP id 661D81380163; Sat, 7 Sep 2024 16:03:36 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-12.internal (MEProxy); Sat, 07 Sep 2024 16:03:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1725739416; x=1725825816; bh=IErSUF1zyW5nG+6ukDJa66K01CrEb1uyowVA4CMXkXQ=; b= mJDet/Z8c+ogRht/DSkm/epSDJqBvqyNAeAGYYdgbqO6aYiHFX/dfBhneXrtbrZ1 Ow4+BMYljbkIY8bSuFIiMaRx7Ng3bB0Hkqi+ITJopGh2qaHOynhO7lXbVoBWBrl/ J9ykeTugK8BJUeKgkuejjLlPJohxQJuJMrGsp3PxnlXKF68mw42BsPiD0lTRkK98 4SK1H89C2hbcVj+RCoQ6/yxT552NSs9HV0vt+bL+cFFHa7jp14K9P8a1wFv3Kj0A CHMT6lWHrFSU5FUEDdE7cBgZ4s5QPL3v/AaVYLbzfCBe4PES1ahbbuwOgTwsuW/v pc92/tFVsedMHN/NTlN0fg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1725739416; x= 1725825816; bh=IErSUF1zyW5nG+6ukDJa66K01CrEb1uyowVA4CMXkXQ=; b=p LnGgN1gJR0/utWGYUlvTK/jZ/7ewQZAbVcKajoJnfGpdH+eAwh66duOhRMSrD3TA XhDYrvuWNd4nRVEwtIAnajuD9BCFLskyOS3jPFscekxuvhtkxh4h0dTqLDvd8rgv L4L8n+VPRKlaIiRBvHzZnbKMnD0xpgr7lWtqvxzwwTPY3O7XPimiF/wcirXIaIAE pIg5npDXZbPen3PycovWiTKz27B6qXtsXtGkiFA581Et20ymRl/HzGBDNiofUft7 /giKB1NWxcCCdPgcxGbQxaSb+l0QQLkTUk6LAwQs6GaiyJ+XxYoou1tfvbc5g9pF 5kkdxVN6EmUyvADw2wP6w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudeifedgudegkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefkffggfgfuvfevfhfhjggtgfesthejredttddv jeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvg hrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpefhtdefffeijedvhefh teehfeevfeffieffkeffheettdegudffueeigeeguefhueenucffohhmrghinhepphhshi gtohhpghdrohhrghdptghomhhmihhtrdhmhienucevlhhushhtvghrufhiiigvpedtnecu rfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvg hrrdgtohhmpdhnsggprhgtphhtthhopeefpdhmohguvgepshhmthhpohhuthdprhgtphht thhopehkrghrshhtvghnrdhhihhlsggvrhhtsehgmhigrdhnvghtpdhrtghpthhtohepph hshigtohhpgheslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhgpdhrtghpthhtohep phihthhhohhnqdhlihhsthesphihthhhohhnrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 7 Sep 2024 16:03:35 -0400 (EDT) Message-ID: <5ee80b84-f04b-454d-ab39-45572e0751a1@aklaver.com> Date: Sat, 7 Sep 2024 13:03:34 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: psycopg2: proper positioning of .commit() within try: except: blocks To: Karsten Hilbert Cc: psycopg@lists.postgresql.org, python-list@python.org References: <7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com> Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 9/7/24 12:44, Karsten Hilbert wrote: > Am Sat, Sep 07, 2024 at 09:46:03AM -0700 schrieb Adrian Klaver: > > No I don't but - to my understanding - an ongoing transaction > is being closed upon termination of the hosting connection. > Unless .commit() is explicitely being issued somewhere in the > code that closing of a transaction will amount to a ROLLBACK. > > In case of SQL having failed within a given transaction a > COMMIT will fail-but-rollback, too (explicit ROLLBACK would > succeed while a COMMIT would fail and, in-effect, roll back). > > IOW, when SOME_SQL has failed it won't matter that I close > the connection with conn.commit() and it won't matter that > conn.commit() runs a COMMIT on the database -- an open > transaction having run that failed SQL will still roll back > as if ROLLBACK had been issued. Or else my mental model is > wrong. > > https://www.psycopg.org/docs/connection.html#connection.close Which says: " Note that closing a connection without committing the changes first will cause any pending change to be discarded as if a ROLLBACK was performed" That indicates the ROLLBACK is done on the close() not the commit() and only if a commit() was not issued first. NOTE: If you use the with context manager the transaction automatically commits on success and rolls back exception, though it does not close the connection. This is changed in psycopg3 where the connection is closed In the case you show you are doing commit() before the close() so any errors in the transactions will show up then. My first thought would be to wrap the commit() in a try/except and deal with error there. > > In the particular case I was writing about the SQL itself > succeeded but then the COMMIT failed due to serialization. I > was wondering about where to best place any needed > conn.commit(). My knee-jerk reaction was to then put it last > in the try: block... > > All this is probably more related to Python than to PostgreSQL. > > Thanks, > Karsten > -- > GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B -- Adrian Klaver adrian.klaver@aklaver.com