Skip to content

Commit

Permalink
review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgreenbird committed Jan 18, 2024
1 parent 562cd72 commit d9c8db2
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3222,6 +3222,41 @@ else if (0 == requestedPacketSize)
return this;
}

private boolean shouldFailConnection(int errorCode, int driverErrorCode, int attemptNumber, boolean isDBMirroring,
boolean isTnir) {
if (SQLServerException.LOGON_FAILED == errorCode // logon failed, ie bad password
|| SQLServerException.PASSWORD_EXPIRED == errorCode // password expired
|| SQLServerException.USER_ACCOUNT_LOCKED == errorCode // user account locked
|| SQLServerException.DRIVER_ERROR_INVALID_TDS == driverErrorCode // invalid TDS
|| SQLServerException.DRIVER_ERROR_SSL_FAILED == driverErrorCode // SSL failure
|| SQLServerException.DRIVER_ERROR_INTERMITTENT_TLS_FAILED == driverErrorCode // TLS1.2 failure
|| SQLServerException.DRIVER_ERROR_UNSUPPORTED_CONFIG == driverErrorCode // unsupported config
// (eg Sphinx, invalid
// packetsize, etc)
|| timerHasExpired(timerExpire)) {
return true;
}

// retry at least once for failover
if (isDBMirroring) {
if (SQLServerException.ERROR_SOCKET_TIMEOUT == driverErrorCode && attemptNumber < 1) {
return false;
} else if (connectRetryCount == 0 && attemptNumber >= 1) {
return true;
}
return false;
}

// retry at least once for tnir
if (isTnir && connectRetryCount == 0 && attemptNumber >= 1) {
return true;
} else if (!isTnir && attemptNumber >= connectRetryCount) {
return true;
}

return false;
}

/**
* This function is used by non failover and failover cases. Even when we make a standard connection the server can
* provide us with its FO partner. If no FO information is available a standard connection is made. If the server
Expand Down Expand Up @@ -3438,21 +3473,7 @@ private void login(String primary, String primaryInstanceName, int primaryPortNu
int driverErrorCode = e.getDriverErrorCode();
sqlServerError = e.getSQLServerError();

if (SQLServerException.LOGON_FAILED == errorCode // logon failed, ie bad password
|| SQLServerException.PASSWORD_EXPIRED == errorCode // password expired
|| SQLServerException.USER_ACCOUNT_LOCKED == errorCode // user account locked
|| SQLServerException.DRIVER_ERROR_INVALID_TDS == driverErrorCode // invalid TDS
|| SQLServerException.DRIVER_ERROR_SSL_FAILED == driverErrorCode // SSL failure
|| SQLServerException.DRIVER_ERROR_INTERMITTENT_TLS_FAILED == driverErrorCode // TLS1.2 failure
|| SQLServerException.DRIVER_ERROR_UNSUPPORTED_CONFIG == driverErrorCode // unsupported config
// (eg Sphinx, invalid
// packetsize, etc)
|| (SQLServerException.ERROR_SOCKET_TIMEOUT == driverErrorCode // socket timeout
&& (!isDBMirroring || attemptNumber > 0)) // If mirroring, only close after failover has been tried (attempt >= 1)
|| timerHasExpired(timerExpire) || (connectRetryCount == 0 && !useTnir)
|| (connectRetryCount == 0 && useTnir && attemptNumber >= 1)
// for non-dbmirroring cases, do not retry after tcp socket connection succeeds
) {
if (shouldFailConnection(errorCode, driverErrorCode, attemptNumber, isDBMirroring, useTnir)) {
if (loggerResiliency.isLoggable(Level.FINER)) {
loggerResiliency.finer(
toString() + " Connection open - connection failed on attempt: " + attemptNumber + ".");
Expand Down

0 comments on commit d9c8db2

Please sign in to comment.