Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,11 @@ public static Connection getConnectionFromSqlObject(final Object obj) {
return (Connection) obj;
} else if (obj instanceof Statement) {
final Statement stmt = (Statement) obj;
return stmt.getConnection();
return !stmt.isClosed() ? stmt.getConnection() : null;
} else if (obj instanceof ResultSet) {
final ResultSet rs = (ResultSet) obj;
return rs.getStatement() != null ? rs.getStatement().getConnection() : null;
final Statement stmt = rs.getStatement();
return stmt != null && !stmt.isClosed() ? stmt.getConnection() : null;
}
} catch (final SQLException | UnsupportedOperationException e) {
// Do nothing. The UnsupportedOperationException comes from ResultSets returned by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@

package software.amazon.jdbc.util;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -138,4 +143,19 @@ void testExecutesWithPluginsWithExceptionIsSequential() {
future.join();
}
}

@Test
void getConnectionFromSqlObjectChecksStatementNotClosed() throws Exception {
final Statement mockClosedStatement = mock(Statement.class);
when(mockClosedStatement.isClosed()).thenReturn(true);
when(mockClosedStatement.getConnection()).thenThrow(IllegalStateException.class);

final ResultSet mockResultSet = mock(ResultSet.class);
when(mockResultSet.getStatement()).thenReturn(mockClosedStatement);

final Connection stmtConn = WrapperUtils.getConnectionFromSqlObject(mockClosedStatement);
assertNull(stmtConn);
final Connection rsConn = WrapperUtils.getConnectionFromSqlObject(mockClosedStatement);
assertNull(rsConn);
}
}