Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Connection Pooling in JDBC

It is a nice concept and having advantages over a single connection. In JDBC concept, when we are opening or closing a data store session that increase load on the database, which affects the availability and performance of the application. and the another fact is opening and closing connection to data store is a resource consuming process.

So to eliminate this disadvantage, pooling mechanism is used to establish the connections before the user makes a request. Here the Connection objects created to represent the session established to the data store are pooled and recycled for further use. This is known as Connection Pooling. In connection pooling, connection is retrieved from the pool, used by an application, and then returned to the pool. The pool contains the objects of the physical connections. In connection pooling, the connection objects are reused instead of being created each time when required. Here the main advantage of the connection pooling is that it increases the performance and availability of the resource.

In connection pooling, the application makes a request to the connection pool manager for the connection. The pool manager picks one of the connection from the pool, then create a logical connection and then returns the logical connection to the application. then the application uses the logical connection to communicate with the data store through the physical connection. Once the application has completed using the connection, it closes the logical connection which releases the physical connection. Now the physical connection is placed back into the pool, so that it can be used by other users.

Here we use javax.sql.DataSource interface which contains some methods that are used to get the connection from connection pool. Let’s see a demo program:
import java.sql.*;
class ConnectionPoolDemo
    public static void main(String[] args)
        long before=System.currentTimeMillis();
        for(int i=0;i<=10;i++)
            Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
        long after=System.currentTimeMillis();
        System.out.println("Time taken to create 11 database connections without using connection pool"+(after-before)+"milli seconds");

        //getting connection using connection pool
        BasicDataSource bds=new BasicDataSource();
        for(int i=0;i<500;i++)
            Connection con=bds.getConnection();
        System.out.println("Time taken to create 500 database connections using connection pool"+(after-before)+"milli seconds");

Here the above program represents the time difference to access jdbc connections with and without connection pool. First we have created a program to find the time taken to create 11 database connections without using connection pool, and then create 500 logical connections with connection pool.

About the Author

Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc