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 1sn3Hh-000fZ3-44 for psycopg@arkaria.postgresql.org; Sat, 07 Sep 2024 21:47:58 +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 1sn3Hf-004cBc-Vd for psycopg@arkaria.postgresql.org; Sat, 07 Sep 2024 21:47:55 +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 1sn3Hf-004c36-OC for psycopg@lists.postgresql.org; Sat, 07 Sep 2024 21:47:55 +0000 Received: from fhigh2-smtp.messagingengine.com ([103.168.172.153]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sn3Hc-000315-Fi for psycopg@lists.postgresql.org; Sat, 07 Sep 2024 21:47:54 +0000 Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfhigh.phl.internal (Postfix) with ESMTP id AF1A511400CD; Sat, 7 Sep 2024 17:47:50 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Sat, 07 Sep 2024 17:47:50 -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=1725745670; x=1725832070; bh=fZ6RkLnh4N36EChcYseze39K3iEn0bvma4R0Gfrmd3Q=; b= R6zzVGa8q0uBWWpGdXITUvlMpC+1mD8+fN7adKfi5/1JuuQK4nIJr0hs6h6bRaJF WFU5MrX9Jflr9HDeFw31TzmgTWu2wH7kEAiomFzXJYmmsXOcj6NUjn1BobXBUq2K LWy+roNVtst2/y7Uaa+ZEaPABziaHQlM3sBcALyMscOJZfdtUKHkUQmGSEjyHRPX zUvdAt3gkHWKApzfGLwec7PKjDDT8vrL9fRyu0ErWC9D6Gkih9+/XjZD+EnTDqzN XkAnFRc91nhca/7j5JCBjOyydppYMBzYWUpnKZVUXDDHW3YaeiWlPXOZJSclVTLt 17AJFwN/+L5KH8+ZGwSQkw== 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=1725745670; x= 1725832070; bh=fZ6RkLnh4N36EChcYseze39K3iEn0bvma4R0Gfrmd3Q=; b=p 3uSKdyDoA1qOPyL1pFCUA6DNlc6xN9ZFLUrQkwzaypZZTYqHxGkAQn+wBH63yv2z ieCJK5EHAgsbtH1EE++0mBSoe8iXEvIyXwz6XYrjrAU05YRDnR7kAFL4eBGo+4Zv lR3XcY3fg5xhBbLigQZpYTMjOjShFccenD06VGzlHIvVrPoBe2iiCnPlCYXv447h QMWLbFBHXkl0hdJUsovwfY864jtPenUYUktNwVMQn3zPqjSUOQi32UM053XK/ei5 sDtzWhqjeOZMgoVbbUguOTtmnnhSaaBcJ8JljmjAGpYRwOYSV01xQimfzphoCdIa 4p9UgLpP2rz4b96XR2Fng== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudeigedgtdduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepkfffgggfuffvvehfhfgjtgfgsehtjeertddtvdej necuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrh esrghklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhephfeviefhveelffeftdeh udekveefhfeftdegieefveetfffgfeehtdfftedutedtnecuvehluhhsthgvrhfuihiivg eptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghk lhgrvhgvrhdrtghomhdpnhgspghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpd hrtghpthhtohepkhgrrhhsthgvnhdrhhhilhgsvghrthesghhmgidrnhgvthdprhgtphht thhopehpshihtghophhgsehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 7 Sep 2024 17:47:49 -0400 (EDT) Message-ID: Date: Sat, 7 Sep 2024 14:47:49 -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 References: <7cb50df2-9c76-477f-91c9-e149c7637104@aklaver.com> <5ee80b84-f04b-454d-ab39-45572e0751a1@aklaver.com> <4a1b12fc-24b7-4c7e-b1f2-6ec9c5f341c2@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 14:20, Karsten Hilbert wrote: > Am Sat, Sep 07, 2024 at 02:09:28PM -0700 schrieb Adrian Klaver: > >>> Right, and this was suggested elsewhere ;) >>> >>> And, yeah, the actual code is much more involved :-D >>> >> >> I see that. >> >> The question is does the full code you show fail? >> >> The code sample you show in your original post is doing something very different then >> what you show in your latest post. At this point I do not understand the exact problem >> we are dealing with. > > We are not dealing with an unsolved problem. I had been > asking for advice where to best place that .commit() call in > case I am overlooking benefits and drawbacks of choices. > > The > > 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. Elegant is nice, but correct is better:) > > It is also insufficient because the .commit() itself may > elicit exceptions (from the database). Yeah with Serializable that is part of the package: "While PostgreSQL's Serializable transaction isolation level only allows concurrent transactions to commit if it can prove there is a serial order of execution that would produce the same effect ..." > 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. I'm not following, if you do that then you won't have a commit. > > Best, > Karsten > -- > GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B -- Adrian Klaver adrian.klaver@aklaver.com