Skip to content

Commit 3db155e

Browse files
committed
Don't get Statement from closed ResultSet
It's possible that getConnectionFromSqlObject is called on a ResultSet that is already closed, e.g. in DefaultConnectionPlugin when calling .close on a ResultSet. With some implementations, such as PgResultSet, calling getStatement on a closed ResultSet throws an SQLException. There is a performance overhead associated with creating and catching these exceptions everytime a ResultSet is used, and closed, so it should be avoided.
1 parent d0a246e commit 3db155e

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

wrapper/src/main/java/software/amazon/jdbc/util/WrapperUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ public static Connection getConnectionFromSqlObject(final Object obj) {
579579
return !stmt.isClosed() ? stmt.getConnection() : null;
580580
} else if (obj instanceof ResultSet) {
581581
final ResultSet rs = (ResultSet) obj;
582-
final Statement stmt = rs.getStatement();
582+
final Statement stmt = !rs.isClosed() ? rs.getStatement() : null;
583583
return stmt != null && !stmt.isClosed() ? stmt.getConnection() : null;
584584
}
585585
} catch (final SQLException | UnsupportedOperationException e) {

wrapper/src/test/java/software/amazon/jdbc/util/WrapperUtilsTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ void getConnectionFromSqlObjectChecksStatementNotClosed() throws Exception {
160160
assertNull(rsConn);
161161
}
162162

163+
@Test
164+
void getConnectionFromSqlObjectChecksResultSetNotClosed() throws Exception {
165+
final ResultSet mockResultSet = mock(ResultSet.class);
166+
when(mockResultSet.isClosed()).thenReturn(true);
167+
when(mockResultSet.getStatement()).thenThrow(IllegalStateException.class);
168+
169+
final Connection rsConn = WrapperUtils.getConnectionFromSqlObject(mockResultSet);
170+
assertNull(rsConn);
171+
}
172+
163173
@Test
164174
void testStatementWrapper() throws InstantiationException {
165175
ConnectionPluginManager mockPluginManager = mock(ConnectionPluginManager.class);

0 commit comments

Comments
 (0)