Introduction
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.
Hikari is a JDBC DataSource implementation that provides a connection pooling mechanism. Compared to other implementations, it promises to be lightweight, reliable, high-performance JDBC connection pool and better performing.
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.
Database Connection Pooling
Spring Boot uses Tomcat pooling tomcat-jdbc
by default, and follow this sequence to find the connection pool in general:
Tomcat pool -->> HikariCP -->> Commons DBCP -->> Commons DBCP2
Step 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.
Step 2: Adding 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>
Note: In Spring Boot 2, Hikari is the default DataSource implementation. However, to use the latest version, we need to add the Hikari dependency in the pom.xml explicitly. The dependency to Hikari is now automatically included in spring-boot-starter-data-jpa and spring-boot-starter-jdbc. The discovery algorithm that automatically determines a DataSource implementation now prefers Hikari over TomcatJDBC (see the reference manual). So, we have nothing to do if we want to use Hikari in an application based on Spring Boot 2.x, unless we want to use its latest version.
Step 3: 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 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>
Step 4: HikariCP Configurations
One of Hikari’s advantages over other DataSource implementations is the fact that it offers a lot of configuration parameters. Below are the basic Hikari Data Source properties to include for fine tuning the spring boot application and some explanation in detail can be found here
#maximum number of milliseconds that a client will wait for a connection
spring.datasource.hikari.connection-timeout = 20000
#minimum number of idle connections maintained by HikariCP in a connection pool
spring.datasource.hikari.minimum-idle= 10
#maximum pool size
spring.datasource.hikari.maximum-pool-size= 10
#maximum idle time for connection
spring.datasource.hikari.idle-timeout=10000
# maximum lifetime in milliseconds of a connection in the pool after it is closed.
spring.datasource.hikari.max-lifetime= 1000
#default auto-commit behavior.
spring.datasource.hikari.auto-commit =true
Step 5: Hikari Log Output
Once we start our application, monitor the console log, you may notice an identical output:
2020-11-08 13:39:57.896 INFO 10136 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure 2020-11-08 13:39:57.900 INFO 10136 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource] 2020-11-08 13:39:57.946 INFO 10136 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) Connection Polling datasource : HikariDataSource (HikariPool-1) 2020-11-08 13:39:57.949 INFO 10136 --- [ main] com.santhosh.hikari.SpringBootConfig : Started SpringBootConfig in 2.581 seconds (JVM running for 2.904) 2020-10-08 13:40:10.925 INFO 10136 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2020-11-08 13:40:10.925 INFO 10136 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2020-11-08 13:40:10.940 INFO 10136 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 14 ms 2020-11-08 13:40:10.960 WARN 10136 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig : HikariPool-1 - maxLifetime is less than 30000ms, setting to default 1800000ms. 2020-11-08 13:40:10.961 INFO 10136 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2020-11-08 13:40:11.070 INFO 10136 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
Conclusion
In this article, We learned how to configure Hiraki Connection pool with spring boot application version1.x. I would like to repeat “HirakiCP” is preferable for the concurrent database application. Sample Code can be found here