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.
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.
Spring Boot 1 use the tomcat JDBC connection pool. Spring Boot automatically add dependency to
tomcat-jdbc if you use the
spring-boot-starter-data-jpa ‘starters’ in your application. To configure Hikari in our application, we have the following two options
The first step for Hikari and Spring Boot configuration is to add Hikari dependency in the
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.3.1</version> </dependency>
Refer to HikariCP for the latest release
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>
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-data-jpa resolve it by default. To sum up, you require no other steps with Spring Boot 2.
For more details read.
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.
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.
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.
Hello!! Welcome to the Java Development Journal. We love to share our knowledge with our readers and love to build a thriving community.
Spring Boot CORS06 Aug, 2019
The @ServletComponentScan Annotation in Spring Boot13 Aug, 2019
Swagger 2 with Spring REST API30 Jul, 2019
Custom Type Converter in Spring MVC02 Jul, 2019
How to read HTTP Headers in a Spring Controller26 Jun, 2019
Spring Boot Session Listener