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 NPE with Statement.closeOnCompletion() #1639

Merged
merged 3 commits into from
Oct 6, 2021
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 @@ -657,7 +657,7 @@ private boolean retryBasedOnFailedReuseOfCachedHandle(SQLException e, int attemp
boolean consumeExecOutParam(TDSReader tdsReader) throws SQLServerException {
final class PrepStmtExecOutParamHandler extends StmtExecOutParamHandler {

PrepStmtExecOutParamHandler(SQLServerStatement statement) {
PrepStmtExecOutParamHandler(SQLServerStatement statement) {
super(statement);
}

Expand Down Expand Up @@ -954,7 +954,7 @@ private void getParameterEncryptionMetadata(Parameter[] params) throws SQLServer
DescribeParameterEncryptionResultSet2.ColumnEncryptionAlgorithm.value()),
null, encType.value, (byte) secondRs.getInt(
DescribeParameterEncryptionResultSet2.NormalizationRuleVersion.value()));

SQLServerStatement statement = (SQLServerStatement) stmt;
// Decrypt the symmetric key.(This will also validate and throw if needed).
SQLServerSecurityUtility.decryptSymmetricKey(params[paramIndex].cryptoMeta, connection, statement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ final boolean isCursorable(int executeMethod) {

class StmtExecOutParamHandler extends TDSTokenHandler {
SQLServerStatement statement;
StmtExecOutParamHandler(SQLServerStatement statement) {
StmtExecOutParamHandler(SQLServerStatement statement) {
super("StmtExecOutParamHandler");
this.statement = statement;
}
Expand Down Expand Up @@ -1320,6 +1320,7 @@ final void ensureExecuteResultsReader(TDSReader tdsReader) {
final void processExecuteResults() throws SQLServerException {
if (wasExecuted()) {
processBatch();
checkClosed(); // processBatch could have resulted in a closed connection if isCloseOnCompletion is set
TDSParser.parse(resultsReader(), "batch completion");
ensureExecuteResultsReader(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ SQLServerSymmetricKey getKey(EncryptionKeyInfo keyInfo, SQLServerConnection conn

// search system/global key store providers
SQLServerColumnEncryptionKeyStoreProvider provider = connection.getSystemOrGlobalColumnEncryptionKeyStoreProvider(keyInfo.keyStoreName);
assert null != provider : "Provider should not be null.";
assert null != provider : "Provider should not be null.";

byte[] plaintextKey;

Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,6 @@ public static String bytesToHexString(byte[] b, int length) {
return sb.toString();
}


/**
*
* @param b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.StringReader;
Expand Down Expand Up @@ -2052,6 +2053,41 @@ public void testClosedResultSet() throws Exception {
}
}

/**
* Tests reusing Statement after being automatically closed via closeOnCompletion().
*
* @throws Exception
*/
@Test
public void testReuseAfterCloseOnCompletion() throws Exception {
try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) {

// enable isCloseOnCompletion
try {
stmt.closeOnCompletion();
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedException") + e.getMessage());
}

try (SQLServerResultSet rs = (SQLServerResultSet) stmt.executeQuery("SELECT 1")) {
assertNotNull(rs, TestResource.getResource("R_resultsetNull"));

try {
// Executing a new command should close the previous ResultSet
// ... and closing the original ResultSet should close the Statement
// ... so this should fail with an error indicating that the Statement is closed.
stmt.executeQuery("SELECT 1");
fail(TestResource.getResource("R_expectedExceptionNotThrown"));
} catch (SQLException e) {
assertEquals(TestResource.getResource("R_statementClosed"), e.getMessage());
} catch (Exception e) {
e.printStackTrace();
fail(TestResource.getResource("R_unexpectedException") + e.getMessage());
}
}
}
}

/**
* Tests closing statement will close resultset
*
Expand Down