Skip to content

Commit

Permalink
Ensure socketTimeout is < loginTimeout before connected (#2280)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgreenbird authored Jan 3, 2024
1 parent 880b9b8 commit 5ca34bd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,13 @@ final InetSocketAddress open(String host, int port, int timeoutMillis, boolean u
tcpSocket.setKeepAlive(true);
setSocketOptions(tcpSocket, this);

// set SO_TIMEOUT
int socketTimeout = con.getSocketTimeoutMilliseconds();

// socket timeout should be bounded by loginTimeout before connected
if (!con.isConnected()) {
socketTimeout = Math.min(con.timerRemaining(con.timerExpire), socketTimeout);
}

tcpSocket.setSoTimeout(socketTimeout);

inputStream = tcpInputStream = new ProxyInputStream(tcpSocket.getInputStream());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,7 @@ final int getRetryCount() {
return connectRetryCount;
}

final boolean attachConnId() {
final boolean isConnected() {
return state.equals(State.CONNECTED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ static String generateStateCode(SQLServerConnection con, int errNum, Integer dat
* original error string.
*/
static String checkAndAppendClientConnId(String errMsg, SQLServerConnection conn) {
if (null != conn && conn.attachConnId()) {
if (null != conn && conn.isConnected()) {
UUID clientConnId = conn.getClientConIdInternal();
assert null != clientConnId;
StringBuilder sb = new StringBuilder(errMsg);
Expand Down

0 comments on commit 5ca34bd

Please sign in to comment.