Spring Boot Hikari DataSource Configuration Example

By Arvind Rai, September 17, 2023
On this page we will learn to configure Hikari DataSource in our Spring Boot application.
1. Spring boot prefers HikariCP for connection pool. If HikariCP is available on classpath, Spring Boot automatically chooses it. HikariCP is considered better in performance and concurrency over other connection pool.
2. Spring Boot prefers HikariCP > Tomcat pooling > Commons DBCP2 > Oracle UCP. It means if HikariCP is available, Spring Boot will pick HikariCP. If HikariCP is not available and Tomcat pooling is available, Spring Boot will pick Tomcat 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 file.
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.
Spring Boot Hikari DataSource Configuration Example

Step-1: Configure DataSource

application.properties
spring.datasource.url=jdbc:mysql://localhost/cp
spring.datasource.username=root
spring.datasource.password=Mysql@1234 

Step-2: HikariCP Specific Settings

Spring Boot configures HikariDataSource automatically. Find the HikariCP specific settings.
application.properties
spring.datasource.hikari.connection-timeout=20000
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=12
spring.datasource.hikari.idle-timeout=300000
spring.datasource.hikari.max-lifetime=1200000
spring.datasource.hikari.auto-commit=true 

Complete List of HikariCP Settings

Find the complete list of HikariCP specific settings.
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 

Change HikariCP Dependency

The spring-boot-starter-jdbc and spring-boot-starter-data-jpa starters automatically resolves HikariCP dependency. If we want to change version, we can resolve manually.
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>5.0.1</version>
</dependency> 

Reference

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us