In some cases, when using a Hikari connection pool and a MySQL or MariaDB database, you may see the following error in the logs:
Failed to validate connection org.mariadb.jdbc.MariaDbConnection@649c7ea2 (Connection.setNetworkTimeout cannot be called on a closed connection). Possibly consider using a shorter maxLifetime value.
In most cases, this means that the value for maxLifetime of Hikari connection is configured to be higher than the wait_timeout of MySQL/MariaDB. So it happens that Hikari tries to use the connection which was already closed by the database server.
wait_timeout is the “number of seconds the server waits for activity on a noninteractive connection before closing it”.
maxLifetime “property controls the maximum lifetime of a connection in the pool … and it should be several seconds shorter than any database or infrastructure imposed connection time limit”. The unit is milliseconds.
To check the current value set for wait_timeout:
MariaDB [(none)]> show global variables like "wait_timeout"; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | wait_timeout | 600 | +---------------+-------+
This means that the global value for wait_timeout is 600 seconds. So you need to set the value for Hikari maxLifetime to be several seconds less.
For example, if you are using Spring Boot, set the following in the
spring.datasource.hikari.max-lifetime = 580000
If you configured Hikari’s minimumIdle to be less than maximumPoolSize, then also adjust the value for idleTimeout accordingly.