Bug #104684 Request option for enabling TCP KeepAlive on Connector/J
Submitted: 23 Aug 2021 0:04 Modified: 23 Aug 2021 6:46
Reporter: Kazuki Matsuda Email Updates:
Status: Not a Bug Impact on me:
Category:Connector / J Severity:S4 (Feature request)
Version: OS:Any
Assigned to: CPU Architecture:Any

[23 Aug 2021 0:04] Kazuki Matsuda
For unexpected MySQL server down/hung, Connector/J has socketTimeout option.
But due to this option is applied for all Query execution, it must be set to a value corresponding to the longest Query execution time.

In our case, it is about 1 minutes, and the detection of OS crashes is delayed by that amount.

There are two ways to detect that a server has become unavailable.

1) Heart beat in Application Layer.
2) Enable TCP Keep Alive.

It's hard to implement Heart Beat.
However, with the support of the OS, TCP Keep Alive can be used with only a configuration change, and it can efficiently tell when a server is down.

If we enable TCP Keep Alive on socket, client side OS sends heart beat packet automatically and server side OS responds automatically.

Generally, many documents recommends that use TCP Keep Alive with caution. https://en.wikipedia.org/wiki/Keepalive#:~:text=and%20if%20included%20must%20default%20to%....

But those documents targeted for internet connection, and I think TCP Keep Alive is good for LAN between Application Server to MySQL Server.

How to repeat:
1. Start mysql server.
2. Connect MySQL Server from Java Application using Connector/J.
3. Execute long running query.
4. Unplug the server.
5. Java Application waiting response and connection is hung.

Suggested fix:
1. Add new option for enabling TCP Keep Alive. like tcpKeepAlive=(int)
2. Enable TCP Keep Alive for socket in Connector/J
[23 Aug 2021 6:46] MySQL Verification Team
Hello Kazuki-San,

Thank you for the feature request.
Imho this feature already exists i.e. tcpKeepAlive since 5.0.7 - Please see https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-connp-props-networking.html