JS7 - idleTimeout and maxLifetime settings of HikariCP

JS7 utilizes HikariCP as its default JDBC connection pool. Proper configuration of HikariCP's idleTimeout and maxLifetime settings is crucial for optimal performance and stability.

idleTimeout

This property defines the maximum amount of time that a connection is allowed to sit idle in the pool.

  • A value of 0 means that idle connections are never removed from the pool.

  • The minimum allowed value is 10000 ms (10 seconds).

  • Default: 600000 ms (10 minutes)

Hibernate Configuration
<property name="hibernate.hikari.idleTimeout">600000</property>

maxLifetime

This property defines the maximum lifetime of a connection in the pool.This value only applies to idle connections – in-use connections are never retired.

  • A value of 0 indicates no maximum lifetime (infinite), but still subject to the idleTimeout.

  • Minimum allowed value: 30000 ms (30 seconds)

  • Default: 1800000 ms (30 minutes

 Always set maxLifetime to slightly less than your DB server’s idle connection timeout.


Hibernate Configuration
<property name="hibernate.hikari.maxLifetime">1800000</property>


Problem

You may observe the following error messages:

HikariPool-1 - Failed to validate connection org.mariadb.jdbc.MariaDbConnection@6ba4798a ((conn=2096708) Connection.setNetworkTimeout cannot be called on a closed connection). Possibly consider using a shorter maxLifetime value.

HikariPool-1 - Failed to validate connection org.postgresql.jdbc.PgConnection@7341ee0b (This connection has been closed.). Possibly consider using a shorter maxLifetime value.


These errors occur when HikariCP tries to validate or use a connection that has already been closed by the database or network—without HikariCP being aware of it.

Analysis

  • HikariCP reuses connections to avoid the cost of creating new ones.

  • Databases like MariaDB or PostgreSQL may silently close idle or long-lived connections based on settings like:

    • wait_timeout (MariaDB)

    • idle_in_transaction_session_timeout (PostgreSQL)

As a result:

  • When maxLifetime is longer than the DB timeout, HikariCP may try to use a connection the DB has already killed.

  • These are often called zombie connections.

Solution

Set hibernate.hikari.maxLifetime to a value shorter than your database’s idle timeout.

Example: If your DB closes idle connections after 10 minutes (600000 ms), set:

Hibernate Configuration
<property name="hibernate.hikari.maxLifetime">540000</property> <!-- 9 minutes -->

This ensures that HikariCP retires connections before the DB does, preventing the errors mentioned earlier.


  • No labels