❮ Previous Next ❯

JDBC Connection Pooling

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:

ConnectionPoolDemo.java

import java.sql.*;
class ConnectionPoolDemo
{
public static void main(String[] args)
{
Class.forName("oracle.jdbc.driver.OracleDriver");
long before=System.currentTimeMillis();
for(int i=0;i<=10;i++)
{
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
con.close();
}
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();
bds.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
bds.setUsername("system");
bds.setPassword("oracle");
bds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
before=System.currentMillis();
for(int i=0;i<500;i++)
{
Connection con=bds.getConnection();
con.close();
}
after=System.currentTimeMillis();
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.

❮ Previous Next ❯


Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved