committing an aborted transaction

From: fschmidt <fschmidt(at)gmail(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: committing an aborted transaction
Date: 2008-11-15 21:00:39
Message-ID: 20519423.post@talk.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc


If one commits an aborted transaction, it seems to rollback without any
notice. Whether a transaction should be aborted by an exception seems
debatable, but to have a commit quietly rollback instead is terrible design.
Committing an aborted transaction should throw an exception. Below is an
example program:

import java.sql.*;

public class P {
public static void main(String[] args) throws Exception {
Class.forName("org.postgresql.Driver");
Connection con =
DriverManager.getConnection("jdbc:postgresql://localhost/n1","postgres","word");
Statement stmt = con.createStatement();
stmt.executeUpdate("drop table if exists t");
stmt.executeUpdate("create table t (id integer)");
stmt.close();

con.setAutoCommit(false);
stmt = con.createStatement();
stmt.executeUpdate("insert into t (id) values (1)");
ResultSet rs = stmt.executeQuery("select * from t where id=1");
System.out.println( (rs.next() ? "ok" : "not found") + " in transaction");
try {
stmt.executeUpdate("nonsense");
} catch(SQLException e) {}
stmt.close();
con.commit();

con.setAutoCommit(true);
stmt = con.createStatement();
rs = stmt.executeQuery("select * from t where id=1");
System.out.println( (rs.next() ? "ok" : "not found") + " after
transaction");
stmt.executeUpdate("drop table if exists t");
stmt.close();
con.close();
}
}

--
View this message in context: http://www.nabble.com/committing-an-aborted-transaction-tp20519423p20519423.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Tom Lane 2008-11-15 21:52:25 Re: committing an aborted transaction
Previous Message Jay Howard 2008-11-15 18:20:41 Re: passing user defined data types to stored procedures