Nearby lessons

23 of 37

JDBC - Connection Pooling

📌 What You Will Learn
  • Why creating a new connection every time is expensive
  • What connection pooling is and how it solves the problem
  • The modern DataSource approach
  • Keeping database settings in a Properties file

Connection Pooling is a fundamental part of database programming with JDBC. This lesson explains The Problem — Connections Are Expensive, What is Connection Pooling? and The Modern Way — DataSource with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

The Problem — Connections Are Expensive

Creating a database connection involves: loading the driver, opening a network channel, the database verifying the username and password, and allocating memory. Doing this for every single query is slow.

Imagine opening a new shop door every time a customer wants to enter — ridiculous. A shop has one door kept open and customers use it again and again. Connection pooling does exactly that.

What is Connection Pooling?

Connection pooling means keeping a pool (a set) of ready connections alive. When the application needs a connection, it borrows one from the pool; when finished, it returns it to the pool — it is not destroyed.

In simple words: A connection pool keeps ready connections so you borrow one, use it, and return it. The connection is never destroyed, so the next query skips all the expensive setup.

Benefits: much faster (no repeated setup), and the database has fewer connections open (no exhaustion).

Example02
JCode Cell
1Without pooling:
2 open connection -> query -> close connection -> (next time) open again...
3 
4With pooling:
5 [conn1] [conn2] [conn3] <- a pool of ready connections
6 borrow conn2 -> query -> return conn2 to pool -> borrow again quickly

The Modern Way — DataSource

The modern JDBC way is the DataSource interface (javax.sql.DataSource). A DataSource object holds the database settings and hands out connections — and it can work together with a connection pool.

Trainer's Note: Updated knowledge: in real projects you will almost never write DriverManager.getConnection(...) repeatedly. Instead, a connection pool (HikariCP, Apache DBCP, C3P0) manages connections behind a DataSource. Spring Boot even sets this up automatically. The concept — borrow, use, return — is the important thing to remember.
Example03
JCode Cell
1// Concept (using a real pool library like HikariCP)
2HikariConfig config = new HikariConfig();
3config.setJdbcUrl("jdbc:mysql://localhost:3306/company");
4config.setUsername("root");
5config.setPassword("password");
6config.setMaximumPoolSize(10); // at most 10 connections
7 
8HikariDataSource ds = new HikariDataSource(config);
9 
10Connection con = ds.getConnection(); // borrow from the pool
11// ... do your queries ...
12con.close(); // return to the pool (not destroyed!)
📝 Key Takeaways
  • Creating a connection each time is slow; pooling reuses ready connections.
  • Pool = borrow a connection, use it, return it — never destroy it.
  • DataSource is the modern replacement for DriverManager, often backed by a pool.
  • Properties file keeps DB settings outside the code; edit it without recompiling.
  • Use Properties.load() + getProperty() to read settings.
  • Keep real passwords out of public repos.

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3