Spring Boot HikariCP DataSource Configuration

HikariCP is a fast, reliable and lightweight production-ready JDBC connection pool. Learn about various options for configuring the HikariCP with Spring Boot and Hibernate.

1. Default Connection Pool in Spring Boot

Spring Boot configures Hibernate as the default JPA provider; so we don’t need to configure its related beans until we want to customize them.

1.1. With Spring Boot 2.x

Spring boot 2.x uses HikariCP as the default connection pool. It is transitively imported with spring-boot-starter-jdbc or spring-boot-starter-data-jpa starter dependency, so we do not need to do anything extra.

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
</dependencies>

By setting the spring.datasource.type property, we can explicitly force the connection pool to use. This is especially important if we run the application in a Tomcat container where tomcat-jdbc dependency is provided by default.

spring.datasource.type=com.zaxxer.hikari.HikariDataSource

1.2. With Spring Boot 1.x

Spring boot 1.x had been using TomcatJDBC so if you are still using it then we need to import its latest version and configure it explicitly.

<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>
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>5.0.1</version>
</dependency>

2. HikariCP Configuration

Spring Boot exposes Hikari-specific settings to spring.datasource.hikari namespace. Below are the most commonly used properties for configuring the datasource and HikariCP connection pool. I will highly recommend understanding the usage of each property option and its impact on the application. Use these properties on a need basis, and define their values as per requirements.

Go through the HikariCP official GitHub page for an explanation of various configuration options. Bookmark this page listing all Spring boot application.properties options available for quick reference.

We should use logging level-related properties only in development mode, and turn off in production.

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.connection.provider_class=com.zaxxer.hikari.hibernate.HikariConnectionProvider
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/test_db
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.hikari.connection-timeout=50000
spring.datasource.hikari.idle-timeout=300000
spring.datasource.hikari.max-lifetime=900000
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.pool-name=ConnPool
spring.datasource.hikari.connection-test-query=select 1 from dual

spring.datasource.hikari.data-source-properties.cachePrepStmts=true
spring.datasource.hikari.data-source-properties.prepStmtCacheSize=250
spring.datasource.hikari.data-source-properties.prepStmtCacheSqlLimit=2048
spring.datasource.hikari.data-source-properties.useServerPrepStmts=true
spring.datasource.hikari.data-source-properties.useLocalSessionState=true
spring.datasource.hikari.data-source-properties.rewriteBatchedStatements=true
spring.datasource.hikari.data-source-properties.cacheResultSetMetadata=true
spring.datasource.hikari.data-source-properties.cacheServerConfiguration=true
spring.datasource.hikari.data-source-properties.elideSetAutoCommits=true
spring.datasource.hikari.data-source-properties.maintainTimeStats=false

logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
logging.level.com.zaxxer.hikari=TRACE

The above properties use MySQL as the backend database, so please use the appropriate property values if we use a different database such as Oracle DB or Microsoft SQL Server.

3. JDBC Connection URL

The default datasource connection properties are generally as follows:

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.pool-size=30

If we have Hikari on the classpath, this generic setup does not work, because Hikari has no url property. It has jdbcUrl property. To fix this, we must follow one of two possible approaches.

Either fix the property name from URL to jdbcUrl:

spring.datasource.jdbc-url=jdbc:mysql://localhost/test

Or, force the connection pool to use and return a dedicated implementation of HikariDataSource. We can do the using the properties file.

spring.datasource.type=com.zaxxer.hikari.HikariDataSource

Or we can use DataSourceBuilder to register a bean of type HikariDataSource. Note that If we create our own DataSource, the auto-configuration backs off.

@Configuration(proxyBeanMethods = false)
public class AppDataSourceConfiguration {

    @Bean
    @ConfigurationProperties("app.datasource")
    public HikariDataSource dataSource() {
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }
}

4. Conclusion

This tutorial taught us to configure HikariCP connection pooling with Spring boot 2.x and 1.x. We also learned to configure various properties to customize the runtime behavior.

Happy Learning !!

Sourcecode Download

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial