Reason: There are various reasons due to which a "communication link failure" exception can occur.



Some of the reasons are listed below:

  • Any network connectivity issues.
  • The IP address or hostname in the JDBC URL is wrong.
  • Hostname in JDBC URL is not recognized by the local DNS server.
  • Port number is missing or wrong in JDBC URL.
  • The database server is down.
  • The database server does not accept TCP/IP connections.
  • The database server has run out of connections.
  • Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
  • A non-compatible version of the JDBC driver is used.
  • An incorrect JDBC driver from a vendor.


Solution:

Here's a compilation of a list of solutions below, one of the following solutions can help to resolve the issue:

  • Verify and test the connection with ping.
  • Refresh DNS or use an IP address in JDBC URL instead.
  • Verify if mysqld is started without the --skip-networking option.
  • Restart the database service.
  • If you are using a custom code/script, then check how you are closing the database connection. If required, kindly fix your code.
  • Disable firewall and/or configure firewall/proxy to allow/forward the port.
  • Try a different version of JDBC driver.
  • Try a JDBC driver from another vendor.
  • changing "bind-address" attribute
Uncomment "bind-address" attribute or change it to one of the following IPs:
bind-address="127.0.0.1"
or,
bind-address="0.0.0.0"
  • commenting out "skip-networking"
If there is a "skip-networking" line in your MySQL config file, make it comment by adding "#" sign at the beginning of that line.
  • increase "wait_timeout", "interactive_timeout", "connect_timeout" values in mysql db setting file my.ini
Add these lines to the MySQL config file:
wait_timeout = number (wait_timeout=2814400)
interactive_timeout = number
connect_timeout = number
  • Make sure Java isn't translating 'localhost' to [:::1] instead of [127.0.0.1]

This could be avoided by using one of two approaches:

  1. In a connection string, use 127.0.0.1 instead of localhost to avoid localhost being translated to :::1
  2. Run java with the option -Djava.net.preferIPv4Stack=true to force java to use IPv4 instead of IPv6. On Linux, this could also be achieved by adding an entry in /etc/profile or /home/username/.bashrc file:
export _JAVA_OPTIONS="-Djava.net.preferIPv4Stack=true"
  • AutoReconnect Connection String option

Append this code to your connection string at the end:

&autoReconnect=true&failOverReadOnly=false&maxReconnects=10


Notes:

For the solutions that you need to change the MySQL settings, you can refer to the following files:

  • Linux: /etc/mysql/my.cnf or /etc/my.cnf (depending on the Linux distribution and MySQL package used)
  • Windows: C:**ProgramData**\MySQL\MySQL Server 5.6\my.ini (Notice it's ProgramData, not Program Files)


References:

For further read, please refer to these articles: