背景:公司所用的开发结构SSM(spring+spinrgMVC+Mybatis),spring 内置了很多的定时任务,于是新开一个项目把后续用到的定时任务单独配置起来
异常描述:经常会出现数据库连接超时。定时任务没跑 进本上都是这种问题。上网搜了一把,好像是mysql连接池中的连接已经expired的问题。试了几种方式,最终通过在MYSQL的JDBC连接串上加了autoReconnectForPools=true解决了
异常代码:
org.springframework.dao.RecoverableDataAccessException:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 5,903,769 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
### The error may exist in class path resource [mybatis/ErpWarehouseMapper.xml]
### The error may involve com.tqmall.erp.quartz.dao.ErpWarehouseDao.selectByParam-Inline
### The error occurred while setting parameters
### SQL:
### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 5,903,769 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
; SQL []; Communications link failure
The last packet successfully received from the server was 5,903,769 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 5,903,769 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:98) ~[erp-quartz.jar:na]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) ~[erp-quartz.jar:na]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[erp-quartz.jar:na]
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73) ~[erp-quartz.jar:na]
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371) ~[erp-quartz.jar:na]
at com.sun.proxy.$Proxy28.selectList(Unknown Source) ~[na:na]
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:198) ~[erp-quartz.jar:na]
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:122) ~[erp-quartz.jar:na]
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:64) ~[erp-quartz.jar:na]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53) ~[erp-quartz.jar:na]
at com.sun.proxy.$Proxy29.selectByParam(Unknown Source) ~[na:na]
处理方案:数据库连接信息配置后面加 autoReconnectForPools=true(不知道有效果没)