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 2 or Spring Boot 1 application to use the Hikari DataSource.

2. Configuring Hikari With Spring Boot 2.x

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:

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>4.0.3</version>
</dependency>

This is what’s changed from Spring Boot 1.x:

  • 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.
**

3. 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 as well as in the Spring docs.

4. 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.

4.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>4.0.3</version>
</dependency>

The most current version can be found on Maven Central.

4.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());
    }
}

4.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.

5. Conclusion

In this article, we configured the Hikari DataSource implementation in a Spring Boot 2.x application. And we learned how to leverage Spring Boot’s autoconfiguration.

We also had a look at the changes required to configure Hikari when using Spring Boot 1.x.

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.