A JDBC login can complete and still hang while Connector/J reads server variables, character sets, isolation level, or time-zone information. connectTimeout covers the socket connection; add socketTimeout and a pool-level acquisition deadline to bound the full operation.
Last updated: November 7, 2026.
String url = "jdbc:mysql://db.example:3306/app"
+ "?connectTimeout=5000"
+ "&socketTimeout=10000"
+ "&tcpKeepAlive=true";
HikariConfig config = new HikariConfig();
config.setJdbcUrl(url);
config.setUsername(System.getenv("DB_USER"));
config.setPassword(System.getenv("DB_PASSWORD"));
config.setConnectionTimeout(15000);The three deadlines cover different layers. The socket-connect limit bounds the TCP handshake, the socket limit bounds network reads, and the pool limit bounds how long application code waits to obtain a usable connection.
Use the right timeout for each phase
MySQL’s Connector/J networking reference defines connectTimeout as the socket-connect timeout and socketTimeout as the timeout for network socket operations. Both default to zero, meaning no timeout. Set finite values that exceed normal database latency but remain below the request or job deadline.
A current Connector/J report notes that authentication may finish before initialization queries run. This is why a login timeout alone is not a complete safety boundary. The pool deadline provides an outer limit while driver behavior is reviewed or updated.
Treat a timed-out connection as unusable
When initialization times out, close or evict that physical connection. Do not return it to the pool, because the protocol stream may contain an unread response. Retry only through the pool with a bounded backoff, and cap total attempts within the caller’s deadline.
Log the driver version, target host, elapsed phase, SQLState, and exception class without recording credentials. Test by blocking traffic after authentication and confirm that the application fails within the expected bound. Also verify that your pool’s validation query and startup checks cannot wait longer than the same budget.
For related connection failures, see preventing JDBC hangs, discarding canceled MySQL connections, and recovering from MySQL disconnects.