Skip to content

Commit fc25aeb

Browse files
committed
fix: Actually close unclosed results. Previously was not closing the first unclosed result fixes #1903 (#1905)
* fix: Actually close unclosed results. Previously was not closing the first unclosed result fixes #1903 * rename and comment closing of unclosed results to make it more clear
1 parent e551d1a commit fc25aeb

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

pgjdbc/src/main/java/org/postgresql/jdbc/PgStatement.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,11 @@ public boolean executeWithFlags(int flags) throws SQLException {
332332
PSQLState.WRONG_OBJECT_TYPE);
333333
}
334334

335-
private void closeUnclosedResults() throws SQLException {
335+
/*
336+
If there are multiple result sets we close any that have been processed and left open
337+
by the client.
338+
*/
339+
private void closeUnclosedProcessedResults() throws SQLException {
336340
synchronized (this) {
337341
ResultWrapper resultWrapper = this.firstUnclosedResult;
338342
ResultWrapper currentResult = this.result;
@@ -354,7 +358,11 @@ protected void closeForNextExecution() throws SQLException {
354358

355359
// Close any existing resultsets associated with this statement.
356360
synchronized (this) {
357-
closeUnclosedResults();
361+
closeUnclosedProcessedResults();
362+
363+
if ( this.result != null && this.result.getResultSet() != null ) {
364+
this.result.getResultSet().close();
365+
}
358366
result = null;
359367

360368
ResultWrapper generatedKeys = this.generatedKeys;
@@ -1188,7 +1196,7 @@ public boolean getMoreResults(int current) throws SQLException {
11881196
// CLOSE_ALL_RESULTS
11891197
if (current == Statement.CLOSE_ALL_RESULTS) {
11901198
// Close preceding resultsets.
1191-
closeUnclosedResults();
1199+
closeUnclosedProcessedResults();
11921200
}
11931201

11941202
// Done.

pgjdbc/src/test/java/org/postgresql/test/jdbc2/StatementTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ public void testClose() throws SQLException {
9898
}
9999
}
100100

101+
@Test
102+
public void testResultSetClosed() throws SQLException {
103+
Statement stmt = con.createStatement();
104+
ResultSet rs = stmt.executeQuery("select 1");
105+
stmt.close();
106+
assertTrue(rs.isClosed());
107+
}
108+
101109
/**
102110
* Closing a Statement twice is not an error.
103111
*/

pgjdbc/src/test/java/org/postgresql/test/jdbc4/jdbc41/CloseOnCompletionTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.postgresql.test.jdbc4.jdbc41;
77

8+
import static org.junit.Assert.assertEquals;
89
import static org.junit.Assert.assertFalse;
910
import static org.junit.Assert.assertTrue;
1011

@@ -94,8 +95,28 @@ public void testNoResultSet() throws SQLException {
9495
@Test
9596
public void testExecuteTwice() throws SQLException {
9697
PreparedStatement s = conn.prepareStatement("SELECT 1");
97-
s.closeOnCompletion();
98+
99+
s.executeQuery();
98100
s.executeQuery();
101+
102+
}
103+
104+
@Test
105+
public void testCloseOnCompletionExecuteTwice() throws SQLException {
106+
PreparedStatement s = conn.prepareStatement("SELECT 1");
107+
108+
/*
109+
once we set close on completion we should only be able to execute one as the second execution
110+
will close the resultsets from the first one which will close the statement.
111+
*/
112+
113+
s.closeOnCompletion();
99114
s.executeQuery();
115+
try {
116+
s.executeQuery();
117+
} catch (SQLException ex) {
118+
assertEquals(ex.getMessage(),"This statement has been closed.");
119+
}
120+
100121
}
101122
}

0 commit comments

Comments
 (0)