Skip to content

Commit

Permalink
socketTimeout should reset to original value after a successful conne…
Browse files Browse the repository at this point in the history
…ction open (#2355)
  • Loading branch information
tkyc committed Mar 26, 2024
1 parent ed7d2c8 commit 7e0dcb3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,10 @@ final int getNetworkTimeout() throws IOException {
final void setNetworkTimeout(int timeout) throws IOException {
tcpSocket.setSoTimeout(timeout);
}

void resetTcpSocketTimeout() throws SocketException {
this.tcpSocket.setSoTimeout(con.getSocketTimeoutMilliseconds());
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3210,9 +3210,15 @@ else if (0 == requestedPacketSize)

state = State.OPENED;

// Socket timeout is bounded by loginTimeout during the login phase.
// Reset socket timeout back to the original value.
tdsChannel.resetTcpSocketTimeout();

if (connectionlogger.isLoggable(Level.FINER)) {
connectionlogger.finer(toString() + " End of connect");
}
} catch (SocketException e) {
throw new SQLServerException(e.getMessage(), null);
} finally {
// once we exit the connect function, the connection can be only in one of two
// states, Opened or Closed(if an exception occurred)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,18 @@ public void testConnectRetryTimeout() {
"total time: " + totalTime + " interval: " + TimeUnit.SECONDS.toMillis(interval));
}

@Test
public void testSocketTimeoutBoundedByLoginTimeoutReset() throws Exception {
try (Connection con = PrepUtil.getConnection(connectionString + ";socketTimeout=90000;loginTimeout=10;");
Statement stmt = con.createStatement()) {
// Login timeout (10s) is less than the 15s sec WAITFOR DELAY. Upon a login attempt, socketTimeout should be bounded
// by loginTimeout. After a successful login, when executing a query, socketTimeout should be reset to the
// original 90000ms timeout. The statement below should successfully execute as socketTimeout should not be bounded
// by loginTimeout, otherwise the test fails with a socket read timeout error.
stmt.execute("WAITFOR DELAY '00:00:15';");
}
}

// Test for detecting Azure server for connection retries
@Test
public void testAzureEndpointRetry() {
Expand Down

0 comments on commit 7e0dcb3

Please sign in to comment.