com.mysql.jdbc.exceptions.jdbc4.CommunicationsExce

2019-01-02 20:59发布

My program that connects to a MySQL database was working fine. Then, without changing any code used to set up the connection, I get this exception:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

What happened?

The code used to get the connection:

private static Connection getDBConnection() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    String username = "user";
    String password = "pass";
    String url = "jdbc:mysql://www.domain.com:3306/dbName?connectTimeout=3000";

    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
}

标签: java mysql jdbc
15条回答
何处买醉
2楼-- · 2019-01-02 21:01

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.

查看更多
牵手、夕阳
3楼-- · 2019-01-02 21:05

The escential problem is that Mysql JDBC pool connections is not used, then the Timeout from Mysql, close the Connections. You need change the pool Parameters to get restart connection when the connection has failures, on this way:

Connection Validation: Required (Check)
Validation Method: autocommit

You can change the Validation Method if you cannot get it works!

查看更多
不流泪的眼
4楼-- · 2019-01-02 21:07

If you use WAMP, make sure it is online. What I did was, first turned my firewall off, then it worked, so after that I allowed connection for all local ports, specially port 80. Than I got rid of this problem. For me it was the Firewall who was blocking the connection.

查看更多
长期被迫恋爱
5楼-- · 2019-01-02 21:09

Ensure skip-networking is commented out in my.cnf/my.ini

查看更多
查无此人
6楼-- · 2019-01-02 21:11

check your wait timeout set on the DB server. Some times it defaults to 10 seconds. This looses the connection in 10 seconds.

mysql> show global variables like '%time%' ;

update it make it something like 28800

mysql> SET GLOBAL wait_timeout = 28800;
查看更多
冷夜・残月
7楼-- · 2019-01-02 21:15

As BalusC mentioned, it would be very useful to post the full stacktrace (always post a full stacktrace, it is useless and frustrating to have only the first lines of a stacktrace).

Anyway, you mentioned that your code was working fine and that this problem started suddenly to occur without any code change so I'm wondering if this could be related to you other question Problem with not closing db connection while debugging? Actually, if this problem started while debugging, then I think it is (you ran out of connections). In that case, restart you database server (and follow the suggestions of the other question to avoid this situation).

查看更多
登录 后发表回答