Skip to content

Commit aae6a01

Browse files
authored
Invalidate existing enclave session during connection (re)connect (#2638)
* Invalidate existing enclave session in during (re)connect * Added check on logging level * Added a speculative fix to invalidate enclaveCache on error 33195 * Moved error checking in PreparedStatement doExecute methods * Added trade for invalidation * Directly call invalidateEnclaveSessionCache from reconnect
1 parent 1ab5f7c commit aae6a01

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,11 @@ Connection connect(Properties propsIn, SQLServerPooledConnection pooledConnectio
21952195

21962196
long elapsedSeconds = 0;
21972197
long start = System.currentTimeMillis();
2198+
2199+
// Any existing enclave session would be invalid, make sure it is invalidated.
2200+
// For example, if this is a session recovery reconnect.
2201+
//
2202+
invalidateEnclaveSessionCache();
21982203
for (int connectRetryAttempt = 0, tlsRetryAttempt = 0;;) {
21992204
try {
22002205
if (0 == elapsedSeconds || elapsedSeconds < loginTimeoutSeconds) {
@@ -8969,6 +8974,15 @@ ArrayList<byte[]> initEnclaveParameters(SQLServerStatement statement, String use
89698974
parameterNames);
89708975
}
89718976

8977+
void invalidateEnclaveSessionCache() {
8978+
if (enclaveProvider != null) {
8979+
if (connectionlogger.isLoggable(Level.FINE)) {
8980+
connectionlogger.fine("Invalidating existing enclave session for enclave provider : " + enclaveProvider);
8981+
}
8982+
enclaveProvider.invalidateEnclaveSession();
8983+
}
8984+
}
8985+
89728986
boolean enclaveEstablished() {
89738987
return (null != enclaveProvider.getEnclaveSession());
89748988
}

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public final class SQLServerException extends java.sql.SQLException {
7373
// Built-in function '%.*ls' in impersonation context is not supported in this version of SQL Server.
7474
static final int IMPERSONATION_CONTEXT_NOT_SUPPORTED = 40529;
7575

76+
static final int INVAID_ENCLAVE_SESSION_HANDLE_ERROR = 33195;
77+
7678
// Facility for driver-specific error codes
7779
static final int DRIVER_ERROR_NONE = 0;
7880
static final int DRIVER_ERROR_FROM_DATABASE = 2;

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,12 @@ final void doExecutePreparedStatement(PrepStmtExecCmd command) throws SQLServerE
687687
startResults();
688688
getNextResult(true);
689689
} catch (SQLException e) {
690+
if (connection.isAEv2() && (e.getErrorCode() == SQLServerException.INVAID_ENCLAVE_SESSION_HANDLE_ERROR)) {
691+
//If the exception received is as below then just invalidate the cache
692+
//code = '33195', SQL state = 'S0001': Internal enclave error. Enclave was provided with an invalid session handle. For more information, contact Customer Support Services..
693+
//
694+
connection.invalidateEnclaveSessionCache();
695+
}
690696
if (retryBasedOnFailedReuseOfCachedHandle(e, attempt, needsPrepare, false)) {
691697
continue;
692698
} else if (!inRetry && connection.doesServerSupportEnclaveRetry()) {
@@ -3119,6 +3125,12 @@ final void doExecutePreparedStatementBatch(PrepStmtBatchExecCmd batchCommand) th
31193125
assert numBatchesExecuted == numBatchesPrepared;
31203126
}
31213127
} catch (SQLException e) {
3128+
if (connection.isAEv2() && (e.getErrorCode() == SQLServerException.INVAID_ENCLAVE_SESSION_HANDLE_ERROR)) {
3129+
//If the exception received is as below then just invalidate the cache
3130+
//code = '33195', SQL state = 'S0001': Internal enclave error. Enclave was provided with an invalid session handle. For more information, contact Customer Support Services..
3131+
//
3132+
connection.invalidateEnclaveSessionCache();
3133+
}
31223134
if (retryBasedOnFailedReuseOfCachedHandle(e, attempt, needsPrepare, true)
31233135
&& connection.isStatementPoolingEnabled()) {
31243136
// Reset number of batches prepared.

0 commit comments

Comments
 (0)