Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k2tC5-0004Wt-8G for pgsql-general@arkaria.postgresql.org; Tue, 04 Aug 2020 09:25:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k2tC4-00014v-88 for pgsql-general@arkaria.postgresql.org; Tue, 04 Aug 2020 09:25:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k2tC3-00014o-Tu for pgsql-general@lists.postgresql.org; Tue, 04 Aug 2020 09:25:12 +0000 Received: from post.visena.com ([46.226.10.50]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.92) (envelope-from ) id 1k2tC1-00044J-8c for pgsql-general@lists.postgresql.org; Tue, 04 Aug 2020 09:25:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=visena.com; s=20141101.wh; h=Content-Type:MIME-Version:Subject:References:In-Reply-To:Message-ID:Cc:To:From:Date; bh=v7MUjF2DcX2+1mFDABYwK/oYqAyq01TeZbrLqrMkwOg=; b=Nt8nr0Z+XEZme2Ow9gTHjJHR+16d0sMNvhAJfd5d9iUb58vKtIWwqqG5gzbViCZ1RifvW4+as95JBQlMa/0GQ2NEVZsfz3gBtJQNesLHypekfqyNNR41jZfKqUPCT1NisSUmfBBjBy9G6OzOOAcPFolMlL/+fEs4ZCEnXU7YUBY=; Received: from [10.0.1.10] (helo=tc7-visena.wh.internal.visena.com) by post.visena.com with esmtp (Exim 4.82) (envelope-from ) id 1k2tBy-0001xh-UA; Tue, 04 Aug 2020 11:25:06 +0200 Received: from localhost ([127.0.0.1] helo=tc7-visena.wh.internal.visena.com) by tc7-visena.wh.internal.visena.com with esmtp (Exim 4.90_1) (envelope-from ) id 1k2tBy-0000DI-SM; Tue, 04 Aug 2020 11:25:06 +0200 Date: Tue, 4 Aug 2020 11:25:06 +0200 (CEST) From: Andreas Joseph Krogh To: Urko Lekuona Cc: pgsql-general@lists.postgresql.org, =?UTF-8?Q?I=C3=B1igo_Telleria?= Message-ID: In-Reply-To: References: Subject: Sv: PostgreSQL transaction aborted on SQL error MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_188_122345579.1596533106723" X-Mailer: Visena Mail 2.2.352 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_188_122345579.1596533106723 Content-Type: multipart/related; boundary="----=_Part_189_131676868.1596533106723" ------=_Part_189_131676868.1596533106723 Content-Type: multipart/alternative; boundary="----=_Part_190_699875180.1596533106739" ------=_Part_190_699875180.1596533106739 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable P=C3=A5 tirsdag 04. august 2020 kl. 10:44:36, skrev Urko Lekuona >:=20 Hello,=20 First time writing here, I hope this is the right place to ask this kind of= =20 question. I've been working with PostgreSQL for a while now but i've just f= ound=20 out that PostgreSQL marks my transaction for ROLLBACK and even stops the=20 execution of the transaction if an error occurs.=20 I'm a Java developer and I'm using JDBC to connect to PostgreSQL. I've made= a=20 gist to showcase this behavior ( https://gist.github.com/UrkoLekuona/4be14e099aeec2e7d8ef616886b66fdf/=20 ). I= f=20 you run it, you'd see that when the unique key constraint is violated, my= =20 transaction is stopped, i.e. the SELECT and DROP statements are not execute= d.=20 The thrown exception is org.postgresql.util.PSQLException: ERROR: current= =20 transaction is aborted, commands ignored until end of transaction block=20 I've purposely set AutoCommit to false, because in my real life use case th= is=20 is not an option. The only workaround I've found for this exception is sett= ing=20 the connection propertyautosave to ALWAYS, ( https://jdbc.postgresql.org/documentation/head/connect.html=20 ).=20 My question is: is this the correct way of solving this issue? I'd rather i= f=20 there was a PostgreSQL flag to disable this behavior and make it work like= =20 other RDBMS do, where if a statement failed, the transaction could continue= =20 without explicitly marking a savepoint and rolling back.=20 Thanks in advance for your help, it is appreciated.=20 Urko=20 The correct approach is to ROLLBACK the transaction in a "catch-block" inst= ead=20 of trying to execute further statements. The java.sql.Connection is "invali= d"=20 after an SQLException and should be rolled back.=20 -- Andreas Joseph Krogh=20 ------=_Part_190_699875180.1596533106739 Content-Type: text/html;charset=UTF-8 Content-Transfer-Encoding: quoted-printable
P=C3=A5 tirsdag 04. august 2020 kl. 10:44:36, skrev Urko Lekuona <<= a href=3D"mailto:urko@arima.eu">urko@arima.eu>:
Hello,
=C2=A0
First time writing here, I hope this is the right place to ask this ki= nd of question. I've been working with PostgreSQL for a while now but i've = just found out that PostgreSQL marks my transaction for ROLLBACK and even s= tops the execution of the transaction if an error occurs.
=C2=A0
I'm a Java developer and I'm using JDBC to connect to PostgreSQL. I've= made a gist to showcase this behavior (https://gist.github.com/Urko= Lekuona/4be14e099aeec2e7d8ef616886b66fdf/). If you run it, you'd see th= at when the unique key constraint is violated, my transaction is stopped, i= .e. the SELECT and DROP statements are not executed. The thrown exception i= s=C2=A0org.postgresql.util.PSQLException: ERROR: current transaction is abo= rted, commands ignored until end of transaction block
=C2=A0
I've purposely=C2=A0set AutoCommit to false, because=C2=A0in my real l= ife use case this is not an option. The only workaround I've found for this= exception is setting the connection property autosave=C2=A0to ALWAY= S, (https://jdbc.postgresql.org/documentation/head/connect.html).
=C2=A0
My question is: is this the correct way of solving this issue? I'd rat= her if there was a PostgreSQL flag to disable this behavior and make it wor= k like other RDBMS do, where if a statement failed, the transaction could c= ontinue without explicitly marking a savepoint and rolling back.
=C2=A0
Thanks in advance for your help, it is appreciated.
=C2=A0
Urko
=C2=A0
=C2=A0
The correct approach is to ROLLBACK the transaction in a "catch-b= lock" instead of trying to execute further statements. The java.sql.Co= nnection is "invalid" after an SQLException and should be rolled = back.
=C2=A0
">
--
Andreas Joseph Krogh
------=_Part_190_699875180.1596533106739-- ------=_Part_189_131676868.1596533106723-- ------=_Part_188_122345579.1596533106723--