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 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 

测试显示问题解决了。

时间: 2024-10-25 14:40:00

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决办法的相关文章

数据库-异常:com.mysql.jdbc.exceptions.jdbc4.communicatio

问题描述 异常:com.mysql.jdbc.exceptions.jdbc4.communicatio 异常:com.mysql.jdbc.exceptions.jdbc4.communicationException:communication link failure last packet sent to the server was 0 ms ago at sun.refect.nativecon.........等等 我用的是mysql数据库,Java文件可以编译,但是一运行就出现错

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException异常求助

问题描述 具体错误是YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'5*fromforumwhereid>0orderbyiddesc'atline1异常代码如下图:分页代码如下:packagedao;importjava.sql.Connection;importjava.sql.PreparedStatement;impo

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'db_item'

    一直很奇怪,为什么报错,进入mysql命令行,show databases:发现多谢了一个空格,如上图.

ActiveMQ:Communications link failure问题以及解决办法

ActiveMQ版本:5.5.1 MQ 所使用的 MySQL 是 InnoDB存储引擎 记录人:@郑昀 现象: 业务表面现象:无.系统现象:无. 日志信息:业务系统发送 MQ 消息时,下面这种错误日志断断续续地一直都有: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure   The last packet successfully received from the serve

JDBC中的Communications link failure due to underlying exception错误

问题描述:使用JDBC连接Mysql数据库时,出现"Connection reset"错误,抛出如下异常: Connection couldn't be established to jdbc:mysql://***/***?user=***&password=******** com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: ** B

com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:

问题描述 WEB系统报以下的错,是为什么? Caused by: java.sql.BatchUpdateException: Duplicate entry '3592030993-2011-11-23 19:22:45' for key 'PRIMARY'at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2024)at com.mysql.jdbc.PreparedStatement

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException错误

问题描述 请教大虾:以下错误如何解决:严重:Servlet.service()forservletactionthrewexceptioncom.mysql.jdbc.exceptions.MySQLSyntaxErrorException:Table'netctoss.pricing'doesn'texistatcom.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)atcom.mysql.jdbc.MysqlIO.checkE

配置- hive Communications link failure

问题描述 hive Communications link failure 在hive链接mysql作为数据源的过程中,配置conf下的hive-site.xml 文件,hive是装在Hadoop集群的master上,ip地址是192.168.1.154.mysql直接使用的sudo apt-get install mysql-server 安装的.使用netstat -nat 显示: 结果如下:然后我的hive 配置文件是:把ip地址换成localhost或者127.0.0.1 hive下 s

将MySQL从MyISAM转换成InnoDB错误和解决办法_Mysql

后来在做WordPress,一开始还不知道原来WordPress用的是InnoDB数据引擎,于是在原来的数据库里面就建了一个数据库,一开始也没发觉问题,安装,导入sql,都没问题,当时也没多想.直到这几天因为又要装多一个WordPress,用phpmyadmin访问数据库多一点,问题来了.老是一访问WordPress的数据库,就弹出错误提示"Mysqld-nt.exe应用程序错误:"0x005346c4"指令引用的"0x00786000"内存,该内存不能为