HikariCP 和 maxLifetime

2022-09-03 02:29:47

我把我的项目搬到了HikariCP。到目前为止,一切都很顺利,但是有一个设置,我遇到了麻烦。

这是HikariConfig对象中的设置。我收到此警告.setMaxLifetime(30*1000)

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

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


答案 1

我不知道你的版本,但在版本2.2.4中,你会发现它会抛出上述警告的原因。 (在):HikariCPHikariConfig.classcom.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;
     }

从此代码中,最大LifeTime至少为120000ms,使用默认值1800000ms,因此您无法将其设置为30000ms(30 * 1000)。我猜你的版本至少早于2.2.4。maxLifeTimeHikariCP

但是当你找到最新的HikariCP版本2.7.4时。它说:“我们强烈建议设置此值,并且它应该比任何数据库或基础架构施加的连接时间限制至少少30秒。"

同一类 :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;
    }

}

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

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

但是,如果您使用JDK 8将HikariCP版本更新到2.7.4,我也建议您两点:

1. 将最大寿命值设置为至少 30000ms。

2.将maxLifeTime值设置为比mysql的wait_timeout少几分钟(显示“%timeout%”等变量)以避免断开连接异常。


答案 2

使用Hikari版本2.7.9,我进行了以下设置:

HikariConfig cpConfig = new HikariConfig();
cpConfig.setJdbcUrl(jdbcUrl);
cpConfig.setUsername(username);
cpConfig.setPassword(password);
cpConfig.setMaximumPoolSize(15);
cpConfig.setConnectionTestQuery("SELECT 1");

// performance senstive settings
cpConfig.setMinimumIdle(0);
cpConfig.setConnectionTimeout(30000);
cpConfig.setIdleTimeout(35000);
cpConfig.setMaxLifetime(45000);

cpConfig.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

HikariDataSource cpDatasource = new HikariDataSource(cpConfig);
localContainerEntityManagerFactoryBean.setDataSource(cpDatasource);
localContainerEntityManagerFactoryBean.setJpaProperties(jpaProperties);
localContainerEntityManagerFactoryBean.afterPropertiesSet();

它的工作原理。但请小心:

 cpConfig.setMinimumIdle(0);

如果你的数据库,例如MSSQL有无限的MaxLifetime,这必须严格设置为0,否则你将有许多连接没有无限(无限期地)

关闭干杯,
Artanis Zeratul


推荐