Home  >  Q&A  >  body text

java报错Communications link failure 该如何解决?

  1. 就是普通的查询mysql数据库的代码:

  2. 错误栈如下:

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.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2209)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:284)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at testHttpConnection.util.DBUtil.getConn(DBUtil.java:26)
    at testHttpConnection.TestMysql.main(TestMysql.java:23)
Caused by: 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.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:675)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1078)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2137)
    ... 13 more
Caused by: java.net.SocketException: Permission denied: recv failed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:152)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:113)
    at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:160)
    at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:188)
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2494)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:599)
    ... 15 more
天蓬老师天蓬老师2765 days ago562

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-18 10:54:26

    Let me share the solution to this error I had before, which is different from the first floor:

    eclipse中,设置“Default VM arguments”添加:“-Djava.net.preferIPv4Stack=true”
    

    The specific cause of the error seems to be due to configuration changes in the jvm, which prevents the jvm from obtaining some information through the network

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:54:26

    This should solve your problem:
    Solution to Communications link failure

    Use Connector/J to connect to the MySQL database. After the program runs for a long time, the following error will be reported:
    Communications link failure, The last packet successfully received from the server was millisecond ago.The last packet successfully sent to the server was millisecond ago.

    The error will also prompt you to modify wait_timeout or use the autoReconnect attribute of Connector/J to avoid this error.
    After checking some information, I found that there are quite a few people who have encountered this problem. Most of them have this problem when using the connection pool method. It should be difficult to have this problem with short connections. The reason for this problem:
    The default "wait_timeout" of the MySQL server is 28800 seconds or 8 hours, which means that if a connection is idle for more than 8 hours, MySQL will automatically disconnect the connection, but the connection pool considers the connection to be still valid. (because the validity of the connection is not verified), when the application applies to use the connection, it will cause the above error.
    Modify the parameters of MySQL. The maximum wait_timeout is 31536000, which is 1 year. Add to my.cnf:
    [mysqld]
    wait_timeout=31536000
    interactive_timeout=31536000
    Restart to take effect. You need to modify these two parameters at the same time.

    reply
    0
  • Cancelreply