From 9b9336589bd8a32e62f31817d99c32fec8accf79 Mon Sep 17 00:00:00 2001 From: Suraiya Hameed Date: Thu, 1 Jun 2017 11:56:17 -0700 Subject: [PATCH 1/4] get hostname before TDSChannel opens --- .../sqlserver/jdbc/SQLServerConnection.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java index b003124fc..6b95133d6 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java @@ -44,10 +44,10 @@ import java.util.Map; import java.util.Properties; import java.util.UUID; +import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.ConcurrentLinkedQueue; import java.util.logging.Level; import javax.sql.XAConnection; @@ -223,6 +223,8 @@ ServerPortPlaceHolder getRoutingInfo() { // is // false). + private static String hostName = null; + boolean sendStringParametersAsUnicode() { return sendStringParametersAsUnicode; } @@ -1991,6 +1993,15 @@ private void connectHelper(ServerPortPlaceHolder serverInfo, connectionlogger.fine(toString() + " Connecting with server: " + serverInfo.getServerName() + " port: " + serverInfo.getPortNumber() + " Timeout slice: " + timeOutsliceInMillis + " Timeout Full: " + timeOutFullInSeconds); } + + // Before opening the TDSChannel, calculate local hostname + // as the InetAddress.getLocalHost() takes more than usual time in certian OS and JVM combination, + // get the hostName prior to opening TDSChannel, it avoids connection loss + hostName = activeConnectionProperties.getProperty(SQLServerDriverStringProperty.WORKSTATION_ID.toString()); + if (hostName == null || hostName.length() == 0) { + hostName = Util.lookupHostName(); + } + // if the timeout is infinite slices are infinite too. tdsChannel = new TDSChannel(this); if (0 == timeOutFullInSeconds) @@ -4015,7 +4026,6 @@ final boolean complete(LogonCommand logonCommand, // Fed Auth feature requested without specifying fedAuthFeatureExtensionData. assert (null != fedAuthFeatureExtensionData || !(federatedAuthenticationInfoRequested || federatedAuthenticationRequested)); - String hostName = activeConnectionProperties.getProperty(SQLServerDriverStringProperty.WORKSTATION_ID.toString()); String sUser = activeConnectionProperties.getProperty(SQLServerDriverStringProperty.USER.toString()); String sPwd = activeConnectionProperties.getProperty(SQLServerDriverStringProperty.PASSWORD.toString()); String appName = activeConnectionProperties.getProperty(SQLServerDriverStringProperty.APPLICATION_NAME.toString()); @@ -4034,10 +4044,6 @@ final boolean complete(LogonCommand logonCommand, if (serverName != null && serverName.length() > 128) serverName = serverName.substring(0, 128); - if (hostName == null || hostName.length() == 0) { - hostName = Util.lookupHostName(); - } - byte[] secBlob = new byte[0]; boolean[] done = {false}; if (null != authentication) { From fed073c55fe2c528ce3021979bb1d3b63557c110 Mon Sep 17 00:00:00 2001 From: Suraiya Hameed Date: Thu, 1 Jun 2017 12:35:17 -0700 Subject: [PATCH 2/4] update comments --- .../java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java index 6b95133d6..af1619bc7 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java @@ -1995,8 +1995,7 @@ private void connectHelper(ServerPortPlaceHolder serverInfo, } // Before opening the TDSChannel, calculate local hostname - // as the InetAddress.getLocalHost() takes more than usual time in certian OS and JVM combination, - // get the hostName prior to opening TDSChannel, it avoids connection loss + // as the InetAddress.getLocalHost() takes more than usual time in certian OS and JVM combination, it avoids connection loss hostName = activeConnectionProperties.getProperty(SQLServerDriverStringProperty.WORKSTATION_ID.toString()); if (hostName == null || hostName.length() == 0) { hostName = Util.lookupHostName(); From 895b863605912603863c0c82abaea98865806ad8 Mon Sep 17 00:00:00 2001 From: Suraiya Hameed Date: Fri, 2 Jun 2017 15:06:49 -0700 Subject: [PATCH 3/4] Update SQLServerConnection.java --- .../java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java index af1619bc7..662ac745c 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java @@ -1995,7 +1995,7 @@ private void connectHelper(ServerPortPlaceHolder serverInfo, } // Before opening the TDSChannel, calculate local hostname - // as the InetAddress.getLocalHost() takes more than usual time in certian OS and JVM combination, it avoids connection loss + // as the InetAddress.getLocalHost() takes more than usual time in certain OS and JVM combination, it avoids connection loss hostName = activeConnectionProperties.getProperty(SQLServerDriverStringProperty.WORKSTATION_ID.toString()); if (hostName == null || hostName.length() == 0) { hostName = Util.lookupHostName(); From 903864aa0f528b997a08dde1957348a8a861f827 Mon Sep 17 00:00:00 2001 From: Suraiya Hameed Date: Fri, 2 Jun 2017 15:16:18 -0700 Subject: [PATCH 4/4] switch to StringUtils --- .../java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java index 662ac745c..566c42b75 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java @@ -1997,7 +1997,7 @@ private void connectHelper(ServerPortPlaceHolder serverInfo, // Before opening the TDSChannel, calculate local hostname // as the InetAddress.getLocalHost() takes more than usual time in certain OS and JVM combination, it avoids connection loss hostName = activeConnectionProperties.getProperty(SQLServerDriverStringProperty.WORKSTATION_ID.toString()); - if (hostName == null || hostName.length() == 0) { + if (StringUtils.isEmpty(hostName)) { hostName = Util.lookupHostName(); }