Search code examples
javaspringpostgresqlspring-boothikaricp

Hikari connection pool postgres


I'm using Hikari Cp in my spring boot application.

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;
    }


when I start the application and after sending first request I'm getting the below logs.

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)

after few seconds it immediately adds next four connections into 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)

My question here, I sent only a one request and why hikira is adding 4 more additional connections and already a one idle connection why can't it reuse the same connection.

and I have given the maxLiftTime condition for each connection and after the maxLiftTime passes still the connections are in the idle state.

so any suggestions would be helpful..


Solution

  • From Hikary documentation

    🔢minimumIdle: This property controls the minimum number of idle connections that HikariCP tries to maintain in the pool. If the idle connections dip below this value and total connections in the pool are less than maximumPoolSize, HikariCP will make a best effort to add additional connections quickly and efficiently. However, for maximum performance and responsiveness to spike demands, we recommend not setting this value and instead allowing HikariCP to act as a fixed size connection pool. Default: same as maximumPoolSize

    It is using as mininimumIdle connection the max pool size you have specified. Set this value to 0 if you do not want any idle connection at all