The timeout passed to JDBC Connection.isValid() is intended to bound validation, but a current Microsoft driver report shows a silent TCP failure can still hang when socketTimeout=0. Configure a finite socket timeout and coordinate it with query and cancellation limits.
Last updated: November 14, 2026.
String url = "jdbc:sqlserver://db.example.com:1433;"
+ "databaseName=app;"
+ "encrypt=true;"
+ "loginTimeout=15;"
+ "socketTimeout=30000;"
+ "queryTimeout=20;"
+ "cancelQueryTimeout=5;";
try (Connection connection = DriverManager.getConnection(url, properties)) {
if (!connection.isValid(5)) {
throw new SQLException("SQL Server connection validation failed");
}
// Use the connection.
}socketTimeout is measured in milliseconds; the other timeout values shown here are seconds. Choose values from your service latency budget rather than copying them unchanged.
Use the timeout that covers socket I/O
Microsoft’s JDBC timeout guide states that socketTimeout applies to all driver communication. Its default is zero, meaning an infinite wait. A nonzero value causes the driver to close the connection when the server stops acknowledging or replying.
The same guide recommends making socketTimeout greater than queryTimeout and cancelQueryTimeout. The isValid documentation still defines its argument as seconds to wait for validation, so keep using a sensible validation timeout; the socket timeout is an additional transport boundary, not a replacement.
Configure the connection pool too
Set finite acquisition and validation limits in the pool so one failed endpoint cannot exhaust application threads. When validation fails or a socket timeout closes the connection, let the pool discard it and create a replacement. Do not return it to normal use or retry an uncertain write automatically.
The reported hang concerns Microsoft JDBC Driver 13.4.0.jre11 with Java 11 and socketTimeout=0. It remains under investigation, so do not generalize it to every driver version. Reproduce network loss in a non-production environment, record the driver version, and verify that validation returns or throws within your configured upper bound. Monitor validation latency as well as failures.