java报错Communications link failure 该如何解决?
天蓬老师
天蓬老师 2017-04-18 10:52:26
0
2
557
  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
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回覆(2)
PHPzhong

我來分享下我之前出現這個錯誤的解決方法吧,和1樓不一樣:

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

具體錯誤原因嘛,好像是由於jvm中的配置改變,導致jvm無法透過網路取得部分資訊

Ty80

這個應該可以解決你的問題:
Communications link failure的解決方案

使用Connector/J連接MySQL資料庫,程式運行較長時間後就會報以下錯誤:
Communications link failure,The last packet successfully received from the server was millisecond ago.The last ullypacket suessserver last ullypacket server last ullypacket server last millisecond ago。

其中錯誤也會提示你修改wait_timeout或是使用Connector/J的autoReconnect屬性避免該錯誤。
後來查了一些資料,才發現遇到這個問題的人還真不少,大部分都是使用連接池方式時才會出現這個問題,短連接應該很難出現這個問題。這個問題的原因:
MySQL伺服器預設的「wait_timeout」是28800秒即8小時,意味著如果一個連接的空閒時間超過8個小時,MySQL將自動斷開該連接,而連接池卻認為該連接還是有效的(因為並未校驗連接的有效性),當應用申請使用該連接時,就會導致上面的報錯。
修改MySQL的參數,wait_timeout最大為31536000即1年,在my.cnf中加入:
[mysqld]
wait_timeout=31536000
interactive_timeout=31536000
這兩個參數,需要修改這兩個參數,需要修改這兩個參數,需要修改這兩個參數。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!