File tree Expand file tree Collapse file tree
main/java/org/postgresql/jdbc
test/java/org/postgresql/test Expand file tree Collapse file tree Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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 */
Original file line number Diff line number Diff line change 55
66package org .postgresql .test .jdbc4 .jdbc41 ;
77
8+ import static org .junit .Assert .assertEquals ;
89import static org .junit .Assert .assertFalse ;
910import 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}
You can’t perform that action at this time.
0 commit comments