com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure [duplicate]

This is a wrapped exception and not really interesting. It is the root cause of the exception which actually tells us something about the root cause. Please look a bit further in the stacktrace. The chance is big that you’ll then face a SQLException: Connection refused or SQLException: Connection timed out.

If this is true in your case as well, then all the possible causes are:

  1. IP address or hostname in JDBC URL is wrong.
  2. Hostname in JDBC URL is not recognized by local DNS server.
  3. Port number is missing or wrong in JDBC URL.
  4. DB server is down.
  5. DB server doesn’t accept TCP/IP connections.
  6. Something in between Java and DB is blocking connections, e.g. a firewall or proxy.

To solve the one or the either, follow the following advices:

  1. Verify and test them with ping.
  2. Refresh DNS or use IP address in JDBC URL instead.
  3. Verify it based on my.cnf of MySQL DB.
  4. Start it.
  5. Verify if mysqld is started without the --skip-networking option.
  6. Disable firewall and/or configure firewall/proxy to allow/forward the port.

By the way (and unrelated to the actual problem), you don’t necessarily need to load the JDBC driver on every getConnection() call. Just only once during startup is enough.

Leave a Comment