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

Append server name from connection string to error message #1311

Merged
merged 16 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 13 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
38 changes: 28 additions & 10 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,25 @@ SharedTimer getSharedTimer() throws SQLServerException {
return this.sharedTimer;
}

/**
* Get the server name string including redirected server if applicable
*
* @param serverName
* @return
*/
String getServerNameString(String serverName) {
String serverNameFromConnectionStr = activeConnectionProperties
ulvii marked this conversation as resolved.
Show resolved Hide resolved
.getProperty(SQLServerDriverStringProperty.SERVER_NAME.toString());
if (null == serverName || !isRoutedInCurrentAttempt) {
return serverName;
}

// server was redirected
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_redirectedFrom"));
Object[] msgArgs = {serverName, serverNameFromConnectionStr};
return form.format(msgArgs);
}

static class CityHash128Key implements java.io.Serializable {

/**
Expand Down Expand Up @@ -1316,8 +1335,7 @@ private void registerKeyStoreProviderOnConnection(String keyStoreAuth, String ke
case KeyVaultClientSecret:
// need a secret use use the secret method
if (null == keyStoreSecret) {
throw new SQLServerException(
SQLServerException.getErrString("R_keyStoreSecretNotSet"), null);
throw new SQLServerException(SQLServerException.getErrString("R_keyStoreSecretNotSet"), null);
} else {
SQLServerColumnEncryptionAzureKeyVaultProvider provider = new SQLServerColumnEncryptionAzureKeyVaultProvider(
keyStorePrincipalId, keyStoreSecret);
Expand Down Expand Up @@ -2328,9 +2346,6 @@ private void login(String primary, String primaryInstanceName, int primaryPortNu
// is done just to be consistent with the rest of the logic.
attemptNumber++;

// set isRoutedInCurrentAttempt to false for the next attempt
isRoutedInCurrentAttempt = false;

// useParallel and useTnir should be set to false once we get routed
useParallel = false;
useTnir = false;
Expand All @@ -2343,12 +2358,15 @@ private void login(String primary, String primaryInstanceName, int primaryPortNu
if (timerHasExpired(timerExpire)) {
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_tcpipConnectionFailed"));
Object[] msgArgs = {currentConnectPlaceHolder.getServerName(),
Object[] msgArgs = {getServerNameString(currentConnectPlaceHolder.getServerName()),
Integer.toString(currentConnectPlaceHolder.getPortNumber()),
SQLServerException.getErrString("R_timedOutBeforeRouting")};
String msg = form.format(msgArgs);
terminate(SQLServerException.DRIVER_ERROR_UNSUPPORTED_CONFIG, msg);
} else {
// set isRoutedInCurrentAttempt to false for the next attempt
isRoutedInCurrentAttempt = false;

continue;
}
} else
Expand Down Expand Up @@ -2807,7 +2825,7 @@ void Prelogin(String serverName, int portNumber) throws SQLServerException {
+ " Unexpected end of prelogin response after " + responseBytesRead + " bytes read");
}
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_tcpipConnectionFailed"));
Object[] msgArgs = {serverName, Integer.toString(portNumber),
Object[] msgArgs = {getServerNameString(serverName), Integer.toString(portNumber),
SQLServerException.getErrString("R_notSQLServer")};
terminate(SQLServerException.DRIVER_ERROR_IO_FAILED, form.format(msgArgs));
}
Expand All @@ -2831,7 +2849,7 @@ void Prelogin(String serverName, int portNumber) throws SQLServerException {
+ preloginResponse[0]);
}
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_tcpipConnectionFailed"));
Object[] msgArgs = {serverName, Integer.toString(portNumber),
Object[] msgArgs = {getServerNameString(serverName), Integer.toString(portNumber),
SQLServerException.getErrString("R_notSQLServer")};
terminate(SQLServerException.DRIVER_ERROR_IO_FAILED, form.format(msgArgs));
}
Expand All @@ -2845,7 +2863,7 @@ void Prelogin(String serverName, int portNumber) throws SQLServerException {
+ preloginResponse[1]);
}
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_tcpipConnectionFailed"));
Object[] msgArgs = {serverName, Integer.toString(portNumber),
Object[] msgArgs = {getServerNameString(serverName), Integer.toString(portNumber),
SQLServerException.getErrString("R_notSQLServer")};
terminate(SQLServerException.DRIVER_ERROR_IO_FAILED, form.format(msgArgs));
}
Expand All @@ -2860,7 +2878,7 @@ void Prelogin(String serverName, int portNumber) throws SQLServerException {
+ responseLength + " is greater than allowed length:" + preloginResponse.length);
}
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_tcpipConnectionFailed"));
Object[] msgArgs = {serverName, Integer.toString(portNumber),
Object[] msgArgs = {getServerNameString(serverName), Integer.toString(portNumber),
SQLServerException.getErrString("R_notSQLServer")};
terminate(SQLServerException.DRIVER_ERROR_IO_FAILED, form.format(msgArgs));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ static String getErrString(String errCode) {
initCause(cause);
logException(null, errText, true);
if (Util.isActivityTraceOn()) {
// set the activityid flag so that we don't send the current ActivityId later.
ActivityCorrelator.setCurrentActivityIdSentFlag();
// set the activityid flag so that we don't send the current ActivityId later.
ActivityCorrelator.setCurrentActivityIdSentFlag();
}
}

Expand All @@ -167,15 +167,15 @@ static String getErrString(String errCode) {
initCause(cause);
logException(null, errText, true);
if (Util.isActivityTraceOn()) {
ActivityCorrelator.setCurrentActivityIdSentFlag();
ActivityCorrelator.setCurrentActivityIdSentFlag();
}
}

SQLServerException(Object obj, String errText, String errState, int errNum, boolean bStack) {
super(errText, errState, errNum);
logException(obj, errText, bStack);
if (Util.isActivityTraceOn()) {
ActivityCorrelator.setCurrentActivityIdSentFlag();
ActivityCorrelator.setCurrentActivityIdSentFlag();
}
}

Expand Down Expand Up @@ -280,9 +280,9 @@ static void ConvertConnectExceptionToSQLServerException(String hostName, int por
MessageFormat formDetail = new MessageFormat(SQLServerException.getErrString("R_tcpOpenFailed"));
Object[] msgArgsDetail = {connectException.getMessage()};
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_tcpipConnectionFailed"));
Object[] msgArgs = {hostName, Integer.toString(portNumber), formDetail.format(msgArgsDetail)};
String s = form.format(msgArgs);
SQLServerException.makeFromDriverError(conn, conn, s,
Object[] msgArgs = {conn.getServerNameString(hostName), Integer.toString(portNumber),
formDetail.format(msgArgsDetail)};
SQLServerException.makeFromDriverError(conn, conn, form.format(msgArgs),
SQLServerException.EXCEPTION_XOPEN_CONNECTION_CANT_ESTABLISH, false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ protected Object[][] getContents() {
{"R_invalidPacketSize", "The packetSize {0} is not valid."},
{"R_packetSizeTooBigForSSL",
"SSL encryption cannot be used with a network packet size larger than {0} bytes. Please check your connection properties and SQL Server configuration."},
{"R_redirectedFrom" , "{0} (redirected from {1})"},
{"R_tcpipConnectionFailed", "The TCP/IP connection to the host {0}, port {1} has failed. Error: \"{2}\"."}, // <loccomment>{PlaceHolder="TCP/IP"}</locomment>
{"R_invalidTransactionLevel", "The transaction level {0} is not valid."},
{"R_cantInvokeRollback", "Cannot invoke a rollback operation when the AutoCommit mode is set to \"true\"."},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,4 +788,16 @@ public void run() {

assertTrue(isInterrupted, TestResource.getResource("R_threadInterruptNotSet"));
}

/**
* Test calling method to get redirected server string.
*/
@Test
public void testRedirectedError() {
try (SQLServerConnection conn = getConnection()) {
assertTrue(conn.getServerNameString(null) == null);
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
}
}
}