Best Practices for Configuring HikariCP in Spring Boot
This article explains why HikariCP is the default Spring Boot connection pool, details the required dependencies, walks through essential configuration parameters, shows how to use HikariTemplate, monitor pool metrics, avoid common pitfalls, and compares HikariCP with other popular pools.
Why Use HikariCP?
Official default in Spring Boot, no extra dependency needed.
Performance beats C3P0, DBCP2, Tomcat-JDBC.
Lightweight lock‑free design, stable under high concurrency.
Simple configuration with few pitfalls.
Supports monitoring and dynamic adjustments.
Dependency (Already Included)
Including JDBC, MyBatis, or JPA automatically brings in HikariCP via the spring-boot-starter-jdbc starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>Relevant Configuration (application.yml)
spring:
datasource:
url: jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
# HikariCP core settings (production best practice)
hikari:
pool-name: HikariCP-Pool
minimum-idle: 10
maximum-pool-size: 20
auto-commit: true
max-lifetime: 300000
connection-timeout: 5000
idle-timeout: 60000
connection-test-query: SELECT 1Key Parameter Details
1. maximum-pool-size
Not larger is always better.
Recommended for a single instance: 10~30.
CPU‑bound workloads: keep smaller.
IO‑bound (many queries): can increase.
Formula reference:
connections = ((cpu_cores * 2) + disk_count)2. max-lifetime
Must be less than MySQL wait_timeout (default 8 hours).
Suggested: 5–10 minutes.
Prevents the pool from holding connections that MySQL has already closed.
3. connection-timeout
Recommended 3–5 seconds to avoid indefinite blocking when acquiring a connection.
4. minimum-idle
Set equal to maximum-pool-size and disable the shrink mechanism for greater stability.
Using HikariTemplate in Spring Boot
@Service
public class UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<User> list() {
return jdbcTemplate.query(
"select * from user",
new BeanPropertyRowMapper<>(User.class)
);
}
}MyBatis / MyBatis‑Plus also automatically use HikariCP without any changes.
Viewing Hikari Monitoring Information
@Component
public class HikariMonitor {
@Autowired
private DataSource dataSource;
public void showPoolState() {
HikariDataSource hikari = (HikariDataSource) dataSource;
HikariPoolMXBean poolBean = hikari.getHikariPoolMXBean();
System.out.println("Active connections: " + poolBean.getActiveConnections());
System.out.println("Idle connections: " + poolBean.getIdleConnections());
System.out.println("Pending threads: " + poolBean.getPendingThreads());
System.out.println("Total connections: " + poolBean.getTotalConnections());
}
}This is very useful for high‑concurrency troubleshooting.
Precautions
1. Connection Timeout Issues
Too small max pool size.
Slow SQL holding connections.
Long‑running transactions.
2. MySQL 8.0 Connection Drops
Add the following parameters to the JDBC URL:
&allowPublicKeyRetrieval=true&useSSL=false3. Connection Leak
Uncommitted/rolled‑back transactions.
Code not releasing resources.
Enable leak-detection-threshold monitoring.
4. Performance Not Improving Under High Load
Increase maximum-pool-size.
Check for slow SQL.
Verify proper indexing.
Comparison with Other Connection Pools
HikariCP: fast, stable, default choice.
Druid: feature‑rich, full monitoring, heavier.
DBCP2: used in legacy projects, average performance.
C3P0: almost obsolete.
Conclusion
HikariCP is the Spring Boot‑endorsed strongest connection pool. Grasp three configuration points:
Reasonable maximum pool size
max-lifetime smaller than the database timeout
Appropriate timeout values
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Tech Workshop
Focused on Java backend technologies, sharing fundamentals, multithreading, JVM, the Spring ecosystem, microservices, distributed systems, high concurrency, source‑code analysis, and practical experience. Continuously delivers high‑quality original content, interview guides, and learning roadmaps to help Java developers progress from beginner to advanced, enhancing technical skills and core competitiveness.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
