数据库连接池(五)常见问题总结

一、数据库连接超时:

1、报错:

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: The last packet successfully received from the server was 599,998 milliseconds ago.  The last packet sent successfully to the server was 599,999 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 this problem.
	at sun.reflect.GeneratedConstructorAccessor69.newInstance(Unknown Source)

2、原因:Mysql服务器有个wait_timeout属性,通过show global variables like 'wait_timeout'可以查看,单位为秒,一个connection空闲超过这个时间,Mysql将自动断开该connection。这种情况一般发生在定时组件中,比如每隔一天运行一次,第一次正常,以后每隔一段时间就会报错,当然不会影响功能(因为超时的连接报错,任务运行时还是会创建新的连接的)。

3、解决方法:

可以增加mysql的wait_timeout,一般不得已才这样做;可以在url后面加上autoReconnect=true,但是对于mysql高版本会失效;最好的方法是利用连接池自带的属性来解决:

(1.)druid:

百度的各种方法:

 #超时回收机制
      removeAbandoned: true
      removeAbandonedTimeout: 1800
      logAbandoned: false

测试无效。

配置连接池的testOnBorrow=true,这样在每次从连接池中取出且准备使用之前先测试下当前使用是否能用,如果不能用,系统就会自动删除掉。 但是这样会大大的降低程序的性能

测试无效。

配置maxWait=-1

 

二、

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值