From: Andreas Joseph Krogh <andreas@visena.com>
To: Urko Lekuona <urko@arima.eu>
Cc: pgsql-general@lists.postgresql.org, Iñigo Telleria <inigo@arima.eu>
Subject: Sv: PostgreSQL transaction aborted on SQL error
Date: Tue, 4 Aug 2020 11:25:06 +0200 (CEST)
Message-ID: <VisenaEmail.29.9761365f727dce2b.173b8c858ff@tc7-visena> (raw)
In-Reply-To: <CAKjfNx+Ztikjp8Wmy+t5KHLudtWuF0ApWVjmFObhyBuS=BZdwA@mail.gmail.com>
References: <CAKjfNx+Ztikjp8Wmy+t5KHLudtWuF0ApWVjmFObhyBuS=BZdwA@mail.gmail.com>
På tirsdag 04. august 2020 kl. 10:44:36, skrev Urko Lekuona <urko@arima.eu
<mailto:urko@arima.eu>>:
Hello,
First time writing here, I hope this is the right place to ask this kind 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 stops the
execution of the transaction if an error occurs.
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/UrkoLekuona/4be14e099aeec2e7d8ef616886b66fdf/
<https://gist.github.com/UrkoLekuona/4be14e099aeec2e7d8ef616886b66fdf/;). If
you run it, you'd see that when the unique key constraint is violated, my
transaction is stopped, i.e. the SELECT and DROP statements are not executed.
The thrown exception is org.postgresql.util.PSQLException: ERROR: current
transaction is aborted, commands ignored until end of transaction block
I've purposely set AutoCommit to false, because in my real life use case this
is not an option. The only workaround I've found for this exception is setting
the connection propertyautosave to ALWAYS, (
https://jdbc.postgresql.org/documentation/head/connect.html
<https://jdbc.postgresql.org/documentation/head/connect.html;).
My question is: is this the correct way of solving this issue? I'd rather if
there was a PostgreSQL flag to disable this behavior and make it work like
other RDBMS do, where if a statement failed, the transaction could continue
without explicitly marking a savepoint and rolling back.
Thanks in advance for your help, it is appreciated.
Urko
The correct approach is to ROLLBACK the transaction in a "catch-block" instead
of trying to execute further statements. The java.sql.Connection is "invalid"
after an SQLException and should be rolled back.
--
Andreas Joseph Krogh
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: andreas@visena.com, urko@arima.eu, inigo@arima.eu
Subject: Re: PostgreSQL transaction aborted on SQL error
In-Reply-To: <VisenaEmail.29.9761365f727dce2b.173b8c858ff@tc7-visena>
References: <CAKjfNx+Ztikjp8Wmy+t5KHLudtWuF0ApWVjmFObhyBuS=BZdwA@mail.gmail.com> <VisenaEmail.29.9761365f727dce2b.173b8c858ff@tc7-visena>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox