alibaba / druid

阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
https://github.com/alibaba/druid/wiki
Apache License 2.0
27.9k stars 8.57k forks source link

springboot框架Druid连接池,使用tidb数据库,偶尔出现db连接断开异常 #3941

Open simusuishi opened 4 years ago

simusuishi commented 4 years ago

【TiDB 版本】:v4.0.4 【问题描述】:javaWeb程序在切换tidb数据库后,偶尔会抛出db连接断开异常,频率是每天4次左右。

框架:springboot2.2.2.RELEASE 连接池:druid1.1.20

连接池配置:

datasource druid pool

spring.datasource.dynamic.druid.filters= stat spring.datasource.dynamic.druid.initial-size=5 spring.datasource.dynamic.druid.max-active=300 spring.datasource.dynamic.druid.min-idle=16 spring.datasource.dynamic.druid.max-wait=60000 spring.datasource.dynamic.druid.time-between-eviction-runs-millis=360000 spring.datasource.dynamic.druid.min-evictable-idle-time-millis=360000 spring.datasource.dynamic.druid.validation-query=SELECT 1 FROM DUAL spring.datasource.dynamic.druid.test-while-idle=true

tidb查询数据源 spring.datasource.dynamic.datasource.datastidb.username=USERNAME spring.datasource.dynamic.datasource.datastidb.password=PASSWORD spring.datasource.dynamic.datasource.datastidb.url=jdbc:mysql://TIDB_DB?useUnicode=true&useSSL=false&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false spring.datasource.dynamic.datasource.datastidb.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.dynamic.datasource.datastidb.type=com.alibaba.druid.pool.DruidDataSource

注:为了不暴露真实的数据源,USERNAME,PASSWORD和TIDB_DB是使用临时的字符串

异常信息: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet successfully received from the server was 29,742 milliseconds ago. The last packet sent successfully to the server was 29,742 milliseconds ago. at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:370) at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3461) 略。。。

Alexander-Jim commented 3 years ago

检查数据库wait_timeout参数是否过短(是否小于连接池的minEvictableIdleTimeMillis+timeBetweenEvictionRunsMillis)

kud1 commented 2 years ago

最终怎么解决的?