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

Fix | Fix issues with Login Timeout not getting applied appropriately. #1049

Merged
merged 5 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 11 additions & 6 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -750,15 +750,20 @@ private Properties parseAndMergeProperties(String Url, Properties suppliedProper
throw new SQLServerException(null, SQLServerException.getErrString("R_nullConnection"), null, 0, false);
}

// Pull the URL properties into the connection properties
Properties connectProperties = Util.parseUrl(Url, drLogger);
if (connectProperties == null)
if (null == connectProperties)
return null; // If we are the wrong driver dont throw an exception

// put the user properties into the connect properties
int nTimeout = DriverManager.getLoginTimeout();
if (nTimeout > 0) {
connectProperties.put(SQLServerDriverIntProperty.LOGIN_TIMEOUT.toString(),
Integer.valueOf(nTimeout).toString());
int currentTimeout = Integer
.valueOf(connectProperties.getProperty(SQLServerDriverIntProperty.LOGIN_TIMEOUT.toString(),
String.valueOf(SQLServerDriverIntProperty.LOGIN_TIMEOUT.getDefaultValue())));
int maxTimeout = DriverManager.getLoginTimeout();

// Reset Login Timeout to Max Timeout if currentTimeout exceeds max timeout allowed.
if (maxTimeout > 0 && currentTimeout > maxTimeout) {
connectProperties.setProperty(SQLServerDriverIntProperty.LOGIN_TIMEOUT.toString(),
String.valueOf(maxTimeout));
}

// Merge connectProperties (from URL) and supplied properties from user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.junit.jupiter.api.Assertions.fail;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

Expand Down Expand Up @@ -53,6 +54,59 @@ public void testDefaultLoginTimeout() {

long timeDiff = timerEnd - timerStart;
assertTrue(timeDiff > 14000);
// Verify that login timeout does not default to max value of 60 seconds
assertTrue(timeDiff < 30000);
}

@Test
public void testURLLoginTimeout() {
long timerStart = 0;
long timerEnd = 0;

timerStart = System.currentTimeMillis();

try (Connection con = PrepUtil
.getConnection("jdbc:sqlserver://" + randomServer + ";user=sa;password=pwd;logintimeout=5")) {

} catch (Exception e) {
assertTrue(e.getMessage().contains(TestResource.getResource("R_tcpipConnectionToHost")));
timerEnd = System.currentTimeMillis();
}

assertTrue(0 != timerEnd, TestResource.getResource("R_shouldNotConnect"));

long timeDiff = timerEnd - timerStart;
assertTrue(timeDiff > 4000);

// Verify that login timeout does not default to default value of 15 seconds
assertTrue(timeDiff < 10000);
}

@Test
public void testGlobalLoginTimeout() {
long timerStart = 0;
long timerEnd = 0;
DriverManager.setLoginTimeout(30);
timerStart = System.currentTimeMillis();

try (Connection con = PrepUtil
.getConnection("jdbc:sqlserver://" + randomServer + ";user=sa;password=pwd;logintimeout=60")) {

} catch (Exception e) {
assertTrue(e.getMessage().contains(TestResource.getResource("R_tcpipConnectionToHost")));
timerEnd = System.currentTimeMillis();
}

assertTrue(0 != timerEnd, TestResource.getResource("R_shouldNotConnect"));

long timeDiff = timerEnd - timerStart;

// Verify that login timeout is not set to default value of 15 seconds
assertTrue(timeDiff > 29000);

// Verify that login timeout does not get set to URL provided value of 60 seconds
assertTrue(timeDiff < 40000);
DriverManager.setLoginTimeout(0);
}

@Test
Expand All @@ -62,9 +116,9 @@ public void testFailoverInstanceResolution() throws SQLException {

timerStart = System.currentTimeMillis();
// Try a non existing server and see if the default timeout is 15 seconds
try (Connection con = PrepUtil
.getConnection("jdbc:sqlserver://" + randomServer + ";databaseName=FailoverDB_abc;failoverPartner="
+ randomServer + "\\foo;user=sa;password=pwd;")) {} catch (Exception e) {
try (Connection con = PrepUtil.getConnection("jdbc:sqlserver://" + randomServer
+ ";databaseName=FailoverDB_abc;failoverPartner=" + randomServer + "\\foo;user=sa;password=pwd;")) {
} catch (Exception e) {
assertTrue(e.getMessage().contains(TestResource.getResource("R_tcpipConnectionToHost")));
timerEnd = System.currentTimeMillis();
}
Expand All @@ -80,9 +134,9 @@ public void testFOInstanceResolution2() throws SQLException {
long timerEnd = 0;

timerStart = System.currentTimeMillis();
try (Connection con = PrepUtil
.getConnection("jdbc:sqlserver://" + randomServer + "\\fooggg;databaseName=FailoverDB;failoverPartner="
+ randomServer + "\\foo;user=sa;password=pwd;")) {} catch (Exception e) {
try (Connection con = PrepUtil.getConnection("jdbc:sqlserver://" + randomServer
+ "\\fooggg;databaseName=FailoverDB;failoverPartner=" + randomServer + "\\foo;user=sa;password=pwd;")) {
} catch (Exception e) {
timerEnd = System.currentTimeMillis();
}
assertTrue(0 != timerEnd, TestResource.getResource("R_shouldNotConnect"));
Expand Down