As we all know, the springboot2 project uses HikariCP connection pooling, which is the “fastest connection pooling ever”. After the springBoot2 project system went online, a warning was found in the log:

WARN  com.zaxxer.hikari.pool.PoolBase - Retail_HikariCP - Failed to validate connection com.mysql.jdbc.JDBC4Connection@355cd37b (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
Copy the code

The warning is that the maxLifetime value is recommended to be set to a smaller value. This indicates that there is a problem with the configuration file. Some brief configuration instructions for the HikariCP connection pool are as follows:

Configuration items describe Constructor default value
connectionTimeout The maximum number of milliseconds to wait for connections from the pool 30000
idleTimeout The maximum amount of time a connection is allowed to remain idle in the pool 600000
maxLifetime Maximum connection lifetime in the pool 1800000

Change the maxLifetime value from 1800000 to 600000 as prompted, which is the same as idle-timeout.

# # database configuration spring. The datasource. Type = com. Zaxxer. Hikari. HikariDataSource spring. The datasource. DriverClassName = com.mysql.jdbc.Driver spring.datasource.u```rl = jdbc:mysql://localhost:3306/ssm? useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username = root spring.datasource.password = root ## Hikari connection pool configuration ------ https://github.com/brettwooldridge/HikariCP # # minimum number of idle connections spring. The datasource. Hikari. Minimum - idle = 5 # # free connection for most time, The default 600000 (10 minutes) spring. The datasource. Hikari. Idle - timeout = 600000 # # connection pool maximum number of connections, Default is 10. Spring datasource. Hikari. Maximum - the pool - size = 10 # # this property control back from a pool of connections the default autocommit behaviour, default: True spring. The datasource. Hikari. Auto - commit = true # # connection pool and spring. The datasource. Hikari. The pool - name = testHikariCP # # This property control connections in the pool, the longest life cycle value 0 means infinite life cycle, the default is 30 minutes 1800000 spring. The datasource. Hikari. Max - lifetime = 600000 # # database connection timeout, 30 seconds, by default The spring of 30000. The datasource. Hikari. Connection - the timeout = 30000 spring. The datasource. Hikari. Connection - test - query = SELECT 1Copy the code

But when the project started, there was another warning:


WARN  com.zaxxer.hikari.HikariConfig - Retail_HikariCP - idleTimeout is close to or more than maxLifetime, disabling it.

Copy the code

Idle-timeout +1 second >max-lifetime and max-lifetime>0. With two warnings, the problem of the HikariCP connection pool is understood more deeply. Change the maxLifetime value to a larger value than idle-timeout and set it to 800000 (13 minutes). Optimize the configuration file as follows according to the project situation:

# # database configuration spring. The datasource. Type = com. Zaxxer. Hikari. HikariDataSource spring. The datasource. DriverClassName = com.mysql.jdbc.Driver spring.datasource.u```rl = jdbc:mysql://localhost:3306/ssm? useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username = root spring.datasource.password = root ## Hikari connection pool configuration ------ https://github.com/brettwooldridge/HikariCP # # minimum number of idle connections spring. The datasource. Hikari. Minimum - idle = 5 # # free connection for most time, The default 600000 (10 minutes) spring. The datasource. Hikari. Idle - timeout = 600000 # # connection pool maximum number of connections, Default is 10. Spring datasource. Hikari. Maximum - the pool - size = 10 # # this property control back from a pool of connections the default autocommit behaviour, default: True spring. The datasource. Hikari. Auto - commit = true # # connection pool and spring. The datasource. Hikari. The pool - name = testHikariCP # # This property control connections in the pool, the longest life cycle value 0 means infinite life cycle, the default is 30 minutes 1800000 spring. The datasource. Hikari. Max - lifetime = 800000 # # database connection timeout, 30 seconds, by default The spring of 30000. The datasource. Hikari. Connection - the timeout = 30000 spring. The datasource. Hikari. Connection - test - query = SELECT 1Copy the code

After optimization, it went live and the warning never happened again. Every time the line must pay attention to details, details are all knowledge points.