java報錯Communications link failure 該如何解決?
問題描述
就是普通的查詢mysql數(shù)據(jù)庫的代碼:
錯誤棧如下:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failureThe 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 failureThe 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 moreCaused 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
問題解答
回答1:我來分享下我之前出現(xiàn)這個錯誤的解決辦法吧,和1樓不一樣:
eclipse中,設(shè)置“Default VM arguments”添加:“-Djava.net.preferIPv4Stack=true”
具體錯誤原因嘛,好像是由于jvm中的配置改變,導(dǎo)致jvm無法通過網(wǎng)絡(luò)獲取部分信息
回答2:這個應(yīng)該能解決你的問題: Communications link failure的解決辦法
使用Connector/J連接MySQL數(shù)據(jù)庫,程序運(yùn)行較長時間后就會報以下錯誤: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。
其中錯誤還會提示你修改wait_timeout或是使用Connector/J的autoReconnect屬性避免該錯誤。后來查了一些資料,才發(fā)現(xiàn)遇到這個問題的人還真不少,大部分都是使用連接池方式時才會出現(xiàn)這個問題,短連接應(yīng)該很難出現(xiàn)這個問題。這個問題的原因:MySQL服務(wù)器默認(rèn)的“wait_timeout”是28800秒即8小時,意味著如果一個連接的空閑時間超過8個小時,MySQL將自動斷開該連接,而連接池卻認(rèn)為該連接還是有效的(因?yàn)椴⑽葱r?yàn)連接的有效性),當(dāng)應(yīng)用申請使用該連接時,就會導(dǎo)致上面的報錯。修改MySQL的參數(shù),wait_timeout最大為31536000即1年,在my.cnf中加入:[mysqld]wait_timeout=31536000interactive_timeout=31536000重啟生效,需要同時修改這兩個參數(shù)。
相關(guān)文章:
1. php - 想要遠(yuǎn)程推送emjio ios端怎么搞 需要怎么配合2. python沒入門,請教一個問題3. javascript - swiper輪播圖使用左右布局高度不一致4. mysql - 關(guān)于時間的入庫問題,大神們你們存數(shù)據(jù)庫的時間是取本地的時間,還是取utc的時間?5. python - 如何把152753這個字符串轉(zhuǎn)變成時間格式15:27:536. mysql - 類似于之類的通知系統(tǒng)如何設(shè)計數(shù)據(jù)庫7. mysql優(yōu)化 - mysql EXPLAIN之后怎么看結(jié)果進(jìn)行優(yōu)化 ?8. python - 數(shù)據(jù)無法插入到mysql表里9. Javascript 比較不同編碼的字符串10. python - 關(guān)于beautifulsoup獲取文檔內(nèi)容
