2019-06-04 数据库连接池连接失败问题

使用数据库连接池时,偶尔会有如下错误出现,原因为Mysql服务器有个“wait_timeout”的配置,默认值是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该 connection。按照错误提示,连接URL带上“autoReconnect=true”,但是问题并没有解决。
实际上,mysql5.4之后不再支持此选项。

### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 90,394,971 milliseconds ago.  The last packet sent successfully to the server was 90,394,972 milliseconds ago. is l
onger than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connec
tor/J connection property 'autoReconnect=true' to avoid this problem.
; SQL []; The last packet successfully received from the server was 90,394,971 milliseconds ago.  The last packet sent successfully to the server was 90,394,972 milliseconds ago. is longer than the server configured value of 'wait_timeout'. 
You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid th
is problem.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 90,394,971 milliseconds ago.  The last packet sent successfully to the server was 90,394,972 
milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeout
s, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
        at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:98)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:82)
        at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
        at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
        at com.sun.proxy.$Proxy80.selectList(Unknown Source)
        at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
        at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
        at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
        at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
        at com.sun.proxy.$Proxy104.queryFeedsByFromFeedsIds(Unknown Source)
        at com.tencent.hlife.keeper.service.impl.HshServiceImpl.fillPublishData(HshServiceImpl.java:291)
        at com.tencent.hlife.keeper.service.impl.HshServiceImpl.getFeeds(HshServiceImpl.java:160)
        at com.tencent.iask.huilife.controller.audit.AuditKeeperFeedsController.searchFeeds(AuditKeeperFeedsController.java:51)
        at com.tencent.iask.huilife.controller.audit.AuditKeeperFeedsController$$FastClassBySpringCGLIB$$1dc7c0ab.invoke()
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:736)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
        at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:84)
        at com.tencent.iask.huilife.intercepter.LogAop.around(LogAop.java:34)

可用配置,使用test-while-idle 和 validation-query,在连接空闲时进行检查,springboot下配置示例如下:

spring:
  datasource:
    testdb:
      url: jdbc:mysql://127.0.0.1:3306/test?useAffectedRows=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false
      username: test
      password: 123456
      driver-class-name: com.mysql.jdbc.Driver
      test-while-idle: true
      validation-query: SELECT 1

你可能感兴趣的:(2019-06-04 数据库连接池连接失败问题)