Lesson 40: Connection Pooling

10 Aug 2026 1 min Swarnil Singhai

Opening a DB connection is expensive — pooling keeps a set of ready connections alive and reused across requests instead of opening one per request.

Real-time example

A web app handling 1000 requests/sec would collapse opening a fresh DB connection each time; a pool of 20 reused connections handles it fine.

// Conceptual - real code uses HikariCP:
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:postgresql://localhost/shop");
config.setMaximumPoolSize(20);
HikariDataSource ds = new HikariDataSource(config);

try (Connection conn = ds.getConnection()) {
    // borrowed from the pool, returned automatically on close()
}

What's happening

close() on a pooled connection doesn't actually close the TCP connection — it returns the connection to the pool for reuse.

Pool size should roughly match your DB's capacity, not your app's thread count — too large a pool can overwhelm the database.

Advertisement