JDBC

jdbc5ConnectionPooling

Path
pkg11jdbc/jdbc5ConnectionPooling.java
Package
pkg11jdbc
Study order
5
Run
Single-file source launch
Command
java pkg11jdbc/jdbc5ConnectionPooling.java
Dependencies
Optional in-memory JDBC driver (H2, SQLite, or HSQLDB)

There is no in-browser runner. This is the file from the curriculum, unchanged.

pkg11jdbc/jdbc5ConnectionPooling.java
1package pkg11jdbc;2 3/*4 * jdbc5ConnectionPooling.java5 * ---------------------------6 * Connection pooling & DataSource: how production apps manage DB connections.7 *8 * DEFINITION:9 *   Opening a DB connection is expensive (TCP + auth + session setup). A10 *   connection pool keeps a set of open connections and hands them out on11 *   demand, returning them to the pool on close() instead of tearing them down.12 *13 * KEY POINTS:14 *   - Prefer javax.sql.DataSource over DriverManager in real apps.15 *   - Popular pools: HikariCP (default in Spring Boot), Apache DBCP, c3p0.16 *   - Tune: max pool size, min idle, connection timeout, max lifetime.17 *   - "close()" on a pooled connection RETURNS it to the pool (doesn't close it).18 *19 * This file is conceptual (no external pool jar in this repo). It prints the20 * canonical HikariCP setup so you can copy it into a real project.21 */22public class jdbc5ConnectionPooling {23 24    public static void main(String[] args) {25        System.out.println("Why pool? Open/close per request is slow and exhausts the DB.");26        System.out.println("A pool reuses a fixed set of warm connections.\n");27 28        System.out.println("DataSource vs DriverManager:");29        System.out.println("  DriverManager.getConnection(url)  -> new physical connection each time");30        System.out.println("  dataSource.getConnection()        -> borrow from the pool, return on close\n");31 32        System.out.println("Typical HikariCP setup (add HikariCP + driver jars):");33        System.out.println("""34              HikariConfig cfg = new HikariConfig();35              cfg.setJdbcUrl("jdbc:postgresql://localhost:5432/shop");36              cfg.setUsername("app");37              cfg.setPassword("secret");38              cfg.setMaximumPoolSize(10);        // cap concurrent connections39              cfg.setMinimumIdle(2);             // keep a few warm40              cfg.setConnectionTimeout(30_000);  // ms to wait for a free connection41              cfg.setMaxLifetime(1_800_000);     // recycle after 30 min42 43              try (HikariDataSource ds = new HikariDataSource(cfg);44                   Connection c = ds.getConnection();          // borrow45                   PreparedStatement ps = c.prepareStatement("SELECT 1")) {46                  ps.executeQuery();47              }                                                  // close() returns it to the pool""");48 49        System.out.println("\nRule of thumb: pool size ~= (core_count * 2) for CPU-bound,");50        System.out.println("higher for I/O-bound workloads. Measure, then tune.");51    }52}