HikariCP和maxLifetime

HikariCP和maxLifetime

本文介绍了HikariCP和maxLifetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将项目移至。到目前为止一切都很好,但是使用一种设置我遇到了麻烦。

I moved my project to HikariCP. Everything is going fine so far, but with one setting I'm having trouble.

这是 .setMaxLifetime(30 * 1000)设置。我收到此警告

It's the .setMaxLifetime(30*1000) setting in HikariConfig object. I get this warning

WARN com.zaxxer.hikari.HikariConfig - maxLifetime is less than 120000ms, using default 1800000ms.

我知道他们建议不要将设置设置为我尝试的那么低。但是不幸的是,由于无法更改的情况,每个打开时间超过50秒的TCP连接都将在生产环境中终止。

I know that they recommend not setting is that low as I am trying to. But Unfortunately due to circumstances that I can not change, every TCP connection that is open longer than 50 secods will be terminated in our production environment.

推荐答案

我不知道您的 HikariCP 版本,但是在版本2.2.4中,您会找到它发出上述警告的原因。
HikariConfig.class (在 com.zaxxer.hikari.HikariConfig 中):

i don't know your HikariCP Version, but in the version 2.2.4 you will find the reason why it will throw the above warning.HikariConfig.class (in the com.zaxxer.hikari.HikariConfig):

 private void More ...validateNumerics()
  {
     Logger logger = LoggerFactory.getLogger(getClass());

     if (connectionTimeout == Integer.MAX_VALUE) {
        logger.warn("No connection wait timeout is set, this might cause an infinite wait.");
     }

     if (minIdle < 0 || minIdle > maxPoolSize) {
        minIdle = maxPoolSize;
     }

     if (maxLifetime < 0) {
        logger.error("maxLifetime cannot be negative.");
        throw new IllegalArgumentException("maxLifetime cannot be negative.");
     }
     else if (maxLifetime > 0 && maxLifetime < TimeUnit.SECONDS.toMillis(120)) {
        logger.warn("maxLifetime is less than 120000ms, using default {}ms.", MAX_LIFETIME);
        maxLifetime = MAX_LIFETIME;
     }

     if (idleTimeout != 0 && idleTimeout < TimeUnit.SECONDS.toMillis(30)) {
        logger.warn("idleTimeout is less than 30000ms, using default {}ms.", IDLE_TIMEOUT);
        idleTimeout = IDLE_TIMEOUT;
     }
     else if (idleTimeout > maxLifetime && maxLifetime > 0) {
        logger.warn("idleTimeout is greater than maxLifetime, setting to maxLifetime.");
        idleTimeout = maxLifetime;
     }

此代码中的maxLifeTime至少为120000ms,使用默认的1800000ms。因此您不能将 maxLifeTime 设置为30000ms(30 * 1000)。我猜您的 HikariCP 版本至少早于2.2.4。

from this code, the maxLifeTime is at least 120000ms, using default 1800000ms. so you can't set the maxLifeTime to 30000ms(30*1000). I guess your HikariCP version is at least older than 2.2.4.

但是当您找到。它说: 我们强烈建议设置此值,并且该值至少应比任何数据库或基础结构施加的连接时间限制少30秒。

But when you find the latest HikariCP version 2.7.4. it said "We strongly recommend setting this value, and it should be at least 30 seconds less than any database or infrastructure imposed connection time limit."

同一个类 HikariConfig.class

private void validateNumerics() {
    if(this.maxLifetime != 0L && this.maxLifetime < TimeUnit.SECONDS.toMillis(30L)) {
        LOGGER.warn("{} - maxLifetime is less than 30000ms, setting to default {}ms.", this.poolName, Long.valueOf(MAX_LIFETIME));
        this.maxLifetime = MAX_LIFETIME;
    }

    if(this.idleTimeout + TimeUnit.SECONDS.toMillis(1L) > this.maxLifetime && this.maxLifetime > 0L) {
        LOGGER.warn("{} - idleTimeout is close to or more than maxLifetime, disabling it.", this.poolName);
        this.idleTimeout = 0L;
    }

    if(this.idleTimeout != 0L && this.idleTimeout < TimeUnit.SECONDS.toMillis(10L)) {
        LOGGER.warn("{} - idleTimeout is less than 10000ms, setting to default {}ms.", this.poolName, Long.valueOf(IDLE_TIMEOUT));
        this.idleTimeout = IDLE_TIMEOUT;
    }

    if(this.leakDetectionThreshold > 0L && !unitTest && (this.leakDetectionThreshold < TimeUnit.SECONDS.toMillis(2L) || this.leakDetectionThreshold > this.maxLifetime && this.maxLifetime > 0L)) {
        LOGGER.warn("{} - leakDetectionThreshold is less than 2000ms or more than maxLifetime, disabling it.", this.poolName);
        this.leakDetectionThreshold = 0L;
    }

    if(this.connectionTimeout < 250L) {
        LOGGER.warn("{} - connectionTimeout is less than 250ms, setting to {}ms.", this.poolName, Long.valueOf(CONNECTION_TIMEOUT));
        this.connectionTimeout = CONNECTION_TIMEOUT;
    }

    if(this.validationTimeout < 250L) {
        LOGGER.warn("{} - validationTimeout is less than 250ms, setting to {}ms.", this.poolName, Long.valueOf(VALIDATION_TIMEOUT));
        this.validationTimeout = VALIDATION_TIMEOUT;
    }

    if(this.maxPoolSize < 1) {
        this.maxPoolSize = this.minIdle <= 0?10:this.minIdle;
    }

    if(this.minIdle < 0 || this.minIdle > this.maxPoolSize) {
        this.minIdle = this.maxPoolSize;
    }

}

此代码中的 maxLifeTime 在此版本中至少已更新为30000ms。

from this code, the maxLifeTime has been updated to 30000ms at least in this version.

所以现在请更新您的 HikariCP 版本如果要将maxLifeTime设置为30000ms,则为最新版本2.7.4。

So now please update your HikariCP version to the latest version 2.7.4 if you want to set maxLifeTime to 30000ms.

但是如果将HikariCP版本更新为2.7.4对于JDK 8,我还建议您注意两点:

But if you update your HikariCP version to 2.7.4 with JDK 8, i also recommend you two points:

1。将 maxLifeTime 的值设置为至少30000ms。

1. to set maxLifeTime value to be at least 30000ms.

2。将 maxLifeTime 的值设置为比mysql的 wait_timeout 少几分钟(显示类似%timeout% )以避免断开连接异常。

2. to set maxLifeTime value few minute less than mysql's wait_timeout(show variables like "%timeout%") to avoid broken connection exception.

这篇关于HikariCP和maxLifetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-21 03:06