JDBC Connection Pooling — Why It Matters
Fast recall
Quick revision
Core answer
Whether you understand that pooling reuses costly connections and bounds the total count to protect the database.
Opening a new connection per request or per query, which kills performance and can overwhelm the database with connections.
Mention pool sizing, the borrow/return lifecycle, the connectionTimeout, and that HikariCP is Spring Boot's default.
Interview-ready guide
Interview answer
A web request may borrow a connection only around its database transaction, then close it before calling a remote service. The underlying connection remains open in the pool for another request. If code holds it during slow non-database work, the pool can exhaust even with no leak. If the pool is made too large, the database can spend more time contending rather than doing useful work.
JDBC Connection Pooling — Why It Matters
Direct Answer
A connection pool owns reusable physical database connections and exposes logical Connection handles through a DataSource. Borrowing avoids repeated setup and limits concurrent sessions. Calling close on the logical handle normally resets/returns it. Pooling improves resource management and throughput for many workloads, but results depend on transaction length, SQL latency, database capacity, and correct sizing.
Code Example
HikariConfig cfg = new HikariConfig();
cfg.setJdbcUrl("jdbc:mysql://localhost:3306/shop");
cfg.setUsername("app");
cfg.setPassword(System.getenv("DB_PASS")); // never hard-code
cfg.setMaximumPoolSize(10); // bounded connections
cfg.setConnectionTimeout(30000); // wait up to 30s for a free one
DataSource ds = new HikariDataSource(cfg);
try (Connection con = ds.getConnection()) { // borrow from pool
// ... use it ...
} // returned to pool, not destroyed
In Spring Boot you just set spring.datasource.* and get a HikariCP pool automatically.
Common Mistakes & Pitfalls
Opening a new connection per request or per query, which kills performance and can overwhelm the database with connections.
Check your understanding
Answer these without looking back at the article.
- What is the format of a JDBC connection URL?
- How do you properly close a JDBC connection in Java?
- What information is needed to create a JDBC connection?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.