Spring Boot : Steps to Configure and Fine-Tune HikariCP DataSource

1. For better performance and concurrency in microservice architecture, HikariCP is recommended along with Spring Boot application. Main question is how to configure HikariCP in our microservice? Answer is, its simple and straight forward, if HikariCP jars are available on application classpath, application automatically use it in the application.
2. Suppose, we have multiple connection pools approaches in our microservice, then Spring Boot application prefers as below

HikariCP > Tomcat Connection Pooling > Commons DBCP2 > Oracle UCP.

It means if HikariCP is available, Spring Boot will pick HikariCP. If HikariCP is not available and Tomcat connection pooling is available, Spring Boot will pick Tomcat connection pooling, and so on.
3. HikariCP dependency can be obtained automatically if we use spring-boot-starter-jdbc or spring-boot-starter-data-jpa starters in pom.xml or build.gradle maven file in spring boot application.
4. Additional connection pool can be defined using DataSourceBuilder but auto-configuration will not occur for this DataSource.
5. To fine tune HikariCP implementation settings, use spring.datasource.hikari.* prefix in application.properties file of your microservice.

Step-1: How to Configure DataSource in Spring Boot

In application.properties we need to add following properties along with spring-boot-starter-jdbc and spring-boot-starter-data-jpa starters in microservice pom.xml.

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

application.properties (main configurations: url, usernam, password)

spring.datasource.url=jdbc:mysql://localhost/cp
spring.datasource.username=root
spring.datasource.password=thebasictechinfo

Note: We can configure additional HikariCP specific properties in application.properties for fine tuning. E.g.

spring.datasource.hikari.allow-pool-suspension
spring.datasource.hikari.auto-commit
spring.datasource.hikari.catalog
spring.datasource.hikari.connection-init-sql
spring.datasource.hikari.connection-test-query
spring.datasource.hikari.connection-timeout
spring.datasource.hikari.data-source-class-name
spring.datasource.hikari.data-source-j-n-d-i
spring.datasource.hikari.data-source-properties
spring.datasource.hikari.driver-class-name
spring.datasource.hikari.exception-override-class-name
spring.datasource.hikari.health-check-properties
spring.datasource.hikari.idle-timeout
spring.datasource.hikari.initialization-fail-timeout
spring.datasource.hikari.isolate-internal-queries
spring.datasource.hikari.jdbc-url
spring.datasource.hikari.keepalive-time
spring.datasource.hikari.leak-detection-threshold
spring.datasource.hikari.login-timeout
spring.datasource.hikari.max-lifetime
spring.datasource.hikari.maximum-pool-size
spring.datasource.hikari.metrics-tracker-factory
spring.datasource.hikari.minimum-idle
spring.datasource.hikari.password
spring.datasource.hikari.pool-name
spring.datasource.hikari.read-only
spring.datasource.hikari.register-mbeans
spring.datasource.hikari.scheduled-executor
spring.datasource.hikari.schema
spring.datasource.hikari.transaction-isolation
spring.datasource.hikari.username
spring.datasource.hikari.validation-timeout

Step-2: HikariCP Specific Settings in Spring Boot Application

No need to add any customized configuration to support HiariCP. Spring Boot application configures HikariDataSource automatically. E.g. application.properties

spring.datasource.hikari.connection-timeout=10000
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.maximum-pool-size=20
spring.datasource.hikari.idle-timeout=300000
spring.datasource.hikari.max-lifetime=1200000
spring.datasource.hikari.auto-commit=true

Step-3: HikariCP Specific Settings in Tomcat (server.xml)

Also, we can configure HikariCP in tomcat container as below using environmental variables.

<Resource name="jdbc/datasources/cp" auth="Container" global="jdbc/datasources/cp"
              factory="com.zaxxer.hikari.HikariJNDIFactory"
              driverClassName="oracle.jdbc.OracleDriver"
              jdbcUrl="${DB_URL}"
              type="javax.sql.DataSource"
              minimumIdle="${MINIDLECONNECTION}" 
              maximumPoolSize="${MAXPOOLSIZE}"
              connectionTimeout="${CONNECTIONTIMEOUT}"
              idleTimeout="${IDLETIMEOUT}"
              keepaliveTime="${KEEPALIVETIME}"
              maxLifetime="${MAXLIFETIME}"
              dataSource.user="${USERNAME}"
              dataSource.password="${PASSWORD}"
              closeMethod="close"
    />	

Reference


Keep learning 🙂 Have a great day.