Skip to content

Commit f0c1913

Browse files
machavanDivang Sharma
authored andcommitted
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 66af699 commit f0c1913

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
@@ -2189,6 +2189,11 @@ Connection connect(Properties propsIn, SQLServerPooledConnection pooledConnectio
21892189

21902190
long elapsedSeconds = 0;
21912191
long start = System.currentTimeMillis();
2192+
2193+
// Any existing enclave session would be invalid, make sure it is invalidated.
2194+
// For example, if this is a session recovery reconnect.
2195+
//
2196+
invalidateEnclaveSessionCache();
21922197
for (int connectRetryAttempt = 0, tlsRetryAttempt = 0;;) {
21932198
try {
21942199
if (0 == elapsedSeconds || elapsedSeconds < loginTimeoutSeconds) {
@@ -8913,6 +8918,15 @@ ArrayList<byte[]> initEnclaveParameters(SQLServerStatement statement, String use
89138918
parameterNames);
89148919
}
89158920

8921+
void invalidateEnclaveSessionCache() {
8922+
if (enclaveProvider != null) {
8923+
if (connectionlogger.isLoggable(Level.FINE)) {
8924+
connectionlogger.fine("Invalidating existing enclave session for enclave provider : " + enclaveProvider);
8925+
}
8926+
enclaveProvider.invalidateEnclaveSession();
8927+
}
8928+
}
8929+
89168930
boolean enclaveEstablished() {
89178931
return (null != enclaveProvider.getEnclaveSession());
89188932
}

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)