

Table of Contents
1. Overview
This article is for Spring boot JDBC HikariCP Example. HikariCP is very popular and known database connection pooling library, especially for performance and concurrency matters. Spring boot by default use tomcat connection pooling but we can configure HikariCP easily with spring boot.
According to spring boot documentation, Spring boot also giving high preference to HikariCP for performance and concurrent database application.
In our previous article Spring boot common dbcp2 connection pool, We have explained what is requirements of connection pooling.
Here are steps to configure HikariCP with spring boot application:
Step 1: Exclude Tomcat Connection Pooling Dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <!--It contains database base related classes--> <exclusions> <exclusion> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </exclusion> </exclusions> </dependency>
Step 2: Add HikariCP Dependency
<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>2.7.8</version> </dependency>
2. Example

Spring boot JDBC HikariCP Example
2.1 pom.xml
Here we have excluded tomcat-jdbc
connection pool and add HikariCP
dependency in classpath.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring-boot-example</groupId> <artifactId>Spring-boot-JDBC-HikariCP-Example</artifactId> <description>Spring boot JDBC HikariCP Example</description> <version>1.0-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> </parent> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <!--It contains database base related classes--> <exclusions> <exclusion> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </exclusion> </exclusions> </dependency> <!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>2.7.8</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.2 application.properties
data source related properties available here. We can configure Hikari related properties using spring.datasource.hikari.*
.
Here are all properties which are supported by Hiraki
spring.datasource.url=jdbc:mysql://localhost/demo_database spring.datasource.username=root spring.datasource.password= spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2.3 SpringBootConfig
Here we have Autowired DataSource
to check which connection pool is used by spring boot. At startup of the application, Spring boot will print connection pool object so we can check that Hikari collection pool is configured or not.
package com.javadeveloperzone; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import javax.sql.DataSource; /** * Created by JavaDeveloperZone on 19-07-2017. */ @SpringBootApplication @ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute public class SpringBootConfig implements CommandLineRunner { @Autowired private DataSource dataSource; public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootConfig.class, args); // it wil start application } @Override public void run(String... args) throws Exception { System.out.println("Connection Polling datasource : "+ dataSource); // check connection pooling } }
2.4 EmployeeController
package com.javadeveloperzone.controller; import com.javadeveloperzone.dao.EmployeeDAO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.sql.DataSource; /** * Created by JavaDeveloperZone on 19-07-2017. */ @RestController public class EmployeeController { @Autowired private EmployeeDAO employeeDAO; @RequestMapping(value = "getEmployeeList") public java.util.List getEmployeeList() { return employeeDAO.getEmployeeList(); } }
2.5 EmployeeDAO
Using JDBC template we have retrieved data from the database.
package com.javadeveloperzone.dao; import com.javadeveloperzone.model.Employee; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Component; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * Created by JavaDeveloperZone on 03-08-2017. */ @Component public class EmployeeDAO { @Autowired private JdbcTemplate jdbcTemplate; public java.util.List<Employee> getEmployeeList() { List<Map<String, Object>> employees = jdbcTemplate.queryForList("select * from employee"); return employees.stream().map(e -> { Employee employee = new Employee(); employee.setEmployeeName(e.get("employeeName").toString()); employee.setEmployeeId((Integer) e.get("employeeId")); employee.setEmployeeRole((String) e.get("employeeRole")); return employee; }).collect(Collectors.toList()); } }
2.6 Employee
package com.javadeveloperzone.model; /** * Created by JavaDeveloperZone on 03-08-2017. */ public class Employee { private int employeeId; private String employeeName; private String employeeRole; public int getEmployeeId() { return employeeId; } public void setEmployeeId(int employeeId) { this.employeeId = employeeId; } public String getEmployeeRole() { return employeeRole; } public void setEmployeeRole(String employeeRole) { this.employeeRole = employeeRole; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } }
2.7 Output:
Here is output log so we can check it Hikari Connection pool is configured in our application.
2018-04-01 11:01:31.411 INFO 65724 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure 2018-04-01 11:01:31.427 INFO 65724 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource] 2018-04-01 11:01:31.512 INFO 65724 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
3. Conclusion
In this article, We learned that how to configure Hiraki Connection pool with spring boot application. I like to say again HirakiCP is preferable for the concurrent database application.