committing an aborted transaction - Mailing list pgsql-jdbc

From fschmidt
Subject committing an aborted transaction
Date
Msg-id 20519423.post@talk.nabble.com
Whole thread Raw
Responses Re: committing an aborted transaction  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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.


pgsql-jdbc by date:

Previous
From: "Jay Howard"
Date:
Subject: Re: passing user defined data types to stored procedures
Next
From: Tom Lane
Date:
Subject: Re: committing an aborted transaction