Spring Boot Hikari Connection Pool Configurations

Hikari Connection Pool

Here in this Spring Boot application example, I am going to show you how to use Hikari connection pool to use dedicated HikariDataSource instead of the use of database specific datasource.

Hikari connection pool commonly known as HikariCP is extremely fast, light-weight, simple and production ready JDBC connection pool.

Spring Boot configures Hibernate as a default JPA provider, so you don’t need to configure its related beans until you need to customize them.

A connection pool maintains a pool of connections that can be reused in future when connection requests to the database are required.

With Spring Boot 2 and Spring Boot 3, HikariCP is the default connection pool and it is transitively imported with either spring-boot-starter-jdbc or spring-boot-starter-data-jpa starter dependency, so you don’t need to add any extra dependency to your project.

If you still want to use the other version (for example, latest version) of HikariCP than the version which is imported through spring-boot-starter-jdbc or spring-boot-strater-data-jpa starter dependency then you can use the following dependency:

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>5.0.1</version>
</dependency>

Hikari Connection Pool Configurations

Spring boot exposes Hikari related properties using spring.datasource.hikari namespace.

To configure Hikari connection pool you can use application.properties file. Here I have shown most commonly used properties in the application.property file:

spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.connection.provider_class=com.zaxxer.hikari.hibernate.HikariConnectionProvider

spring.datasource.hikari.connection-timeout=20000
#spring.datasource.hikari.idle-timeout=10000 #has no effect as the pool is operating as a fixed size pool
spring.datasource.hikari.max-lifetime=30000
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.pool-name=HikariConnPool
#spring.datasource.hikari.auto-commit=true #default value is true
spring.datasource.hikari.data-source-properties.cachePrepStmts=true
spring.datasource.hikari.data-source-properties.prepStmtCacheSize=250
spring.datasource.hikari.data-source-properties.prepStmtCacheSqlLimit=2048
spring.datasource.hikari.data-source-properties.useServerPrepStmts=true
spring.datasource.hikari.data-source-properties.useLocalSessionState=true
spring.datasource.hikari.data-source-properties.rewriteBatchedStatements=true
spring.datasource.hikari.data-source-properties.cacheResultSetMetadata=true
spring.datasource.hikari.data-source-properties.cacheServerConfiguration=true
spring.datasource.hikari.data-source-properties.elideSetAutoCommits=true
spring.datasource.hikari.data-source-properties.maintainTimeStats=false

By setting the spring.datasource.type property, I am explicitly forcing the Hikari connection pool to use.

The default MySQL Dialect is MySQL5Dialect, so if you are using other version of MySQL then you need to update the Dialect version also. Here I am using MySQL 8.1.0, so I have changed it to MySQL8Dialect:

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

The brief explanations for the used properties are given below:

datasource.hikari.maximum-pool-size: Sets the maximum number of connections in the pool. This includes both idle and in-use connections. The default value is zero.
datasource.hikari.minimum-idle: Sets the minimum number of idle connections that HikariCP maintains in the connection pool.
datasource.hikari.idle-timeout: Sets the maximum amount of time that a connection remains idle in the pool. This setting only applies when minimum-idle is less than maximum-pool-size. The minimum value is 10000ms (10 seconds). The default value is 600000 (10 minutes).
datasource.hikari.max-lifetime: Sets the maximum lifetime of a connection in the pool. An in-use connection will never retire. HikariCP removes it only when it is closed. The minimum value is 30000 ms (30 seconds). The default value is 1800000 ms (30 minutes). If you set value less than 30000 ms then the default value 1800000 ms will be set to this parameter.
datasource.hikari.keepalive-time: Sets how frequently HikariCP will attempt to keep a connection alive. The minimum value you can set is 30000 ms (30 seconds). However, a value in the range of minutes is most desirable. The default is 0 which disables this setting.
datasource.hikari.pool-name: Sets the name of the connection pool that logging statements should display.
datasource.hikari.auto-commit: Sets the default auto-commit behavior of connections that the pool returns. The default value is true.

datasource.hikari.data-source-properties.cachePrepStmts: You should set the value to true so that other configurations, like prepStmtCacheSize and prepStmtCacheSqlLimit have any effect. By default, HikariCP disables this. You must set this parameter to true.
datasource.hikari.data-source-properties.prepStmtCacheSqlLimit: Sets the maximum length of a prepared SQL statement that the driver will cache. The MySQL server’s default length is 256. The recommended setting is 2048 because ORM frameworks like Hibernate, the default value is below the threshold of the generated statement’s length.
datasource.hikari.data-source-properties.prepStmtCacheSize: Sets the number of prepared statements that the MySQL drivers will cache per connection. The default value for this parameter is 25. The recommended setting is between 250-500.
datasource.hikari.data-source-properties.useServerPrepStmts: Set this property to true for newer versions of MySQL. This is because they support server-side prepared statements that provides a substantial performance boost.
datasource.hikari.data-source-properties.elideSetAutoCommits: Sets the default auto-commit behavior of connections that the pool returns. This parameter has true a default value.

The logging and diagnostic settings are listed below. These parameters will log the connection pool’s configuration values while application run.

level.com.zaxxer.hikari.HikariConfig=DEBUG
level.com.zaxxer.hikari=DEBUG

You can check more about the parameters HikariCP Configuration Parameters.

The default JDBC datasource uses the following connection properties:

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/roytuts
spring.datasource.username=root
spring.datasource.password=root

So even if you have HikariCP in the classpath, the above configuration properties will not work because HikariCP does not have url property, it has jdbcUrl property. To fix this you need to either change the url to jdbcUrl as follows:

spring.datasource.jdbcUrl=jdbc:mysql://localhost:3306/roytuts

Or force the connection pool to use the implementation of HikariDataSource.

spring.datasource.type=com.zaxxer.hikari.HikariDataSource

Or you can create a bean instance of HikariDataSource instead of DataSource using Java configuration but remember using HikariDataSource bean instance will remove the auto-configuration stuffs.

HikariCP being fast and simple, it can perform better than other connection pool, but before you use HikariCP you need to analyze the requirements of the project whether HikariCP would be best fit or any other connection pool needs to be used.

Sample Project

I have created a sample project that uses HikariCP and you can download it for reference purpose. The sample project uses Spring Boot 3.1.3, MySQL 8.1.0, Maven 3.8.5, Java 19.0.1.

Leave a Reply

Your email address will not be published. Required fields are marked *