JDBC Connection Pooling
Connection pooling maintains a cache of reusable database connections, dramatically improving performance for web applications that handle many concurrent users.
Why Connection Pooling?
Creating a new TCP connection and authenticating with the database takes 50-200ms. Pools keep connections open and lend them to threads, reducing latency to microseconds.
HikariCP Configuration
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:postgresql://localhost:5432/appdb");
config.setUsername("app");
config.setPassword(System.getenv("DB_PASS"));
config.setMaximumPoolSize(20);
config.setMinimumIdle(5);
config.setConnectionTimeout(30000);
HikariDataSource ds = new HikariDataSource(config);
try (Connection conn = ds.getConnection()) { /* use connection */ }Pool Sizing Guidelines
- Start with pool size = number of CPU cores × 2 for typical web apps.
- Monitor active/idle connections under load.
- Set connection max lifetime below database timeout.
- Use validation query: SELECT 1 for connection health checks.
Frequently Asked Questions
HikariCP vs Tomcat Pool vs C3P0?▼
HikariCP is the default in Spring Boot 2+ and is widely considered the fastest and most reliable. Use it for new projects.
