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 1sn3cc-000hdT-Jb for psycopg@arkaria.postgresql.org; Sat, 07 Sep 2024 22:09:35 +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 1sn3cc-004qm5-7i for psycopg@arkaria.postgresql.org; Sat, 07 Sep 2024 22:09:34 +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 1sn3cc-004qlx-1A for psycopg@lists.postgresql.org; Sat, 07 Sep 2024 22:09:34 +0000 Received: from fhigh3-smtp.messagingengine.com ([103.168.172.154]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sn3cX-0003IK-JN for psycopg@lists.postgresql.org; Sat, 07 Sep 2024 22:09:33 +0000 Received: from phl-compute-02.internal (phl-compute-02.phl.internal [10.202.2.42]) by mailfhigh.phl.internal (Postfix) with ESMTP id 3BC411140127; Sat, 7 Sep 2024 18:09:29 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-02.internal (MEProxy); Sat, 07 Sep 2024 18:09:29 -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=1725746969; x=1725833369; bh=Q+QC1/jjtJMoHJ7EbcWvLOuc2b2PhZ3yLQpwiFHaPjA=; b= 15dS1sQltWWbrQf2IsbdZSTqHyS+5KUvm465mzxACDwLBeBks/TdiHdn6B7jYZBe RHQROx8dshgovi1RI5t8WGXp/XO3aYx4+9hQSbgKi/f6qZno6o0ah4qXwUU6xP4Q C80KJMpMEikZPPdPQNsaOllGmxw3032xVm/TXtEDvKZsz7U4EexPzqXjnn1L7MbD n1RdAOjqZDBBop3SCzvzbrDmtnEuKA76SjSBt3mj8m4kTGeiHM/BqupaeZtXgs6G YVSDz9lx/6CVPIzA/wRp5xESObpSx/PWlrDyKkVmQq+XnZh7YHI8YEG+jJjnvYlj a2JtPTwjuRLQFuOmRN3pVQ== 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=1725746969; x= 1725833369; bh=Q+QC1/jjtJMoHJ7EbcWvLOuc2b2PhZ3yLQpwiFHaPjA=; b=l 6ar+k2/e6T3Cho5d4hRu+c+2FSTYgLg1V/blevwbYLN46/V3P+GMqdTr+sK8opP/ jIYYKwLZmNAqkt/mZRSQbXpfZ2uu3OpBjZKmFiNgBvr2YSsf2hfP1JByTrZjYMBT zjJvuKn3KejHfk73J1yHbsGmIbPAtO3iK9S2ZqPJcxVZ9RH/IHECzA9Q7KRws7nC 4ioTLBxOYlcta58PNCqcin3Fgrn6v/NwNHwyUdcgk3FCO+kYjjYFwXR0LNUT1tJA RktHNVDGVQ58ws6tw01BoxyTF8BOA89cP/VkkfcVX/a2DYPxhBr7OW4/QHHisHke r6m6AOS8HAfNtaiVkChJg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudeigedgtdehucetufdoteggodetrfdotf 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 18:09:28 -0400 (EDT) Message-ID: <75723bad-7914-4728-a567-a061e4d3c7d6@aklaver.com> Date: Sat, 7 Sep 2024 15:09:28 -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:59, Karsten Hilbert wrote: > Am Sat, Sep 07, 2024 at 02:47:49PM -0700 schrieb Adrian Klaver: > >>> It is also insufficient because the .commit() itself may >>> elicit exceptions (from the database). >> >> Yeah with Serializable that is part of the package: > > No complaints :-) > > >>> 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. > > Perhaps my pseudo-code was to abbreviated. > > conn = psycopg2.connection() > curs = conn.cursor() > curs.execute(SQL) > curs.close() > conn.commit() > conn.close() > > Written more safely: > > conn = psycopg2.connection() > curs = conn.cursor() > try: > curs.execute(SQL) > except SOME_PG_EXCEPTION_VIA_PSYCOPG2: > some_panicstricken_logging() > curs.close() > try: > conn.commit() > except SOME_PG_EXCEPTION_VIA_PSYCOPG2__SUCH_AS_SERIALIZATION_ERROR: > some_more_of_the_panicstricken_logging() > conn.close() > > now factored out: > > def __commit_me_logging_errors(commit_func): > try: > commit_func() > except SOME_PG_EXCEPTION_VIA_PSYCOPG2__SUCH_AS_SERIALIZATION_ERROR: > some_more_of_the_panicstricken_logging() > return > > conn = psycopg2.connection() > curs = conn.cursor() > try: > curs.execute(SQL) > except SOME_PG_EXCEPTION_VIA_PSYCOPG2: > some_panicstricken_logging() > curs.close() > __commit_me_logging_errors(conn.commit) > conn.close() > > More clear ? Yes. > > Karsten > -- > GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B -- Adrian Klaver adrian.klaver@aklaver.com