diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java index 3b21e8715..479ea62ba 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java @@ -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 @@ -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 + ".");