首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HikariCP和maxLifetime

HikariCP和maxLifetime
EN

Stack Overflow用户
提问于 2015-01-28 05:41:22
回答 2查看 52K关注 0票数 16

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

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

代码语言:javascript
复制
WARN com.zaxxer.hikari.HikariConfig - maxLifetime is less than 120000ms, using default 1800000ms.

我知道他们建议不要像我试着设置的那样低。但不幸的是,由于我不能改变的情况,在我们的生产环境中,每个打开超过50秒的TCP连接都将被终止。

EN

回答 2

Stack Overflow用户

发布于 2017-12-07 19:45:05

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

代码语言:javascript
复制
 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

代码语言:javascript
复制
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。

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

但是,如果您使用JDK8将您的HikariCP版本更新到2.7.4,我还推荐您两点:

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

maxLifeTime 2.设置比mysql的 wait_timeout**(**show variables like "%timeout%"**)少几分钟的值,避免断开连接异常。**

票数 8
EN

Stack Overflow用户

发布于 2018-12-06 10:28:34

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

代码语言:javascript
复制
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();

而且它是有效的。但请注意以下内容:

代码语言:javascript
复制
 cpConfig.setMinimumIdle(0);

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

干杯,

Artanis Zeratul

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28180562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档