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.

This quick tutorial shows how to configure a Spring Boot 3 application to use the Hikari DataSource.

2. Configuring Hikari With Spring Boot 3.x

Hikari is the default DataSource implementation in Spring Boot 3, as stated in the reference manual.

The dependency on Hikari is automatically included in spring-boot-starter-data-jpa and spring-boot-starter-jdbc. Hence, there’s nothing we need to do if we want to use Hikari in an application based on Spring Boot 3.x.

However, if we want 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>5.1.0</version>
</dependency>

3. Tuning Hikari Configuration Parameters

One of Hikari’s advantages over other DataSource implementations is 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 
...

In the above configuration, we’ve set the connection timeout to 30,000 milliseconds, idle timeout to 600,000 milliseconds, and maximum lifetime to 1,800,000 milliseconds. These parameter values can be adjusted based on the specific requirements of our application.

A comprehensive list of all available Hikari parameters with detailed explanations is available on the Hikari GitHub site, as well as in the Spring docs.

4. Monitoring Hikari With JMX

Hikari provides support for monitoring its connection pool through JMX (Java Management Extensions). JMX is a Java technology that enables the management and monitoring of applications at runtime. By enabling JMX for Hikari, we can gain valuable insights into the health and performance of our connection pool.

To enable JMX for Hikari, we need to add the following configuration property to our application.properties or application.yml file:

spring.datasource.hikari.registerMbeans=true

With this configuration in place, Hikari exposes its management beans through JMX. These beans can be accessed using JMX tools and libraries to monitor and analyze the connection pool. There are several popular JMX clients available, such as JConsole, VisualVM, and Prometheus with JMX exporter.

By connecting to the Java process hosting the application and using a JMX client, we can access various metrics and attributes provided by Hikari. Some of the key metrics include:

  • Connection count: current number of active connections
  • Pool utilization: percentage of connections in use
  • Connection acquisition time: time taken to acquire a connection from the pool
  • Connection creation time: time taken to create a new connection
  • Connection timeout count: number of connection acquisition attempts that timed out

These metrics can help us understand the behavior of our connection pool and identify any potential performance bottlenecks or issues. We can set up monitoring and alerting systems to track these metrics and take appropriate actions when necessary.

5. Conclusion

In this article, we saw how to configure the Hikari DataSource for a Spring Boot 3.x application, taking advantage of Spring Boot’s autoconfiguration capabilities. Furthermore, we highlighted the significance of enabling Hikari JMX to monitor and manage the connection pool, providing us with valuable insights and control over its behavior.

The code for the Spring Boot 3.x example is available over on GitHub.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE
res – Persistence (eBook) (cat=Persistence)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.