zoukankan      html  css  js  c++  java
  • Druid连接池 报错:abandon connection原因分析

    问题现象:
    使用Druid的数据库连接池,在进行一个查询SQL的时候,抛出了异常:

    [2017-10-20 01:40:59.269 ERROR com.alibaba.druid.pool.DruidDataSource:2189] abandon connection, owner thread: schedulerDuty_Worker-2, connected at : 1508434843057, open stackTrace
    ————————————————

    从报错信息中可以提取到,是Druid连接池执行SQL,获取Statement失败,导致的问题

    问题分析:
    根据异常的第一行,可以得知报错来自于DruidDataSource这个类,根据报错的信息,可以看出是因为执行SQL时获取不到Connection连接,然后去看一下Druid的配置中,有三个配置可以关注一下:

    配置 默认值 说明
    removeAbandoned false 是否强制关闭连接时长大于removeAbandonedTimeoutMillis的连接
    removeAbandonedTimeoutMillis 300 * 1000 一个连接从被连接到被关闭之间的最大生命周期
    logAbandoned false 强制关闭连接时是否记录日志


    再去查看我们的配置,removeAbandoned是true,代表的意思是 是否关闭连接时长大于一定时长的连接,问题可能是出在这里,去看源代码:

    public class DestroyTask implements Runnable {
    public DestroyTask() {
    }

    public void run() {
    DruidDataSource.this.shrink(true);
    if(DruidDataSource.this.isRemoveAbandoned()) {
    DruidDataSource.this.removeAbandoned();
    }
    }
    }

    发现调用removeAbandoned的位置在这里,这里是一个线程,在连接池初始化的时候会启动一个调度,在定时的时间频度去执行,可以看出这里的判断条件是如果开启了配置,就去执行removeAbandoned()这个方法,来看一下这个方法里面有什么:

    public int removeAbandoned() {
    int removeCount = 0;
    long currrentNanos = System.nanoTime();
    ArrayList abandonedList = new ArrayList();
    Map var5 = this.activeConnections;
    synchronized(this.activeConnections) {
    Iterator pooledConnection = this.activeConnections.keySet().iterator();

    while(pooledConnection.hasNext()) {
    DruidPooledConnection buf = (DruidPooledConnection)pooledConnection.next();
    if(!buf.isRunning()) {
    long trace = (currrentNanos - buf.getConnectedTimeNano()) / 1000000L;
    if(trace >= this.removeAbandonedTimeoutMillis) {
    pooledConnection.remove();
    buf.setTraceEnable(false);
    abandonedList.add(buf);
    }
    }
    }
    }

    这里给出了一部分的代码实现,可以看到问题的原因可能就是出现在这里了,这里进行了一个判断,循环遍历连接池中的连接,如果存活,就判断是否超过了配置的removeAbandonedTimeoutMillis(单位是毫秒,配置里面需要配置秒,有转换),如果超过了时间,我就干死你!

    解决方案
    问题原因已经找到,那么解决的办法就是将removeAbandoned这个配置设置为false或者不设置(默认就是false),或者将removeAbandonedTimeoutMillis这个时间配置调大:

    <!--配置成三十分钟-->
    <property name="removeAbandonedTimeout" value="1800" />
    1
    2
    再观察,就不会出现报错的情况了,问题解决~
    ————————————————
    版权声明:本文为CSDN博主「wtopps」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/wtopps/article/details/78343659

  • 相关阅读:
    excel打印预览后整个表格中按页显示虚线,打印时就会打印很多页
    4.8 自定义下拉菜单模式Spinner与setDropDownViewResource
    1.2Matlab基本语法和基本操作.
    CTime类的一个BUG
    【转载】PE_Info 之DIY
    ubuntu使用记录
    反VM测试成功
    获取MAC地址
    【转载】秒到oep,支持所有壳,包括vmp
    【转载】Make your owner PE Protector
  • 原文地址:https://www.cnblogs.com/renjiaqi/p/11693774.html
Copyright © 2011-2022 走看看