In this article, we will learn the steps to configure Hikari with Spring Boot. We will cover steps to configure Hikari for both Spring Boot 1 and Spring Boot 2 application.
Introduction
HikariCP is a reliable, high-performance JDBC connection pool. It is much faster, lightweight and have better performance as compare to other connection pool API. Because of all these compelling reasons, HikariCP is now the default pool implementation in Spring Boot 2. In this article, we will have a closer look to configure Hikari with Spring Boot.
1. Configuring Hikari with Spring Boot 1
Spring Boot 1 use the tomcat JDBC connection pool. Spring Boot automatically add dependency to tomcat-jdbc
if you use the spring-boot-starter-jdbc
or spring-boot-starter-data-jpa
‘starters’ in your application. To configure Hikari in our application, we have the following two options
- Add the HikariCP maven dependency.
- Remove the Tomcat JDBC connection pool and let Spring Boot look for HikariCP in the classpath.
1.1 Maven Dependency
The first step for Hikari and Spring Boot configuration is to add Hikari dependency in the pom.xml
file:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.3.1</version>
</dependency>
1.2 Remove Tomcat JDBC Dependency
Once we have the Hikari dependency in the class path, the next step is to remove the Tomcat JDBC dependency. This step will force Spring Boot to look for the Hikari data source in the class path. We can use the Maven standard <exclude>
tag for this.
<!-- exclude tomcat jdbc connection pool, use HikariCP -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
2. Configuring Hikari with Spring Boot 2
Hikari is the default DataSource implementation with Spring Boot 2. This means we need not add explicit dependency in the pom.xml. The spring-boot-starter-jdbc
and spring-boot-starter-data-jpa
resolve it by default. To sum up, you require no other steps with Spring Boot 2.
3. HikariCP Configurations
Let’s look at some important properties of the Hikari data source which can be useful for fine tuning our application.
spring.datasource.hikari.connection-timeout = 20000 #maximum number of milliseconds that a client will wait for a connection
spring.datasource.hikari.minimum-idle= 10 #minimum number of idle connections maintained by HikariCP in a connection pool
spring.datasource.hikari.maximum-pool-size= 10 #maximum pool size
spring.datasource.hikari.idle-timeout=10000 #maximum idle time for connection
spring.datasource.hikari.max-lifetime= 1000 # maximum lifetime in milliseconds of a connection in the pool after it is closed.
spring.datasource.hikari.auto-commit =true #default auto-commit behavior.
4. Data Source Configuration
If you like to create a custom data source for your Spring Boot Hikari, we can create a simple configuration
class
@Configuration
public class DataSourceConfiguration {
@Bean(name = "customDataSource")
@ConfigurationProperties("spring.datasource")
public DataSource customDataSource() {
return DataSourceBuilder.create().build();
}
}
5. Hikari Log Output
Once we start our application, monitor the console log, you may notice an identical output:
2019-03-24 17:43:58.066 INFO 51692 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-03-24 17:43:58.072 WARN 51692 --- [ restartedMain] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2019-03-24 17:43:58.353 INFO 51692 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
Summary
In this short article, we saw how to configure Hikari with Spring Boot. We also leaned that Hikari is the default data source in Spring Boot.
spring.datasource.hikari.max-lifetime= 1000 # maximum lifetime in milliseconds of a connection in the pool after it is closed
1 second is not a useful value. also the comment is wrong. see https://github.com/brettwooldridge/HikariCP#configuration-knobs-baby
Agree, but this is just to show how we can setup the value based on our requirement.
Automatically gets taken care
2020-07-21 07:55:41.710 WARN 63445 — [ restartedMain] com.zaxxer.hikari.HikariConfig : HikariPool-1 – maxLifetime is less than 30000ms, setting to default 1800000ms.
[WARN] com.zaxxer.hikari.HikariConfig – HikariPool-1 – maxLifetime is less than 30000ms, setting to default 1800000ms.