druid升级1.0.26遇到的连接闲置断开的问题

1. 背景

之前处理了一个mybatis的bug(详见之前的文章),在给系统升级mybatis的时候,想顺便把所有系统的druid也升级了,从版本(0.2.18~1.0.18不等)升级到统一的最新版本(1.0.26)。

但是在beta测试期间,遇到如下异常:

ERROR com.alibaba.druid.pool.DruidDataSource:1243 discard connection
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 1,463,838 milliseconds ago.  The last packet sent successfully to the server was 1,463,839 mill
iseconds 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 configure
d values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_45]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_45]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_45]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_45]
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:408) ~[mysql-connector-java-5.1.32.jar:na]
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1137) ~[mysql-connector-java-5.1.32.jar:na]

复现的方法是程序启动后,访问一些需要查库的功能,然后闲置直至超过mysql配置的wait_timeout时间(默认8小时,可以改小,如3分钟,方便复现),再访问需要查库的资源,就出现这个问题了。


2.相关配置

代码封装了一个工厂,对druid进行了统一的配置。

其中和连接相关的配置只有:

url中的autoReconnect=true(但这个看网上说在mysql 5.x以上不管用了,没有具体考证,不敢乱说)

testWhileIdle=true这两条。


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值