I am trying to test idle connections behavior. I am expecting my code to run into an exception here, but it doesn't:
static public void testConnectionDS() throws SQLException {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(Jdbc_URL);
ds.setUsername(Username);
ds.setPassword(Password);
ds.setMaxLifetime(1000 * 60 * 3);
ds.setMinimumIdle(2);
ds.setIdleTimeout(1000 * 60 * 2);
Connection connection = ds.getConnection();
setupTable(connection);
try {
TimeUnit.MILLISECONDS.sleep(ds.getMaxLifetime() + ds.getIdleTimeout() + 1000 * 35); // around 6 miutes
executeQuery(connection);
} catch (Exception e) {
System.out.println(java.time.LocalTime.now() + " : executeQuery Exception: " + e.getMessage());
e.printStackTrace(System.out);
}
}
Priniting various setting, the seetings are:
KeepaliveTime:0
MaxLifetime:180,000
IdleTimeout:120,000
minimumIdle:2
maximumPoolSize:10
LeakDetectionThreshold:0
and my system settings is:
% sysctl -a |grep tcp |grep keep
net.inet.tcp.keepidle: 7200000
net.inet.tcp.keepintvl: 75000
net.inet.tcp.keepinit: 75000
net.inet.tcp.keepcnt: 8
net.inet.tcp.always_keepalive: 0
net.inet.mptcp.keepalive: 840
With MaxLifetime
of 3 minutes, IdleTimeout
of 2 minutes, minimumIdle
< maximumPoolSize
and disabled keepalive and leakdetection threshold, this connection should have not been able to execute executeQuery
function (which is a simple select query). But it does run into completion successfully.
What am I missing please?
You're misinterpreting the meaning of idleTimeout
in HikariCP: "This property controls the maximum amount of time that a connection is allowed to sit idle in the pool.". Your connection is not idle in the pool: your code has it checked out, so it's in use. Same with maxLifeTime
: " An in-use connection will never be retired, only when it is closed will it then be removed." (quoted text from HikariCP, Configuration (knobs, baby!))
HikariCP doesn't have a way to forcibly close connections after a timeout. The only thing it has is leakDetectionThreshold
, but that will just log that there is a potential connection leak, it will not revoke the connection.