com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解決辦法
阿新 • • 發佈:2019-01-28
09:00:30.307 [http-8080-6] ERROR org.hibernate.transaction.JDBCTransaction -JDBC begin failed com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet successfully received from the server was 56,237,340 milliseconds ago. The last packet sent successfully to the server was 1 milliseconds ago. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.6.0_13] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) ~[na:1.6.0_13] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) ~[na:1.6.0_13] at java.lang.reflect.Constructor.newInstance(Constructor.java:513) ~[na:1.6.0_13] at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.21.jar:na] at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117) ~[mysql-connector-java-5.1.21.jar:na] at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3567) ~[mysql-connector-java-5.1.21.jar:na] at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3456) ~[mysql-connector-java-5.1.21.jar:na] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3997) ~[mysql-connector-java-5.1.21.jar:na] at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468) ~[mysql-connector-java-5.1.21.jar:na] at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629) ~[mysql-connector-java-5.1.21.jar:na] at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2713) ~[mysql-connector-java-5.1.21.jar:na] at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:5060) ~[mysql-connector-java-5.1.21.jar:na] at org.apache.commons.dbcp.DelegatingConnection.setAutoCommit(DelegatingConnection.java:371) ~[commons-dbcp-1.4.jar:1.4] at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.setAutoCommit(PoolingDataSource.java:328) ~[commons-dbcp-1.4.jar:1.4] at sun.reflect.GeneratedMethodAccessor55.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_13] at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_13] at net.bull.javamelody.JdbcWrapper$ConnectionInvocationHandler.invoke(JdbcWrapper.java:185) ~[javamelody-1.49.0.jar:na] at net.bull.javamelody.JdbcWrapper$DelegatingInvocationHandler.invoke(JdbcWrapper.java:282) ~[javamelody-1.49.0.jar:na] at $Proxy36.setAutoCommit(Unknown Source) ~[na:na] at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:91) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final] at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1463) [hibernate-core-3.5.6-Final.jar:3.5.6-Final] at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE] at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371) [spring-tx-3.1.1.RELEASE.jar:3.1.1.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335) [spring-tx-3.1.1.RELEASE.jar:3.1.1.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105) [spring-tx-3.1.1.RELEASE.jar:3.1.1.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE] at $Proxy72.findPageByORMSearch(Unknown Source) [na:na]
網上看到這兩個解決辦法。驗證中。
一是修改my.cnf: [mysqld] wait_timeout=31536000 interactive_timeout=31536000 將過期時間修改為1年。 二是在連線URL上新增引數:&autoReconnect=true&failOverReadOnly=false 最終解決辦法:URL新增引數貌似不管用,我用的是mysql5.6,網上說這個5以前的版本才有效。所以只能使用改mysql配置的方法了。 上述問題是由mysql5資料庫的配置引起的。mysql5將其連線的等待時間(wait_timeout)預設為8小時。在其客戶程式中可以這樣來檢視其值:mysql﹥
mysql﹥ show global variables like 'wait_timeout';
+---------------+---------+
| Variable_name | Value |
+---------------+---------+
| wait_timeout | 28800 |
+---------------+---------+
1 row in set (0.00 sec)
28800 seconds,也就是8小時。
如果在wait_timeout秒期間內,資料庫連線(java.sql.Connection)一直處於等待狀態,mysql5就將該連線關閉。這時,你的Java應用的連線池仍然合法地持有該連線的引用。當用該連線來進行資料庫操作時,就碰到上述錯誤。這解釋了為什麼我的程式第二天不能登入 的問題。
本人覺得最簡單的辦法,就是對症下藥:既然問題是由mysql5的全域性變數wait_timeout的預設值太小引起的,我們將其改大就好了。
檢視mysql5的手冊,發現對wait_timeout的最大值分別是24天/365天(windows/linux)。以windows為 例,假設我們要將其設為21天,我們只要修改mysql5的配置檔案“my.ini”(mysql5 installation dir),增加一行:wait_timeout=1814400
需要重新啟動mysql5。
linux系統配置檔案:/etc/my.cnf
測試顯示問題解決了。