Module 16: Database Integration and Persistence
Connection Pooling
See connection pools as a throughput and stability tool, not just a performance trick. This lesson explains what the pool manages, why leaks and long transactions starve the system, and which configuration questions actually matter in production.
Author
Java Learner Editorial Team
Reviewer
Technical review by Java Learner
Last reviewed
2026-04-17
Java version
Java 25 LTS
Learning goals
- Explain why opening a new connection per operation does not scale well
- Recognize how leaks, long transactions, and slow queries affect pool health
- Choose pool settings with system behavior in mind instead of guessing
Before you start
- You are comfortable with classes, exceptions, and writing small multi-class Java programs
Lesson roadmap: Start with the mental model, then follow the design choices, common pitfalls, and the practical workflow you should apply in a real project.
Opening a database connection is expensive compared with reusing one: Production systems therefore keep a managed pool of ready-to-use connections.
A pool hands out a connection, then takes it back when you close it: Closing a pooled connection usually returns it to the pool instead of destroying it.
Connection leaks are dangerous: If code forgets to close connections, the pool can run dry and the app starts failing under load.
Design takeaway: Always treat connection closure as non-optional, even when a framework manages most of the setup.
What the pool really does: A connection pool amortizes the cost of creating database connections and helps the application limit how many concurrent conversations it opens against the database.
Pool exhaustion is a symptom: When the pool runs out, the real cause is often leaked connections, slow queries, oversized transactions, or traffic spikes that the rest of the system cannot handle cleanly.
Configuration questions that matter: How many concurrent requests can the app realistically process, how long should callers wait for a connection, and how will you notice leaks before users notice failures?
Operational signal: Watch connection acquisition time, active vs idle counts, and transaction duration. Those metrics tell you much more than “the pool size is 20.”
How to study this module: Keep a simple domain in mind, such as users, orders, or tasks. It is easier to understand JDBC, transactions, and ORM mapping when every SQL statement serves a clear business action.
Code review mindset: Database code is not only about “does it work.” Reviewers also look for resource safety, transaction boundaries, predictable query counts, and whether the persistence layer leaks too much SQL detail into higher layers.
Production habit: Measure query behavior early. Slow queries, long transactions, and leaked connections usually hurt real systems more than syntax mistakes do.
Runnable examples
Closing still matters with a pool
try (Connection connection = dataSource.getConnection()) {
// use the connection
}
// closing returns it to the poolExpected output
The connection goes back to the pool instead of leaking.
Pool usage belongs in a small, resource-safe block
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT 1")) {
statement.execute();
}Expected output
The connection is borrowed briefly and returned to the pool quickly.
Common mistakes
Increasing pool size first when the real issue is slow SQL or long transactions
Tune the query and transaction behavior before assuming more connections will solve the problem.
Holding a pooled connection while doing unrelated CPU or remote API work
Acquire the connection as late as possible and release it as early as possible.
Mini exercise
Imagine your app handles a burst of traffic and the pool becomes exhausted. List three causes that are more likely than “Java is too slow,” and explain how each one would be investigated.
Summary
- Pooling reduces repeated connection setup cost.
- Closing pooled connections is still required.
- Leaked connections can break the app under load.
- Pool health depends on fast resource return, not just on the configured max size.
- Slow queries and long transactions often look like pool problems from the outside.
Next step
Now group multiple SQL operations into real transactions.
Sources used