Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 5 of 10 · Java Database Connectivity (JDBC)
Interview question

What is connection pooling and why is it important in JDBC applications? Connection pooling क्या है और JDBC applications में यह क्यों important है?

Answer

Connection pooling maintains a reusable pool of database connections instead of opening and closing a new one for every request - creating a connection is expensive (TCP handshake, authentication), so reuse dramatically improves performance.

Without poolingWith pooling
New connection per request - slow, resource-heavyBorrow existing connection from pool - fast
Risk of exhausting database connection limits under loadPool size is bounded and controlled
Connection setup overhead on every single querySetup cost paid once, connections reused
// Using HikariCP - the most popular JDBC connection pool library
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

HikariConfig config = new HikariConfig();
config.setJdbcUrl('jdbc:mysql://localhost:3306/mydb');
config.setUsername('root');
config.setPassword('password');
config.setMaximumPoolSize(10);      // max connections held in the pool
config.setMinimumIdle(2);           // minimum idle connections kept ready
config.setConnectionTimeout(30000); // max wait time for a free connection (ms)

HikariDataSource dataSource = new HikariDataSource(config);

// Using a connection FROM the pool
try (Connection conn = dataSource.getConnection();
     PreparedStatement pstmt = conn.prepareStatement('SELECT * FROM users')) {
    ResultSet rs = pstmt.executeQuery();
    while (rs.next()) {
        System.out.println(rs.getString('name'));
    }
}
// conn.close() here does NOT actually close the connection -
// it RETURNS it to the pool for reuse, which is much cheaper than
// tearing down and re-establishing a fresh database connection

// Other popular pooling libraries: Apache DBCP, C3P0
// Spring Boot applications commonly configure HikariCP as the default

Connection pooling हर request के लिए नया connection खोलने की बजाय reusable connections का pool रखता है - connection बनाना expensive है (TCP handshake, authentication), इसलिए reuse से performance काफ़ी बेहतर होती है।

Pooling के बिनाPooling के साथ
हर request पर नया connection - धीमाPool से existing connection - तेज़
Database limits खत्म होने का खतराPool size bounded और controlled
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

HikariConfig config = new HikariConfig();
config.setJdbcUrl('jdbc:mysql://localhost:3306/mydb');
config.setUsername('root');
config.setPassword('password');
config.setMaximumPoolSize(10);
config.setMinimumIdle(2);
config.setConnectionTimeout(30000);

HikariDataSource dataSource = new HikariDataSource(config);

try (Connection conn = dataSource.getConnection();
     PreparedStatement pstmt = conn.prepareStatement('SELECT * FROM users')) {
    ResultSet rs = pstmt.executeQuery();
    while (rs.next()) {
        System.out.println(rs.getString('name'));
    }
}
// conn.close() असल में connection close नहीं करता,
// pool को वापस return करता है reuse के लिए

Was this answer clear?