返回顶部

收藏

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

更多
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
        at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:296) ~[spring-jdbc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:320) ~[spring-jdbc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at org.springframework.jdbc.support.SQLErrorCodesFactory.getErrorCodes(SQLErrorCodesFactory.java:214) ~[spring-jdbc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.setDataSource(SQLErrorCodeSQLExceptionTranslator.java:141) [spring-jdbc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.<init>(SQLErrorCodeSQLExceptionTranslator.java:104) [spring-jdbc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at org.springframework.jdbc.support.JdbcAccessor.getExceptionTranslator(JdbcAccessor.java:99) [spring-jdbc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:203) [spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList(SqlMapClientTemplate.java:293) [spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at com.panguso.radar.dao.PublicSentimentDao.getPublicSentimentListByAlarmSettings(PublicSentimentDao.java:99) [radar-core-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at com.panguso.radar.service.PublicSentimentService.getPublicSentmentsByAlarmSetting(PublicSentimentService.java:158) [radar-core-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at com.panguso.radar.async.AlarmThread.doTask(AlarmThread.java:105) [radar-alarm-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at com.panguso.radar.async.SafeStopThread.run(SafeStopThread.java:48) [radar-core-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
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) ~[na:1.6.0_20]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) ~[na:1.6.0_20]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) ~[na:1.6.0_20]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513) ~[na:1.6.0_20]
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.20.jar:na]
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116) ~[mysql-connector-java-5.1.20.jar:na]
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:348) ~[mysql-connector-java-5.1.20.jar:na]
        at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2391) ~[mysql-connector-java-5.1.20.jar:na]
        at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2428) ~[mysql-connector-java-5.1.20.jar:na]
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2213) ~[mysql-connector-java-5.1.20.jar:na]
        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:797) ~[mysql-connector-java-5.1.20.jar:na]
        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) ~[mysql-connector-java-5.1.20.jar:na]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.6.0_20]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) ~[na:1.6.0_20]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) ~[na:1.6.0_20]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513) ~[na:1.6.0_20]
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.20.jar:na]
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389) ~[mysql-connector-java-5.1.20.jar:na]
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305) ~[mysql-connector-java-5.1.20.jar:na]
        at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38) ~[commons-dbcp-1.4.jar:1.4]
        at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582) ~[commons-dbcp-1.4.jar:1.4]
        at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1148) ~[commons-pool-1.5.4.jar:1.5.4]
        at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106) ~[commons-dbcp-1.4.jar:1.4]
        at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044) ~[commons-dbcp-1.4.jar:1.4]
        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) ~[spring-jdbc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
        at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy$TransactionAwareInvocationHandler.invoke(TransactionAwareDataSourceProxy.java:224) ~[spring-jdbc-3.1.2.RELEASE.jar:3.1.2.RELEASE]

测试程序发现这样的异常,从网上查到原因如下:

使用Connector/J连接MySQL数据库,程序运行较长时间后就会报以下错误:

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属性避免该错误。(我的错误没有这样的提示) 后来查了一些资料,才发现遇到这个问题的人还真不少,大部分都是使用连接池方式时才会出现这个问题,短连接应该很难出现这个问题。这个问题的原因:

MySQL服务器默认的“wait_timeout”是28800秒即8小时,意味着如果一个连接的空闲时间超过8个小时,MySQL将自动断开该连接,而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。

1.按照错误的提示,可以在JDBC URL中使用autoReconnect属性,实际测试时使用了

autoReconnect=true&failOverReadOnly=false,不过并未起作用,使用的是5.1版本,可能真像网上所说的只对4之前的版本有效。

2.没办法,只能修改MySQL的参数了,wait_timeout最大为31536000即1年,在my.cnf中加入:

[mysqld]
wait_timeout=31536000
interactive_timeout=31536000

重启生效,需要同时修改这两个参数。

还有评论说:从某个地方看到.timeout最多是2147483(24天)吧,就算设置了大于这个数,也是按照这个来算的。

虽然报了这个异常,我的程序还是照常工作,这个也许是mysql的bug吧!哎!郁闷!怕以后不知道会不会影响我的应用,这几天发现了2个mysql的bug了。

标签:java,jdbc,异常处理

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. kun 发表 2011-09-22 18:13:10 异常处理?
  2. root 发表 2015-04-13 03:46:02 JDBC操作数据库
  3. 冷乐 发表 2015-06-22 09:59:04 集成Log4Jdbc
  4. Hesey 发表 2013-11-18 11:34:53 消除Java应用中的Exception开销
  5. dbafree 发表 2013-04-06 13:20:44 jdbc thin模式-bug 9373370分享
  6. admin 发表 2009-03-24 08:39:21 java如何获取ResultSet记录行数
  7. 唐尤华 发表 2013-07-23 16:00:19 改进异常处理的6条建议
  8. 博主 发表 2016-06-23 08:08:09 Tomcat-Jdbc-Pool参数说明
  9. 博主 发表 2014-11-23 00:00:00 生成Java JDBC访问代码,从SQL文件
  10. importnewzz 发表 2015-01-21 03:08:37 深入理解java异常处理机制
  11. luanhailiang 发表 2016-03-09 11:33:52 java jdbc 将mysql表转换成protobuf
  12. 懒散狂徒 发表 2013-03-11 00:49:24 ApacheHive一点一点进步(2)–HIVEJDBC

发表评论