From: Andreas Joseph Krogh <andreas@visena.com>
To: pgsql-jdbc@postgresql.org
Subject: Re: JDBC behaviour
Date: Thu, 18 Feb 2016 10:46:12 +0100 (CET)
Message-ID: <VisenaEmail.100.3a3abd9c7ae96d92.152f3c1e851@tc7-visena> (raw)
In-Reply-To: <CAGuFTBXnVvX8cmbxF7HYhfrTQf9_pUTU=Lq2F4Niq-4jiObCzg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>
På torsdag 18. februar 2016 kl. 10:38:23, skrev Sridhar N Bamandlapally <
sridhar.bn1@gmail.com <mailto:sridhar.bn1@gmail.com>>:
let me put this way
table employee ( id PrimaryKey, name )
In Java ( just little pseudo-code way )
try {
conn.setAutoCommit(false);
try { executeUpdate("insert into employee(id,name) values(1, 'K1')"); }
catch ...
try { executeUpdate("insert into employee(id,name) values(1, 'K1')"); }
catch ...
try { executeUpdate("insert into employee(id,name) values(1, 'K2')"); }
catch ...
conn.commit();
} catch ...
throws error
1. duplicate key value violates unique constraint "employee_pkey"
2. current transaction is aborted, commands ignored until end of transaction
block
In PL/SQL ( similar error thrown when used BEGIN-END )
postgres=# begin;
BEGIN
postgres=# insert into employee values (1,'aa');
INSERT 0 1
postgres=# insert into employee values (2,'bb');
INSERT 0 1
postgres=# insert into employee values (3,'cc');
INSERT 0 1
postgres=# insert into employee values (1,'aa');
ERROR: duplicate key value violates unique constraint "employee_pkey"
DETAIL: Key (eid)=(1) already exists.
postgres=# insert into employee values (4,'dd');
ERROR: current transaction is aborted, commands ignored until end of
transaction block
my question Java setAutoCommit (false) is behaving like PL/SQL BEGIN-END
You are free to ignore what we tell you about the transaction being invalid
after an exception, and try all you want to issue new statements. But it will
all result in errors like the above. Youhave to issue a ROLLBACK to proceed,
and PG knows this so it refuses to do anything until you do.
I other words; There is no way to issue a statement without getting an error
in a transaction marked as invalid. This is what transactions are for,
guaranteeing consistent results.
-- Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
andreas@visena.com <mailto:andreas@visena.com>
www.visena.com <https://www.visena.com;
<https://www.visena.com;
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-jdbc@postgresql.org
Cc: andreas@visena.com
Subject: Re: JDBC behaviour
In-Reply-To: <VisenaEmail.100.3a3abd9c7ae96d92.152f3c1e851@tc7-visena>
References: <CAGuFTBXnVvX8cmbxF7HYhfrTQf9_pUTU=Lq2F4Niq-4jiObCzg@mail.gmail.com> <VisenaEmail.100.3a3abd9c7ae96d92.152f3c1e851@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