1. Overview
Hikari is a JDBC DataSource implementation that provides a connection pooling mechanism.
Compared to other implementations, it promises to be lightweight and better performing. For an introduction to Hikari, see this article.
This quick tutorial shows how we can configure a Spring Boot 1 or Spring Boot 2 application to use the Hikari DataSource.
2. Configuring Hikari with Spring Boot 1.x
Spring Boot 1.x uses the Tomcat JDBC Connection Pool by default.
As soon as we include spring-boot-starter-data-jpa into our pom.xml, we’ll transitively include a dependency to the Tomcat JDBC implementation. During runtime, Spring Boot will then create a Tomcat DataSource for us to use.
To configure Spring Boot to use the Hikari Connection Pool instead, we have two options.
2.1. Maven Dependency
First, we need to include the dependency on Hikari in our pom.xml:
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.2.0</version> </dependency>
The most current version can be found on Maven Central.
2.2. Explicit Configuration
The safest way to tell Spring Boot to use Hikari is configuring the DataSource implementation explicitly.
To do this, we simply set the property spring.datasource.type to the fully-qualified name of the DataSource implementation we want to use:
@RunWith(SpringRunner.class) @SpringBootTest( properties = "spring.datasource.type=com.zaxxer.hikari.HikariDataSource" ) public class HikariIntegrationTest { @Autowired private DataSource dataSource; @Test public void hikariConnectionPoolIsConfigured() { assertEquals("com.zaxxer.hikari.HikariDataSource", dataSource.getClass().getName()); } }
2.3. Removing the Tomcat JDBC Dependency
The second option is to let Spring Boot find the Hikari DataSource implementation itself.
If Spring Boot cannot find the Tomcat DataSource in the classpath, it will automatically look for the Hikari DataSource next. The discovery algorithm is described in the reference manual.
To remove the Tomcat Connection Pool from the classpath we can exclude it in our pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <exclusions> <exclusion> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </exclusion> </exclusions> </dependency>
Now, the test from the previous section will also work without setting the spring.datasource.type property.
3. Configuring Hikari with Spring Boot 2.x
In Spring Boot 2, Hikari is the default DataSource implementation.
This is what’s changed from Spring Boot 1.x:
- the dependency to Hikari is now automatically included in spring-boot-starter-data-jpa
- the discovery algorithm that automatically determines a DataSource implementation now prefers Hikari over TomcatJDBC (see the reference manual).
Thus, we have nothing to do if we want to use Hikari in an application based on Spring Boot 2.x.
4. Tuning Hikari Configuration Parameters
One of Hikari’s advantages over other DataSource implementations is the fact that it offers a lot of configuration parameters.
We can specify the values for these parameters by using the prefix spring.datasource.hikari and appending the name of the Hikari parameter:
spring.datasource.hikari.connectionTimeout=30000 spring.datasource.hikari.idleTimeout=600000 spring.datasource.hikari.maxLifetime=1800000 ...
A list of all Hikari parameters with a good explanation is available on the Hikari Github Site.
5. Conclusion
In this article, we’ve configured the Hikari DataSource implementation in a Spring Boot 1.x application and learned how to leverage Spring Boot 2.x’s autoconfiguration.
The code for the Spring Boot 1.x example is available here, and the code for the Spring Boot 2.x example is available here.