com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决办法-阿里云开发者社区

开发者社区> 数据库> 正文

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决办法

简介: 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
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 

测试显示问题解决了。

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
数据库
使用钉钉扫一扫加入圈子
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

其他文章