impossibl / pgjdbc-ng

A new JDBC driver for PostgreSQL aimed at supporting the advanced features of JDBC and Postgres
https://impossibl.github.io/pgjdbc-ng
Other
596 stars 108 forks source link

Execution of executeUpdate Closes Previously Acquired ResultSet #598

Closed dwenking closed 7 months ago

dwenking commented 7 months ago

The execution of an executeUpdate statement unexpectedly leads to the closure of a previously obtained ResultSet object. In the provided test case, a ResultSet object is obtained by calling getGeneratedKeys() on a Statement object after executing an UPDATE statement. This ResultSet is expected to remain open for subsequent operations. However, when another executeUpdate is executed on the same Statement object, the previously obtained ResultSet is found to be closed. The expected behavior is that executing an executeUpdate statement should not affect the state (open or closed) of a previously acquired ResultSet.

@Test
public void test() throws SQLException {
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    con = DriverManager.getConnection("jdbc:pgsql://localhost:5432/test69?user=user&password=password");
    stmt = con.createStatement();
    stmt.executeUpdate("CREATE TABLE table16_0(id REAL PRIMARY KEY,value INT);");
    stmt.executeUpdate("UPDATE table16_0 SET value = 77 WHERE id <= 1", 1);
    stmt.executeBatch();
    rs = stmt.getGeneratedKeys();
    System.out.println(rs.isClosed()); // false
    stmt.executeUpdate("DELETE FROM table16_0 WHERE id <= 2055473207", 1);
    System.out.println(rs.isClosed()); // true
    con.close();
}
davecramer commented 7 months ago

This is by design. The spec says A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.

dwenking commented 7 months ago

Thanks for your kind response. I will close this issue since it is a feature.