一、数据库连接超时:
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
二、