DruidDruid

Druid配置参数详解-timeBetweenEvictionR

2020-01-02  本文已影响0人  codeimport

Druid配置参数详解-timeBetweenEvictionRunsMillis

Druid是一个由阿里开源的数据库连接池,Druid的配置非常丰富,但是设置不当会对生产环境造成严重影响,网上Druid的资料虽多,但大部分都是互相复制粘贴,有很多不准确甚至完全错误的描述,Druid已经开源很久,而且作者WenShao的工作重心也已经不在Druid上,有些功能估计他自己都不太了解了。本系列将从源代码的角度分析Druid目前的最新版本(1.1.21)各个常用的配置项的具体含义以及是怎么起作用的。

画外音:目前Druid在开源中国举办的2019年度最受欢迎中国开源软件中排名第7名,支持Druid的朋友可以去投票哇。2019年度最受欢迎中国开源软件

timeBetweenEvictionRunsMillis是什么意思?

timeBetweenEvictionRunsMillis默认值是60s,主要作用在两处地方

作为DestroyTask执行的时间周期,DestroyTask主要有两个作用:
作为验证连接是否有效的时间周期,如果testOnBorrow==false并且testWhileIdle==true,则在应用获取连接的时候会判断连接的空闲时间是否大于timeBetweenEvictionRunsMillis,如果大于则会验证该连接是否有效。

具体可以参见:Druid配置参数详解-testWhileIdle

timeBetweenEvictionRunsMillis是怎么起作用的?

timeBetweenEvictionRunsMillis主要是在DestroyConnectionThread中使用:

    public class DestroyConnectionThread extends Thread {

        public DestroyConnectionThread(String name){
            super(name);
            this.setDaemon(true);
        }

        public void run() {
            initedLatch.countDown();

            for (;;) {
                // 从前面开始删除
                try {
                    if (closed) {
                        break;
                    }

                    if (timeBetweenEvictionRunsMillis > 0) {
                        Thread.sleep(timeBetweenEvictionRunsMillis);
                    } else {
                        Thread.sleep(1000); //
                    }

                    if (Thread.interrupted()) {
                        break;
                    }

                    destroyTask.run();
                } catch (InterruptedException e) {
                    break;
                }
            }
        }

    }

总结

上一篇 下一篇

猜你喜欢

热点阅读