美文网首页DruidDruid
Druid配置参数详解-timeBetweenEvictionR

Druid配置参数详解-timeBetweenEvictionR

作者: codeimport | 来源:发表于2020-01-02 22:46 被阅读0次

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;
                }
            }
        }

    }

总结

  • timeBetweenEvictionRunsMillis可以用来控制空闲连接数的回收周期
  • timeBetweenEvictionRunsMillis可以用来控制回收泄露连接的周期
  • timeBetweenEvictionRunsMillis连接的空闲时间大于该值testWhileIdle才起作用

相关文章

网友评论

    本文标题:Druid配置参数详解-timeBetweenEvictionR

    本文链接:https://www.haomeiwen.com/subject/gcdaoctx.html