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:26

In my case, the local loopback interface wasn't started, so "localhost" couldn't be resolved. You can check this by running "ifconfig" and you should see an interface called "lo". If it is not up, you can activate it by running "ifup lo" or "ifconfig lo up".

查看更多
人气声优
3楼-- · 2019-01-02 21:26

I've been having this issue also for about 8-9 days. Here's some background: I'm developing a simple Java application that runs in bash.

Details:

  • Spring 2.5.6
  • Hibernate3.2.3.ga
  • With maven. (The base of the project is from mkyong.com , the spring tutorial without anotations )
  • MySQL version:
[jvazquez@archbox ~]$ mysql --version
mysql  Ver 14.14 Distrib 5.5.9, for Linux (i686) using readline 5.1
Linux archbox 2.6.37-ARCH #1 SMP PREEMPT Fri Feb 18 16:58:42 UTC 2011 i686 Intel(R) Core(TM)2 Quad CPU Q8200 @ 2.33GHz GenuineIntel GNU/Linux

The application works fine in Arch Linux, Mac OS X 10.6, and FreeBSD 7.2. When I moved the jar file to another arch linux in a different host, using the same mysql, a similar my.cnf, and the similar kernel version, the connection died and obtained the same error as the original poster:

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

I tried every possible combination for this that I found on so and the forums (http://forums.mysql.com/read.php?39,180347,180347#msg-180347 for example, which is closed now and I can't post .. ), specifically:

  • Triple check that I wasn't using skip networking. (verified with ps aux and the my.cnf)
  • Tried enable log_warnings=1 in the my.cnf but obviously, I wasn't hitting the server so I didn't saw anything while using the app
  • SHOW ENGINE innodb STATUS didn't show anything at all; during the tests I could connect via shell, and php also connected to the mysql server
  • /etc/hosts has localhost 127.0.0.1
  • Tried the jdbc properties using localhost and 127.0.0.1 with no results
  • Tried adding c3p0 and changed the max_wait
  • Max connections in the my.cnf was changed to 900 , 2000 and still nothing my.cnf
  • Added wait_timeout = 60 my.cnf
  • Added net_wait_timeout = 360 my.cnf
  • Added the destroy-method="close" spring.xml

As it was pointed out (if you look up for the same exception , you will find several so threads about the issue Reproduce com.mysql.jdbc.exceptions.jdbc4.CommunicationsException with a setup of Spring, hibernate and C3P0 for example ).

  1. If you are using tomcat, please check the security exception (again, it is on SO, you will find it )
  2. Check that you can resolve that url that you are using
  3. Try adding c3p0.
  4. Verify that there isn't a firewall rejecting your connections
  5. Finally , if you are using GNU/Linux ( ARch linux for example and you indeed obtain this exception ) Try MySQL Forums :: JDBC and Java :: EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost

If the link get's removed, just add mysqld:ALL to /etc/hosts.allow

I know that is a bit extense, but it may help anybody using GNU/Linux and having this exception and this thread seemed the best place to post my research.

Hope it helps

查看更多
流年柔荑漫光年
4楼-- · 2019-01-02 21:28

I see you are connecting to a remote host. Now the question is what type of a network are you using to connect to the internet?

WINDOWS

If it's a mobile broadband device then get your machines IP address and add it to your hosting server so that your host server can allow connections coming from your machine.[your host might have turned this off due to security reasons]. Note that every time you use a different network device your IP changes.

If you are using a LAN then set a static IP address on your machine then add it to your host.

I hope this helps!! :)

查看更多
登录 后发表回答