Last packet sent to the server was 0 ms ago.

本文介绍了当Java程序遇到MySQL连接超时导致的异常时,提供了两种解决方案:一是通过配置MySQL的wait_timeout属性来延长连接超时时间;二是通过配置连接池的testOnBorrow和validationQuery属性来检测连接的有效性。
摘要由CSDN通过智能技术生成

异常如下

org.springframework.dao.RecoverableDataAccessException: PreparedStatementCallback; SQL []; Communications link failure

Last packet sent to the server was 0 ms ago.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
	at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:98)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:603)
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:662)
	at net.paoding.rose.jade.dataaccess.DataAccessImpl.select(DataAccessImpl.java:55)
	at net.paoding.rose.jade.statement.SelectQuerier.execute(SelectQuerier.java:63)
	at net.paoding.rose.jade.statement.SelectQuerier.execute(SelectQuerier.java:56)
	at net.paoding.rose.jade.statement.JdbcStatement.execute(JdbcStatement.java:146)
	at net.paoding.rose.jade.statement.cached.CachedStatement.execute(CachedStatement.java:98)
	at net.paoding.rose.jade.context.JadeInvocationHandler.invoke(JadeInvocationHandler.java:128)
	at com.sun.proxy.$Proxy8.getSubjectNoQualify(Unknown Source)
	at com.chaoxing.subject.service.SubjectService.getSubjectNoQualify(SubjectService.java:189)
	at com.chaoxing.subject.jobs.SubjectJobFromKafka.run(SubjectJobFromKafka.java:60)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
	at sun.reflect.GeneratedConstructorAccessor71.newInstance(Unknown Source)
	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.reuseAndReadPacket(MysqlIO.java:2985)
	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2871)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3414)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
	at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1885)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeQuery(FilterChainImpl.java:2714)
	at com.alibaba.druid.wall.WallFilter.preparedStatement_executeQuery(WallFilter.java:603)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeQuery(FilterChainImpl.java:2711)
	at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_executeQuery(FilterEventAdapter.java:465)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeQuery(FilterChainImpl.java:2711)
	at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_executeQuery(FilterEventAdapter.java:465)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeQuery(FilterChainImpl.java:2711)
	at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.executeQuery(PreparedStatementProxyImpl.java:132)
	at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeQuery(DruidPooledPreparedStatement.java:227)
	at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:644)
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:587)
	... 11 more
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
	at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2431)
	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2882)
	... 29 more

原因:

由于MySQL服务在长时间(默认8小时)不连接超时了,所以我的Java程序数据库连接池中的所有连接都是不可用的,但Java程序分辨不了(需要配置属性在解决方法中说明)认为连接可用,这时如果有用户请求访问就会如上异常。这就是异常的原因

两种解决办法:

1 、配置MySQL属性wait_timeout=(配一个大的时间)

2、 配置testOnBorrow和validationQuery每次检测连接是否可用

<property name=”minEvictableIdleTimeMillis” value=”1800000″ />
<property name=”numTestsPerEvictionRun” value=”3″ />
<property name=”testOnBorrow” value=”true” />
<property name=”testWhileIdle” value=”true” />
<property name=”testOnReturn” value=”true” />
<property name=”validationQuery” value=”SELECT 1″ />

102349_0OC9_2902377.jpg

转载于:https://my.oschina.net/kdy1994/blog/831900

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值