我不知道你的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 至少为 120000 毫秒,使用默认值 1800000 毫秒。所以你不能设置maxLifeTime
为 30000ms(30*1000)。我猜你的HikariCP
版本至少早于 2.2.4。
但是当你找到最新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;
}
}
从这段代码中,maxLifeTime
至少在这个版本中已经更新到 30000ms。
HikariCP
因此,如果您想将 maxLifeTime 设置为 30000 毫秒,现在请将您的版本更新到最新版本 2.7.4。
但是如果你用 JDK 8 更新你的 HikariCP 版本到 2.7.4,我也推荐你两点:
1. 将maxLifeTime
值设置为至少 30000ms。
2. 设置值比mysql 的( )maxLifeTime
少几分钟,以避免断开连接异常。wait_timeout
show variables like "%timeout%"