Configuring Hikari Connection Pool with Spring Boot

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-jpastarters’ in your application. To configure Hikari  in our application, we have the following two options

  1. Add the HikariCP maven dependency.
  2. 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>

Refer to HikariCP  for the latest release

 

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.

For more details read.

 

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. Hikari Log Output

Once we start our application, keep an eye on 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.

Java Development Journal

Hello!! Welcome to the Java Development Journal. We love to share our knowledge with our readers and love to build a thriving community.

follow me on:

2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Umesh Awasthiropo Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
ropo
Guest
ropo

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

Umesh Awasthi
Admin
Umesh Awasthi

Agree, but this is just to show how we can setup the value based on our requirement.