Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Actually close unclosed results. Previously was not closing the first unclosed result fixes #1903 #1905

Merged
merged 11 commits into from
Feb 17, 2021
14 changes: 11 additions & 3 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ public boolean executeWithFlags(int flags) throws SQLException {
PSQLState.WRONG_OBJECT_TYPE);
}

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

// Close any existing resultsets associated with this statement.
synchronized (this) {
closeUnclosedResults();
closeUnclosedProcessedResults();

if ( this.result != null && this.result.getResultSet() != null ) {
this.result.getResultSet().close();
}
result = null;

ResultWrapper generatedKeys = this.generatedKeys;
Expand Down Expand Up @@ -1186,7 +1194,7 @@ public boolean getMoreResults(int current) throws SQLException {
// CLOSE_ALL_RESULTS
if (current == Statement.CLOSE_ALL_RESULTS) {
// Close preceding resultsets.
closeUnclosedResults();
closeUnclosedProcessedResults();
}

// Done.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public void testClose() throws SQLException {
}
}

@Test
public void testResultSetClosed() throws SQLException {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select 1");
stmt.close();
assertTrue(rs.isClosed());
}

/**
* Closing a Statement twice is not an error.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.postgresql.test.jdbc4.jdbc41;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -94,8 +95,28 @@ public void testNoResultSet() throws SQLException {
@Test
public void testExecuteTwice() throws SQLException {
PreparedStatement s = conn.prepareStatement("SELECT 1");
s.closeOnCompletion();

s.executeQuery();
s.executeQuery();

}

@Test
public void testCloseOnCompletionExecuteTwice() throws SQLException {
PreparedStatement s = conn.prepareStatement("SELECT 1");

/*
once we set close on completion we should only be able to execute one as the second execution
will close the resultsets from the first one which will close the statement.
*/

s.closeOnCompletion();
s.executeQuery();
try {
s.executeQuery();
} catch (SQLException ex) {
assertEquals(ex.getMessage(),"This statement has been closed.");
jorsol marked this conversation as resolved.
Show resolved Hide resolved
}

}
}