异常如下
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″ />