HikariCP là JDBC connection pool library — quản lý pool kết nối DB thay vì mở/đóng connection mỗi request.
Tại sao HikariCP là default (Spring Boot 2+):
- Nhanh nhất trong benchmark (TomcatCP, DBCP2, C3P0) — minimal overhead per borrow/return.
- Lightweight (~170KB JAR).
- Reliable — được test kỹ, ít bug.
spring:
datasource:
url: jdbc:postgresql://localhost/mydb
username: postgres
password: secret
hikari:
pool-name: MyPool
maximum-pool-size: 20 # max connections
minimum-idle: 5 # min idle connections
connection-timeout: 30000 # ms chờ lấy connection
idle-timeout: 600000 # ms trước khi idle connection bị đóng
max-lifetime: 1800000 # ms tối đa connection sống
leak-detection-threshold: 60000 # ms — cảnh báo nếu connection bị giữ quá lâuPool size best practice:
- Không phải càng nhiều càng tốt — DB có giới hạn concurrent connection.
- Formula: pool_size = (core_count * 2) + effective_spindle_count (HikariCP docs).
- Postgres recommend: 10-20 connections per app instance.
Leak detection: set leakDetectionThreshold để phát hiện connection không được trả về pool (code bug).
HikariCP is a JDBC connection pool library — manages a pool of DB connections instead of opening/closing one per request.
Why HikariCP is the default (Spring Boot 2+):
- Fastest in benchmarks (vs TomcatCP, DBCP2, C3P0) — minimal overhead per borrow/return.
- Lightweight (~170 KB JAR).
- Reliable — well-tested, few bugs.
spring:
datasource:
url: jdbc:postgresql://localhost/mydb
username: postgres
password: secret
hikari:
pool-name: MyPool
maximum-pool-size: 20 # max connections
minimum-idle: 5 # min idle connections
connection-timeout: 30000 # ms to wait for a connection
idle-timeout: 600000 # ms before an idle connection is closed
max-lifetime: 1800000 # max ms a connection stays alive
leak-detection-threshold: 60000 # ms — warn if connection held too longPool size best practice:
- More is not always better — the DB has a limited concurrent connection count.
- Formula: pool_size = (core_count * 2) + effective_spindle_count (HikariCP docs).
- Postgres recommends: 10-20 connections per app instance.
Leak detection: set leakDetectionThreshold to detect connections not returned to the pool (code bugs).