Обсуждение: committing an aborted transaction

Поиск
Список
Период
Сортировка

committing an aborted transaction

От
fschmidt
Дата:
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.


Re: committing an aborted transaction

От
Tom Lane
Дата:
fschmidt <fschmidt@gmail.com> writes:
> Committing an aborted transaction should throw an exception.

This is a server-side behavior, not JDBC's fault.  It's been like that
for ten years and is unlikely to be changed.  See prior discussions,
for instance here:

http://archives.postgresql.org/pgsql-bugs/2004-03/msg00179.php
http://archives.postgresql.org/pgsql-hackers/2004-07/msg00416.php
http://archives.postgresql.org/pgsql-hackers/2004-10/msg00908.php

            regards, tom lane

Re: committing an aborted transaction

От
fschmidt
Дата:

Tom Lane-2 wrote:
>
> This is a server-side behavior, not JDBC's fault.  It's been like that
> for ten years and is unlikely to be changed.  See prior discussions,
> for instance here:
>
> http://archives.postgresql.org/pgsql-bugs/2004-03/msg00179.php
> http://archives.postgresql.org/pgsql-hackers/2004-07/msg00416.php
> http://archives.postgresql.org/pgsql-hackers/2004-10/msg00908.php
>

So it seems, based on this last link, that commit now returns a "ROLLBACK"
in this case.  So then why doesn't JDBC throw an exception when a commit
returns "ROLLBACK"?

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