HikariCP和maxLifetime

时间:2015-01-27 21:41:22

标签: java connection-pooling hikaricp

我将项目移至HikariCP。到目前为止一切都很顺利,但有一个设置我遇到了麻烦。

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

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

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

2 个答案:

答案 0 :(得分:3)

我不知道你的HikariCP版本,但是在版本2.2.4中你会发现它会抛出上述警告的原因。 HikariConfig.class(在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。

但是当你找到the latest HikariCP version 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;
    }

}

从此代码开始,maxLifeTime至少在此版本中已更新为30000毫秒。

现在请将您的HikariCP版本更新到最新版本2.7.4,如果您想将maxLifeTime设置为30000毫秒。

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

<强> 1。将maxLifeTime值设置为至少30000毫秒。

<强> 2。将maxLifeTime值设置为比mysql的wait_timeoutshow variables like "%timeout%")少几分钟,以避免连接异常中断。

答案 1 :(得分:0)

使用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