From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-jdbc@postgresql.org
Subject: Re: JDBC behaviour
Date: Sat, 20 Feb 2016 15:51:58 +0100
Message-ID: <na9uij$9vn$1@ger.gmane.org> (raw)
In-Reply-To: <CAMsr+YH3NHZ__xSxWfVH4neGa6w8nLRuLLRG8kqXt9bgE1eEBA@mail.gmail.com>
References: <VisenaEmail.10e.692062643617cb13.152f4695802@tc7-visena>
<954cc530fdaf2c5cf63dcb180d05fb56@imap.procolix.com>
<CAGuFTBXSwk2UmFs_b0wYuTv_BFtLwVmco1mgT5OSb+dwX-U+Sw@mail.gmail.com>
<4C508581-9CA3-490D-A376-CC1F9FF5D258@me.com>
<CAGuFTBW_xcn5nWtggiwq46f=6Rc5TYEqwtmiWEEGGfB+PSLbUg@mail.gmail.com>
<CAMsr+YH3NHZ__xSxWfVH4neGa6w8nLRuLLRG8kqXt9bgE1eEBA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>
Craig Ringer schrieb am 20.02.2016 um 11:44:
> Please provide a complete, compileable, self-contained example demonstrating behaviour that
>causes a failure or problem in PgJDBC but works correctly with at least most of:
>
> - MS SQL
> - Oracle
> - DB2
> - Sybase
> - MySQL
>
> including test run output demonstrating the details of what exactly the behaviour of each other implementation is.
Disclaimer: I do agree with you that Postgres behaviour is correct. A transaction either succeeds completely or not at all.
Having said that: Postgres' behaviour *is* unique regarding this.
Consider the following table:
create table x (id integer not null primary key);
The following code will run with Oracle, DB2, SQL Sever, MySQL and Firebird:
---- code start ----
public class TestInsert
{
public static void main(String args[])
throws Exception
{
Connection con = DriverManager.getConnection("...", "...", "...");
con.setAutoCommit(false);
PreparedStatement pstmt = con.prepareStatement("insert into x (id) values (?)");
pstmt.setInt(1, 1);
pstmt.executeUpdate();
try
{
pstmt.setInt(1, 1);
pstmt.executeUpdate();
}
catch (Exception ex)
{
System.out.println("***** Error: " + ex.getMessage());
}
System.out.println("trying second row");
pstmt.setInt(1, 2);
pstmt.executeUpdate();
con.commit();
ResultSet rs = con.createStatement().executeQuery("select count(*) from x");
if (rs.next())
{
int rows = rs.getInt(1);
System.out.println("rows: " + rows);
}
con.close();
}
}
---- code end ----
With Oracle DB2, SQL Server, MySQL and Firebird you'll get something like this
(this is from Oracle, the error message will of course differ for the others)
***** Error: ORA-00001: Unique Constraint (THOMAS.SYS_C0021623) violated
trying second row
rows: 2
With Postgres you get:
***** Error: ERROR: duplicate key value violates unique constraint "x_pkey"
Detail: Key (id)=(1) already exists.
trying second row
Exception in thread "main" org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block
But again: in contrast to the OP I do not consider this a bug, it's just that Postgres behaves differently.
But I understand that it can be a problem when migrating applications that have a sloppy way of dealing with transactions.
I was part in the migration of several projects from Oracle and MySQL to Postgres in the last two years, none of them had problems regarding that.
Thomas
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc
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: spam_eater@gmx.net
Subject: Re: JDBC behaviour
In-Reply-To: <na9uij$9vn$1@ger.gmane.org>
References: <VisenaEmail.10e.692062643617cb13.152f4695802@tc7-visena> <954cc530fdaf2c5cf63dcb180d05fb56@imap.procolix.com> <CAGuFTBXSwk2UmFs_b0wYuTv_BFtLwVmco1mgT5OSb+dwX-U+Sw@mail.gmail.com> <4C508581-9CA3-490D-A376-CC1F9FF5D258@me.com> <CAGuFTBW_xcn5nWtggiwq46f=6Rc5TYEqwtmiWEEGGfB+PSLbUg@mail.gmail.com> <CAMsr+YH3NHZ__xSxWfVH4neGa6w8nLRuLLRG8kqXt9bgE1eEBA@mail.gmail.com> <na9uij$9vn$1@ger.gmane.org>
* 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