hikariPool - Failed to validate connection org.mariadb.jdbc.MariaDbConnection@7aa2f8a6 ((conn=463456) Connection.setNetworkTimeout cannot be called on a closed connection). Possibly consider using a shorter maxLifetime value.
package com.zaxxer.hikari.pool;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.SQLExceptionOverride;
import com.zaxxer.hikari.metrics.IMetricsTracker;
import com.zaxxer.hikari.pool.HikariPool.PoolInitializationException;
import com.zaxxer.hikari.util.DriverDataSource;
import com.zaxxer.hikari.util.PropertyElf;
import com.zaxxer.hikari.util.UtilityElf;
import com.zaxxer.hikari.util.UtilityElf.DefaultThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
abstract class PoolBase {
boolean isConnectionDead(final Connection connection)
{
try {
try {
setNetworkTimeout(connection, validationTimeout);
final var validationSeconds = (int) Math.max(1000L, validationTimeout) / 1000;
if (isUseJdbc4Validation) {
return !connection.isValid(validationSeconds);
}
try (var statement = connection.createStatement()) {
if (isNetworkTimeoutSupported != TRUE) {
setQueryTimeout(statement, validationSeconds);
}
statement.execute(config.getConnectionTestQuery());
}
}
finally {
setNetworkTimeout(connection, networkTimeout);
if (isIsolateInternalQueries && !isAutoCommit) {
connection.rollback();
}
}
return false;
}
catch (Exception e) {
lastConnectionFailure.set(e);
logger.warn("{} - Failed to validate connection {} ({}). Possibly consider using a shorter maxLifetime value.",
poolName, connection, e.getMessage());
return true;
}
}
...
스프링에서 다음과 설정을 추가하면 경고 로그는 없어진다.
spring:
datasource:
hikari:
max-lifetime: 177000 # 177초, hikari는 RDS wait_timeout 보다 2 ~ 3초 정도 짧게 줄 것을 권장
hikari CP는 네트워크 지연을 고려하여 max-lifetime을 wait_timeout보다 2~3초 정도 짧게 줄 것을 권고한다.
또는 DB의 wait_timeout 설정을 max-lifetime 보다 길게 설정해줄 수도 있다.
mysql> SHOW GLOBAL VARIABLES LIKE 'wait_timeout';
Variable_name | Value |
---|---|
wait_timeout | 1830 |
SQL>
select PROFILE
, RESOURCE_NAME
, RESOURCE_TYPE
, LIMIT
, COMMON
from dba_profiles
where profile = 'DEFAULT'
order by 1,2;
PROFILE | RESOURCE_NAME | RESOURCE | LIMIT |
---|---|---|---|
DEFAULT | IDLE_TIME | KERNEL | 15 |