The JDBC API has the following note related to ResultSet:
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.
In the JDBC 4.3 Specification:
13.1.4 Closing Statement Objects
Closing a Statement object will close and invalidate any instances of ResultSet produced by that Statement object.
At this point it's clear, closing a statement object, should close the ResultSet
The JavaDoc of Statement has this note:
All execution methods in the Statement interface implicitly close a current ResultSet object of the statement if an open one exists.
Now, the question is how should behave Statement.closeOnCompletion()?
Specifies that this Statement will be closed when all its dependent result sets are closed. If execution of the Statement does not produce any result sets, this method has no effect.
Note: ... However, a call to closeOnCompletion does effect both the subsequent execution of statements, and statements that currently have open, dependent, result sets.
The Statement should allow re-executions? or the re-execution should close the statement on the second execution?
To exemplify with a test:
@Test
public void testCloseOnCompletionMultipleExecutionResultSets() throws SQLException {
Statement statement = conn.createStatement();
ResultSet rs1 = statement.executeQuery("SELECT 1");
assertFalse("rs1 should be open", rs1.isClosed());
statement.closeOnCompletion();
// Should the second execution throw an SQLException with "Statement closed"?
// or it should work and the statement be closed until the second rs is closed?
ResultSet rs2 = statement.executeQuery("SELECT 2"); // fail or not?
assertTrue("rs1 should be closed by rs2", rs1.isClosed());
assertFalse("rs2 should be open", rs2.isClosed());
assertFalse("statement should be open", statement.isClosed());
rs2.close(); // Only close the statement here since is the last rs open.
assertTrue("statement should be closed", statement.isClosed());
}
Related discussion: https://github.com/pgjdbc/pgjdbc/pull/1905