Home Spring Boot Connection Pooling with HikariCP
Post
Cancel

Spring Boot Connection Pooling with HikariCP

Spring Boot Connection Pooling with HikariCP: A Beginner’s Guide

When building a Spring Boot application that communicates with a database, connection pooling can greatly improve performance by reducing the number of times your application needs to open a new database connection. HikariCP is a popular choice for connection pooling in Spring Boot due to its high performance and easy configuration.

In this guide, we’ll walk through the basics of setting up HikariCP for connection pooling in a Spring Boot application.

HikariCP Dependency:

From Spring Boot-2 HikariCP is the default pool implementation

1
2
3
4
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
</dependency>

HikariCP in application.properties

We need to configure HikariCP in our application.properties file. Here’s an example configuration:

1
2
3
4
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=5
spring.datasource.hikari.minimum-idle=1
spring.datasource.hikari.idle-timeout=600000

This configuration sets the connection timeout to 60 seconds, the maximum pool size to 5 connections, the minimum number of idle connections to 1, and the idle timeout to 10 minutes.

Here are some of the most popular HikariCP properties that can be configured in the application.properties file in a Spring Boot application:

  • spring.datasource.hikari.maximum-pool-size: This property sets the maximum number of connections that can be created in the connection pool.

  • spring.datasource.hikari.minimum-idle: This property sets the minimum number of idle connections that should be maintained in the connection pool.

  • spring.datasource.hikari.connection-timeout: This property sets the maximum amount of time that HikariCP will wait for a connection to become available in the pool before throwing an exception.

  • spring.datasource.hikari.idle-timeout: This property sets the maximum amount of time that a connection can remain idle in the pool before it is removed.

  • spring.datasource.hikari.max-lifetime: This property sets the maximum amount of time that a connection can remain in the pool before it is closed and replaced with a new one.

  • spring.datasource.hikari.connection-test-query: This property sets the SQL query that will be used to test connections before they are returned from the pool.

  • spring.datasource.hikari.pool-name: This property sets the name of the connection pool.

  • spring.datasource.hikari.leak-detection-threshold: This property sets the amount of time that a connection can be checked out from the pool before a leak is detected.

  • spring.datasource.hikari.validation-timeout: This property sets the maximum amount of time that HikariCP will wait for a connection to be validated before throwing an exception.

  • spring.datasource.hikari.isolate-internal-queries: This property controls whether HikariCP will isolate internal queries (such as database metadata queries) to a separate connection.

These properties can be used to fine-tune the performance and behavior of the HikariCP connection pool in a Spring Boot application

HikariCP DataSource in Spring

With HikariCP configured, we can now use it as the data source for our Spring application. To do this, we’ll need to create a bean for the HikariCP DataSource class.

1
2
3
4
5
6
7
8
9
10
11
12
13
@Configuration
public class AppConfig {

    @Bean
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost/test");
        config.setUsername("root");
        config.setPassword("password");
        config.setMaximumPoolSize(5);
        return new HikariDataSource(config);
    }
}

In this example, we’ve created a DataSource bean that uses the HikariCP configuration we defined earlier in application.properties. We’ve also set the jdbcUrl, username, and password for our database.

Benefits

It offers a number of benefits that make it a popular choice for many Spring Boot applications:

  • High performance: HikariCP is known for its impressive performance, particularly in high-traffic applications. It uses a number of performance-boosting techniques, such as lazy connection initialization and aggressive connection recycling, to deliver fast and efficient connection pooling.

  • Lightweight and efficient: HikariCP is one of the most lightweight connection pools available, with a small footprint and minimal overhead. This means it can run smoothly on even the most resource-constrained systems, and can handle large numbers of connections with ease.

  • Easy to configure: HikariCP is easy to configure and customize, with a wide range of configuration options that can be fine-tuned to match your application’s specific requirements. It also provides a number of helpful diagnostic features, such as connection statistics and leak detection, to help you diagnose and resolve issues quickly.

  • Compatibility: HikariCP is compatible with a wide range of databases and JDBC drivers, making it a flexible choice for many different types of applications.

  • Overall, HikariCP is a powerful and reliable connection pool that is well-suited to modern, high-performance Spring Boot applications. Its performance, efficiency, and ease of use make it a popular choice for many developers, and its wide range of configuration options make it a versatile tool for handling a variety of use cases.

Conclusion

With HikariCP, setting up connection pooling in a Spring Boot application is easy and highly performant. By following the steps outlined in this guide, you can configure HikariCP in your own project and see the benefits of connection pooling for yourself.

This post is licensed under CC BY 4.0 by the author.