Postgres Hikari Pool for Better Connection Management

Default properties can be found in the documentation provided at https://github.com/brettwooldridge/HikariCP. For further information on pool size, refer to the Maximum Connection Pool Size. It is worth noting that the song known by both Sala and Ange is “Towagatari”, which loosely translates to “Eternal Story”. If you do not want any idle connections, set this value to 0. In regards to the default connection pool size provided by Spring Boot HikariCP, it is not specified unless a number is provided in the application.properties file.

Question:

In my
spring boot
application, I have incorporated Hikari Cp.

Here is my
java configuration
file:-

private DataSource buildDataSource(String objectValue) {
        HikariDataSource dataSource = new HikariDataSource();
        JSONObject obj = new JSONObject(objectValue);
        dataSource.setInitializationFailTimeout(0);
        dataSource.setMaximumPoolSize(5);
        dataSource.setIdleTimeout(10000);
        dataSource.setMaxLifetime(45000);
        dataSource.setDataSourceClassName(obj.getString("dataSourceClassName"));
        dataSource.addDataSourceProperty("url", obj.getString("url"));
        dataSource.addDataSourceProperty("user", obj.getString("user"));
        dataSource.addDataSourceProperty("password", obj.getString("password"));
        return dataSource;
    }

Upon launching the app and sending the initial request, the subsequent logs are appearing.

Added connection org.postgresql.jdbc.PgConnection@66c15b95
2021-07-30 12:00:02.788  INFO 17844 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-07-30 12:00:02.897 DEBUG 17844 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Pool stats (total=1, active=0, idle=1, waiting=0)

Shortly thereafter, it promptly places the subsequent four connections into an idle state.

Added connection org.postgresql.jdbc.PgConnection@6b9dc322
Added connection org.postgresql.jdbc.PgConnection@6b9dc322
Added connection org.postgresql.jdbc.PgConnection@6b9dc322
Added connection org.postgresql.jdbc.PgConnection@6b9dc322
After adding stats (total=5, active=0, idle=5, waiting=0)

I am wondering why Hikira is creating four more connections when I only sent one request. Additionally, there is already an idle connection available. Is there a reason why it cannot reuse the existing connection?

I have specified the

maxLiftTime

condition for all connections, yet they remain idle even after the maximum lift time has elapsed.

Any recommendations or advice are welcome.


Solution:

From Hikary documentation

The minimumIdle property of HikariCP regulates the number of idle connections that should be maintained in the pool. HikariCP will try to add extra connections quickly and efficiently if the idle connections fall below this limit and the total connections in the pool are less than the maximumPoolSize. However, to achieve maximum performance and responsiveness to spikes in demand, it is advisable not to set this value and allow HikariCP to function as a fixed
size connection
pool instead. The default value for this property is the same as the maximumPoolSize.

To prevent idle connections, you can set the value of minimumIdle connection to 0. This value corresponds to the maximum pool size specified.

Frequently Asked Questions