Skip to content

Commit dc33cde

Browse files
Fix | Fix issues with Login Timeout not getting applied appropriately.
1 parent 51d6c40 commit dc33cde

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -750,15 +750,20 @@ private Properties parseAndMergeProperties(String Url, Properties suppliedProper
750750
throw new SQLServerException(null, SQLServerException.getErrString("R_nullConnection"), null, 0, false);
751751
}
752752

753+
// Pull the URL properties into the connection properties
753754
Properties connectProperties = Util.parseUrl(Url, drLogger);
754-
if (connectProperties == null)
755+
if (null == connectProperties)
755756
return null; // If we are the wrong driver dont throw an exception
756757

757-
// put the user properties into the connect properties
758-
int nTimeout = DriverManager.getLoginTimeout();
759-
if (nTimeout > 0) {
760-
connectProperties.put(SQLServerDriverIntProperty.LOGIN_TIMEOUT.toString(),
761-
Integer.valueOf(nTimeout).toString());
758+
int currentTimeout = Integer
759+
.valueOf(connectProperties.getProperty(SQLServerDriverIntProperty.LOGIN_TIMEOUT.toString(),
760+
String.valueOf(SQLServerDriverIntProperty.LOGIN_TIMEOUT.getDefaultValue())));
761+
int maxTimeout = DriverManager.getLoginTimeout();
762+
763+
// Reset Login Timeout to Max Timeout if currentTimeout exceeds max timeout allowed.
764+
if (maxTimeout > 0 && currentTimeout > maxTimeout) {
765+
connectProperties.setProperty(SQLServerDriverIntProperty.LOGIN_TIMEOUT.toString(),
766+
String.valueOf(maxTimeout));
762767
}
763768

764769
// Merge connectProperties (from URL) and supplied properties from user.

src/test/java/com/microsoft/sqlserver/jdbc/connection/TimeoutTest.java

+60-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.junit.jupiter.api.Assertions.fail;
1010

1111
import java.sql.Connection;
12+
import java.sql.DriverManager;
1213
import java.sql.SQLException;
1314
import java.sql.Statement;
1415

@@ -53,6 +54,59 @@ public void testDefaultLoginTimeout() {
5354

5455
long timeDiff = timerEnd - timerStart;
5556
assertTrue(timeDiff > 14000);
57+
// Verify that login timeout does not default to max value of 60 seconds
58+
assertTrue(timeDiff < 30000);
59+
}
60+
61+
@Test
62+
public void testURLLoginTimeout() {
63+
long timerStart = 0;
64+
long timerEnd = 0;
65+
66+
timerStart = System.currentTimeMillis();
67+
68+
try (Connection con = PrepUtil
69+
.getConnection("jdbc:sqlserver://" + randomServer + ";user=sa;password=pwd;logintimeout=5")) {
70+
71+
} catch (Exception e) {
72+
assertTrue(e.getMessage().contains(TestResource.getResource("R_tcpipConnectionToHost")));
73+
timerEnd = System.currentTimeMillis();
74+
}
75+
76+
assertTrue(0 != timerEnd, TestResource.getResource("R_shouldNotConnect"));
77+
78+
long timeDiff = timerEnd - timerStart;
79+
assertTrue(timeDiff > 4000);
80+
81+
// Verify that login timeout does not default to default value of 15 seconds
82+
assertTrue(timeDiff < 10000);
83+
}
84+
85+
@Test
86+
public void testGlobalLoginTimeout() {
87+
long timerStart = 0;
88+
long timerEnd = 0;
89+
DriverManager.setLoginTimeout(30);
90+
timerStart = System.currentTimeMillis();
91+
92+
try (Connection con = PrepUtil
93+
.getConnection("jdbc:sqlserver://" + randomServer + ";user=sa;password=pwd;logintimeout=60")) {
94+
95+
} catch (Exception e) {
96+
assertTrue(e.getMessage().contains(TestResource.getResource("R_tcpipConnectionToHost")));
97+
timerEnd = System.currentTimeMillis();
98+
}
99+
100+
assertTrue(0 != timerEnd, TestResource.getResource("R_shouldNotConnect"));
101+
102+
long timeDiff = timerEnd - timerStart;
103+
104+
// Verify that login timeout is not set to default value of 15 seconds
105+
assertTrue(timeDiff > 29000);
106+
107+
// Verify that login timeout does not get set to URL provided value of 60 seconds
108+
assertTrue(timeDiff < 40000);
109+
DriverManager.setLoginTimeout(0);
56110
}
57111

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

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

82136
timerStart = System.currentTimeMillis();
83-
try (Connection con = PrepUtil
84-
.getConnection("jdbc:sqlserver://" + randomServer + "\\fooggg;databaseName=FailoverDB;failoverPartner="
85-
+ randomServer + "\\foo;user=sa;password=pwd;")) {} catch (Exception e) {
137+
try (Connection con = PrepUtil.getConnection("jdbc:sqlserver://" + randomServer
138+
+ "\\fooggg;databaseName=FailoverDB;failoverPartner=" + randomServer + "\\foo;user=sa;password=pwd;")) {
139+
} catch (Exception e) {
86140
timerEnd = System.currentTimeMillis();
87141
}
88142
assertTrue(0 != timerEnd, TestResource.getResource("R_shouldNotConnect"));

0 commit comments

Comments
 (0)