Spring Boot
HikariCP
application.properties
configuration
database connection pool

How do I configure HikariCP in my Spring Boot app in my application.properties files?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Configuring a connection pool is essential for efficiently managing database connections in your Spring Boot application, especially in a production environment. One of the most widely used connection pool libraries is HikariCP. Known for its performance and simplicity, HikariCP can handle a high number of database connections with ease. This article will guide you through the process of configuring HikariCP in your Spring Boot application using the application.properties file.

Understanding HikariCP

HikariCP is a solid and fast connection pool library for Java applications. It's lightweight but offers a variety of features to enhance database connectivity, such as:

  • Minimal Footprint: HikariCP uses fewer resources and is lightweight.
  • Performance: It ranks as one of the top-performing connection pools in several benchmarking studies.
  • Robust Configuration Options: It provides several configurations to optimize database connections.
  • Flexibility: Easily configurable within your Spring Boot application via an application.properties file.

Configuring HikariCP in Spring Boot

Step 1: Add Dependencies

Spring Boot Starter JDBC or Spring Boot Starter Data JPA will automatically include HikariCP as the default connection pool. However, you should verify this in your pom.xml (for Maven) or build.gradle (for Gradle).

For Maven, ensure you have:

xml
1<dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-data-jpa</artifactId>
4</dependency>

For Gradle:

groovy
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

Step 2: Configure application.properties

The application.properties file is where you’ll define specific HikariCP settings. Below is a configuration example:

properties
1# Database Configuration
2spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
3spring.datasource.username=myuser
4spring.datasource.password=mypassword
5
6# HikariCP Configuration
7spring.datasource.hikari.connection-timeout=20000
8spring.datasource.hikari.maximum-pool-size=10
9spring.datasource.hikari.minimum-idle=5
10spring.datasource.hikari.idle-timeout=300000
11spring.datasource.hikari.max-lifetime=1800000
12spring.datasource.hikari.pool-name=MyPool
13
14# Show SQL Queries
15spring.jpa.show-sql=true
16spring.jpa.properties.hibernate.format_sql=true

Detailed Configuration Options

  1. spring.datasource.url: The database URL to connect to.
  2. spring.datasource.username & spring.datasource.password: Credentials for accessing the database.
  3. HikariCP Properties:
    • spring.datasource.hikari.connection-timeout: Max time to wait for a connection from the pool (milliseconds).
    • spring.datasource.hikari.maximum-pool-size: The maximum number of connections in the pool.
    • spring.datasource.hikari.minimum-idle: Minimum number of idle connections in the pool.
    • spring.datasource.hikari.idle-timeout: Maximum time that a connection is allowed to sit idle in the pool (milliseconds).
    • spring.datasource.hikari.max-lifetime: Maximum lifetime of a connection in the pool (milliseconds).
    • spring.datasource.hikari.pool-name: Name of the connection pool to identify in logs and metrics.

Optional Properties

Configure additional properties to fine-tune performance:

  • spring.datasource.hikari.leak-detection-threshold: Time in milliseconds to indicate a potential connection leak.

Monitoring HikariCP

You can monitor HikariCP metrics using Spring Boot Actuator. Ensure to include the actuator dependency in your pom.xml or build.gradle and enable the appropriate endpoint:

xml
1<dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-actuator</artifactId>
4</dependency>

Within application.properties:

properties
management.endpoints.web.exposure.include=health,metrics

This will enable access to endpoints where you can check the health of your HikariCP connection pool, among other metrics.

Summary Table

Here's a summary of key properties and their explanations:

PropertyDescriptionExample Value
spring.datasource.urlDatabase URLjdbc:mysql://localhost:3306/mydatabase
spring.datasource.usernameDatabase usernamemyuser
spring.datasource.passwordDatabase passwordmypassword
spring.datasource.hikari.connection-timeoutMaximum time to wait for a connection from the pool (milliseconds)20000
spring.datasource.hikari.maximum-pool-sizeMaximum number of connections in the pool10
spring.datasource.hikari.minimum-idleMinimum number of idle connections in the pool5
spring.datasource.hikari.idle-timeoutMaximum idle time for a connection (milliseconds)300000
spring.datasource.hikari.max-lifetimeMaximum lifetime of a connection (milliseconds)1800000
spring.datasource.hikari.pool-nameName of the connection poolMyPool
spring.datasource.hikari.leak-detection-thresholdThreshold for connection leak detection (milliseconds)60000

Conclusion

Configuring HikariCP in a Spring Boot application using the application.properties file is a straightforward process. With the right configurations, you can significantly improve the performance and reliability of database operations in your application. By understanding and utilizing the rich set of configuration options HikariCP provides, you can ensure resource efficiency and optimize database connectivity.


Course illustration
Course illustration

All Rights Reserved.