开发者社区> 问答> 正文

Druid 连接mysql 报skip not validate connection

用的druid关联连接池,数据库是mysql,一段时间不用之后就会报这个错误。之前有人说是mysql系统的问题。我从window换成linux也是这样。下面是我的配置文件
···
url:jdbc:mysql://xxx.xx.xxx.xxx:3306/XXX?Unicode=true&characterEncoding=utf8&autoReconnect=true
driverClassName:com.mysql.jdbc.Driver
username:root
password:123456

filters:stat

maxActive:100
initialSize:10
maxWait:60000
minIdle:20
maxIdle:100

timeBetweenEvictionRunsMillis:60000
minEvictableIdleTimeMillis:300000

validationQuery:SELECT 'x'
testWhileIdle:true
testOnBorrow:false
testOnReturn:false

maxOpenPreparedStatements:20
removeAbandoned:true
removeAbandonedTimeout:180
logAbandoned:true

spring里面的配置

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
    destroy-method="close">
    <!-- 数据库基本信息配置 -->
    <property name="url" value="${url}" />
    <property name="username" value="${username}" />
    <property name="password" value="${password}" />
    <property name="driverClassName" value="${driverClassName}" />
    <property name="filters" value="${filters}" />
    <!-- 最大并发连接数 -->
    <property name="maxActive" value="${maxActive}" />
    <!-- 初始化连接数量 -->
    <property name="initialSize" value="${initialSize}" />
    <!-- 配置获取连接等待超时的时间 -->
    <property name="maxWait" value="${maxWait}" />
    <!-- 最小空闲连接数 -->
    <property name="minIdle" value="${minIdle}" />
    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
    <property name="validationQuery" value="${validationQuery}" />
    <property name="testWhileIdle" value="${testWhileIdle}" />
    <property name="testOnBorrow" value="${testOnBorrow}" />
    <property name="testOnReturn" value="${testOnReturn}" />
    <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
    <!-- 打开removeAbandoned功能 -->
    <property name="removeAbandoned" value="${removeAbandoned}" />
    <!-- 1800秒,也就是30分钟 -->
    <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
    <!-- 关闭abanded连接时输出错误日志 -->
    <property name="logAbandoned" value="${logAbandoned}" />
</bean>

展开
收起
a123456678 2016-06-15 15:56:51 10026 0
1 条回答
写回答
取消 提交回答
  • public DruidPooledConnection getConnectionDirect(long maxWaitMillis) throws SQLException {
            int notFullTimeoutRetryCnt = 0;
            for (;;) {
                // handle notFullTimeoutRetry
                DruidPooledConnection poolableConnection;
                try {
                    poolableConnection = getConnectionInternal(maxWaitMillis);
                } catch (GetConnectionTimeoutException ex) {
                    if (notFullTimeoutRetryCnt <= this.notFullTimeoutRetryCount && !isFull()) {
                        notFullTimeoutRetryCnt++;
                        if (LOG.isWarnEnabled()) {
                            LOG.warn("not full timeout retry : " + notFullTimeoutRetryCnt);
                        }
                        continue;
                    }
                    throw ex;
                }
    
    
                if (isTestOnBorrow()) {
                    boolean validate = testConnectionInternal(poolableConnection.getConnection());
                    if (!validate) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("skip not validate connection.");
                        }
    
    
                        Connection realConnection = poolableConnection.getConnection();
                        discardConnection(realConnection);
                        continue;
                    }
                } else {
                    Connection realConnection = poolableConnection.getConnection();
                    if (realConnection.isClosed()) {
                        discardConnection(null); // 传入null,避免重复关闭
                        continue;
                    }
    
    
                    if (isTestWhileIdle()) {
                        final long currentTimeMillis = System.currentTimeMillis();
                        final long lastActiveTimeMillis = poolableConnection.getConnectionHolder().getLastActiveTimeMillis();
                        final long idleMillis = currentTimeMillis - lastActiveTimeMillis;
                        long timeBetweenEvictionRunsMillis = this.getTimeBetweenEvictionRunsMillis();
                        if (timeBetweenEvictionRunsMillis <= 0) {
                            timeBetweenEvictionRunsMillis = DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
                        }
    
    
                        if (idleMillis >= timeBetweenEvictionRunsMillis) {
                            boolean validate = testConnectionInternal(poolableConnection.getConnection());
                            if (!validate) {
                                if (LOG.isDebugEnabled()) {
                                    LOG.debug("skip not validate connection.");
                                }
    
    
                                discardConnection(realConnection);
                                continue;
                            }
                        }
                    }
                }
    2019-07-17 19:39:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像