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.propertiesfile.
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:
For Gradle:
Step 2: Configure application.properties
The application.properties file is where you’ll define specific HikariCP settings. Below is a configuration example:
Detailed Configuration Options
spring.datasource.url: The database URL to connect to.spring.datasource.username&spring.datasource.password: Credentials for accessing the database.- 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:
Within application.properties:
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:
| Property | Description | Example Value |
spring.datasource.url | Database URL | jdbc:mysql://localhost:3306/mydatabase |
spring.datasource.username | Database username | myuser |
spring.datasource.password | Database password | mypassword |
spring.datasource.hikari.connection-timeout | Maximum time to wait for a connection from the pool (milliseconds) | 20000 |
spring.datasource.hikari.maximum-pool-size | Maximum number of connections in the pool | 10 |
spring.datasource.hikari.minimum-idle | Minimum number of idle connections in the pool | 5 |
spring.datasource.hikari.idle-timeout | Maximum idle time for a connection (milliseconds) | 300000 |
spring.datasource.hikari.max-lifetime | Maximum lifetime of a connection (milliseconds) | 1800000 |
spring.datasource.hikari.pool-name | Name of the connection pool | MyPool |
spring.datasource.hikari.leak-detection-threshold | Threshold 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.

