Skip to content

Commit 17caf22

Browse files
authored
[Backport] SocketTimeout should be unbounded by loginTimeout after a successful connection open (2355) (#2431)
* socketTimeout should reset to original value after a successful connection open (#2355) * SocketTimeout should be unbounded by loginTimeout after a successful connection open
1 parent d9c6cdb commit 17caf22

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
1414
- Clear prepared statement handle before reconnect [#2422](https://github.com/microsoft/mssql-jdbc/pull/2422)
1515
- RPC calls for CallableStatements will be executed directly [#2427](https://github.com/microsoft/mssql-jdbc/pull/2427)
1616
- Corrected authentication token object to accept expiration in milliseconds [#2428](https://github.com/microsoft/mssql-jdbc/pull/2428)
17+
- SocketTimeout should be unbounded by loginTimeout after a successful connection open [#2431](https://github.com/microsoft/mssql-jdbc/pull/2431)
1718

1819
## [12.6.1] Hotfix & Stable Release
1920
### Fixed issues

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,6 +2366,10 @@ final int getNetworkTimeout() throws IOException {
23662366
final void setNetworkTimeout(int timeout) throws IOException {
23672367
tcpSocket.setSoTimeout(timeout);
23682368
}
2369+
2370+
void resetTcpSocketTimeout() throws SocketException {
2371+
this.tcpSocket.setSoTimeout(con.getSocketTimeoutMilliseconds());
2372+
}
23692373
}
23702374

23712375

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,9 +3225,15 @@ else if (0 == requestedPacketSize)
32253225

32263226
state = State.OPENED;
32273227

3228+
// Socket timeout is bounded by loginTimeout during the login phase.
3229+
// Reset socket timeout back to the original value.
3230+
tdsChannel.resetTcpSocketTimeout();
3231+
32283232
if (connectionlogger.isLoggable(Level.FINER)) {
32293233
connectionlogger.finer(toString() + " End of connect");
32303234
}
3235+
} catch (SocketException e) {
3236+
throw new SQLServerException(e.getMessage(), null);
32313237
} finally {
32323238
// once we exit the connect function, the connection can be only in one of two
32333239
// states, Opened or Closed(if an exception occurred)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,18 @@ public void testConnectRetryTimeout() {
281281
"total time: " + totalTime + " interval: " + TimeUnit.SECONDS.toMillis(interval));
282282
}
283283

284+
@Test
285+
public void testSocketTimeoutBoundedByLoginTimeoutReset() throws Exception {
286+
try (Connection con = PrepUtil.getConnection(connectionString + ";socketTimeout=90000;loginTimeout=10;");
287+
Statement stmt = con.createStatement()) {
288+
// Login timeout (10s) is less than the 15s sec WAITFOR DELAY. Upon a login attempt, socketTimeout should be bounded
289+
// by loginTimeout. After a successful login, when executing a query, socketTimeout should be reset to the
290+
// original 90000ms timeout. The statement below should successfully execute as socketTimeout should not be bounded
291+
// by loginTimeout, otherwise the test fails with a socket read timeout error.
292+
stmt.execute("WAITFOR DELAY '00:00:15';");
293+
}
294+
}
295+
284296
// Test for detecting Azure server for connection retries
285297
@Test
286298
public void testAzureEndpointRetry() {

0 commit comments

Comments
 (0)