Partner – Jmix-Haulmont – NPI (cat= Spring Boot)
announcement - icon

Whether you're just starting out or have years of experience, Spring Boot is obviously a great choice for building a web application.

Jmix builds on this highly powerful and mature Boot stack, allowing devs to build and deliver full-stack web applications without having to code the frontend. Quite flexibly as well, from simple web GUI CRUD applications to complex enterprise solutions.

Concretely, The Jmix Platform includes a framework built on top of Spring Boot, JPA, and Vaadin, and comes with Jmix Studio, an IntelliJ IDEA plugin equipped with a suite of developer productivity tools.

The platform comes with interconnected out-of-the-box add-ons for report generation, BPM, maps, instant web app generation from a DB, and quite a bit more:

>> Become an efficient full-stack developer with Jmix

Course – RWSB – NPI (cat=REST/Spring Boot)
announcement - icon

Now that the new version of REST With Spring - “REST With Spring Boot” is finally out, the current price will be available until the 22nd of June, after which it will permanently increase by 50$

>> GET ACCESS NOW

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 – RWSB – NPI (cat=REST/Spring Boot)
announcement - icon

Now that the new version of REST With Spring - “REST With Spring Boot” is finally out, the current price will be available until the 22nd of June, after which it will permanently increase by 50$

>> GET ACCESS NOW

Course – LSD (cat=Persistence)
announcement - icon

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

>> CHECK OUT THE COURSE

res – Persistence (eBook) (cat=Persistence)