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 1smyai-000B2f-Ja for psycopg@arkaria.postgresql.org; Sat, 07 Sep 2024 16:47:18 +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 1smyZj-0015XG-Gb for psycopg@arkaria.postgresql.org; Sat, 07 Sep 2024 16:46:15 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1smyZi-0015Wf-7l for psycopg@lists.postgresql.org; Sat, 07 Sep 2024 16:46:15 +0000 Received: from fhigh7-smtp.messagingengine.com ([103.168.172.158]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1smyZa-0000u5-Nl for psycopg@lists.postgresql.org; Sat, 07 Sep 2024 16:46:12 +0000 Received: from phl-compute-09.internal (phl-compute-09.phl.internal [10.202.2.49]) by mailfhigh.phl.internal (Postfix) with ESMTP id 03F1111401F2; Sat, 7 Sep 2024 12:46:05 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-09.internal (MEProxy); Sat, 07 Sep 2024 12:46:05 -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=1725727564; x=1725813964; bh=mvp1ckDRWii6rZ5Nv8DNUHV2A4AwN7E1jM6YyD62xQE=; b= 2Fp7mJNormAfdlondKjMiqPwud2URD+G3Qcuoj6LwjMUki8eSgohEiZJ9WsVEMVf Httoz3d+Xuh9Nt9asoEe5vTRQztQdXyB9BlwqWnBlljuNY9CVmuG2h1zfflUAvZY I9M1xcMgPI9Ff0FSgTb/mSlb61gh4Ly+sYYVtomvabMqomqiLVvpI5UdsjxrUa+I PaYw87d79oCXDDDIW3Nr/XZGhk5vIwZPQgPTTJocPvmQfOIQ+79JmOHbAelK/RAI zxp94uuZM66wBEaW+SheTClJuFOMzTukzqHKefbTzbX40XMlF/6JUEhSkfrHNmV6 Xa6suAku52k+IB9hPmn9xw== 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=1725727564; x= 1725813964; bh=mvp1ckDRWii6rZ5Nv8DNUHV2A4AwN7E1jM6YyD62xQE=; b=o hfQW+No4tRipUJhFw/lP3HBkVl3ExyzpHjHAH7gg0aT0iF2wKTte8KvMwnIMD/e6 gz25CN7UsvTr8dujv1NA1Sv/MfUkHwEU7qvtwKd1XyonwIsXWYKYLJEqQbeGUvt5 zuewQ3nGo6XAtAIxB6QwsKCmXjrjGtDD6QCWMou9cqOzMqgCHod+LTt+QaXApnq0 eEW1wL2y/FMGRRrWKlf1IHaZ7/Koj9l7X29cqWi2MZykKT3/S7blbC+XK55dHmqK zIlfUbWNcwVn3V0+g4EW3uddatDB5i6dUJYvfvAmdfiVGtDqKsyWMWdkuTshAEnY ISaiOfUnuY1iC0Nvm7ndg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudeifedguddtjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefkffggfgfuvfevfhfhjggtgfesthejredttddv jeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvg hrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpefhudfgtedtgfetgfeh jeduteevgeehtdekfedttedvffdvhefhheelheegtdeffeenucffohhmrghinhepphhshi gtohhpghdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhl fhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmpdhnsggprh gtphhtthhopeefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehkrghrshhtvghn rdhhihhlsggvrhhtsehgmhigrdhnvghtpdhrtghpthhtohepphhshigtohhpgheslhhish htshdrphhoshhtghhrvghsqhhlrdhorhhgpdhrtghpthhtohepphihthhhohhnqdhlihhs thesphihthhhohhnrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 7 Sep 2024 12:46:04 -0400 (EDT) Message-ID: <7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com> Date: Sat, 7 Sep 2024 09:46:03 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: psycopg2: proper positioning of .commit() within try: except: blocks To: Karsten Hilbert , psycopg@lists.postgresql.org Cc: python-list@python.org References: 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 08:48, Karsten Hilbert wrote: > 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(...) In the above do you have: https://www.psycopg.org/docs/extensions.html#psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE Or is that in some other concurrent transaction? > 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 It will if you use with conn:, otherwise it up to you to do the rollback() Are you are doing a rollback() in except PSYCOPG2-Exception: ? -- Adrian Klaver adrian.klaver@aklaver.com