Explain connection pooling in Java

๐Ÿ’ก Concept: Connection Pooling

Connection pooling is a technique used to reuse and manage database connections efficiently instead of creating a new connection for every request.

๐Ÿ“˜ Quick Intro

Establishing database connections is expensive. Connection pooling helps by keeping a pool of open connections and reusing them instead of opening new ones repeatedly.

๐Ÿง  Analogy

Think of connection pooling like a cab stand. Instead of calling a new cab every time, you just take the next available one. Once you're done, the cab returns to the stand for others to use.

๐Ÿ”ง Technical Explanation

  • A pool is created with a fixed number of database connections.
  • When an app requests a connection, it is assigned from the pool.
  • After use, the connection is returned to the pool rather than being closed.
  • Reduces connection creation overhead and improves performance.
  • Popular libraries: HikariCP, Apache DBCP, C3P0.

๐ŸŽฏ Use Cases

  • โœ… Web applications with high user traffic
  • โœ… APIs making frequent database calls
  • โœ… Enterprise Java apps needing scalable database access

๐Ÿ’ป Example: HikariCP Connection Pool


import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;

public class HikariExample {
    public static void main(String[] args) throws Exception {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3306/testdb");
        config.setUsername("root");
        config.setPassword("root123");

        HikariDataSource ds = new HikariDataSource(config);
        try (Connection conn = ds.getConnection()) {
            System.out.println("Connected using pool!");
        }
    }
}

โ“ Interview Q&A

Q1: What is connection pooling?
A: Reusing a pool of database connections to reduce connection overhead.

Q2: Why is it important?
A: It improves performance and scalability by reducing connection creation time.

Q3: What happens when the pool is exhausted?
A: Requests wait or fail, depending on configuration.

Q4: Can pooled connections be closed?
A: They are returned to the pool, not actually closed.

Q5: Name some pooling libraries.
A: HikariCP, Apache DBCP, C3P0.

Q6: Is pooling suitable for multi-threaded apps?
A: Yes, most pooling libraries are thread-safe.

Q7: Can JDBC work without pooling?
A: Yes, but it's inefficient.

Q8: What config is typically set in pool?
A: Max pool size, timeout, idle time.

Q9: Is pooling done at JVM level?
A: No, by libraries or containers.

Q10: What is the default size of HikariCP pool?
A: 10 connections.

๐Ÿ“ MCQs

Q1. What is connection pooling?

  • Creating new connections
  • Reusing database connections
  • Batching SQL
  • Caching queries

Q2. Which library is known for pooling?

  • Log4j
  • Hibernate
  • HikariCP
  • Tomcat

Q3. Is pooling faster than creating new connections?

  • No
  • Yes
  • Equal
  • Depends on DB

Q4. What happens when all pool connections are used?

  • New pool is created
  • App restarts
  • Requests wait or fail
  • Nothing

Q5. Which config limits pool size?

  • Idle timeout
  • Query limit
  • Maximum pool size
  • Cache size

Q6. Do you need to close pooled connections?

  • No
  • Yes
  • They are returned to pool
  • They expire automatically

Q7. What does HikariCP stand for?

  • Java Config Pool
  • High-performance JDBC pool
  • Lightweight Connection Provider
  • None

Q8. Which Java interface is used to acquire connections?

  • Connector
  • Driver
  • DataSource
  • ResultSet

Q9. Is connection pooling part of JDBC spec?

  • Yes
  • No
  • Partially
  • Depends on DB

Q10. When was HikariCP made default in Spring Boot?

  • 1.0
  • 1.5
  • 2.0
  • 3.0

๐Ÿ’ก Bonus Insight

Modern frameworks like Spring Boot use HikariCP as the default connection pool because of its speed and low latency under load.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: