Monday, March 19, 2012

User Defined "Connection Pool Provider" in Java

Dear reader,
Writing a blog about:
1) Creating user defined Connection Pool in Java.
2) Creating Object Pool provider.

Application wise both are same. I have created a pool of Database Connection objects
which can be considered as Object Pool. Such APIs are used in Connection Pooling, EJBs (where
session beans are shared, kept in pool and assigned to client. Once processing is done Bean is
again kept in pool). Also initialization of Connection Pool with total no of initial connections
should be done in synchronous ways. Please see below the complete program:


============ObjectPool.java START==================

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class ObjectPool {

    private static Map<Connection, Boolean> poolMap=new HashMap<Connection, Boolean>();
    private Boolean connUsed=false;
    private Set<Connection> set=null;
    private Iterator<Connection> iter;
    private static final int poolSize=10;   //Making final means no more than 10 connections allowed
    Connection[] dbConn;

    public void initializePool(){
        if(poolMap!=null && poolMap.isEmpty()){
            try {
                dbConn=new Connection[poolSize]; 
                Class.forName("oracle.jdbc.OracleDriver");
                String connQuery = "jdbc:oracle:thin:@10.200.101.45:1521:obdbdev";  //obdbdev is the schema.

                //Initializing of connections should be done by only one thread..
                synchronized(this) {
                    for(int i=0;i<poolSize;i++) {
                        dbConn[i] = DriverManager.getConnection(connQuery,"nktst21","nktst21");
                        poolMap.put(dbConn[i], connUsed);
                    }
                }
                System.out.println("DB connection successful: Total connections available: "+dbConn.length);
                //System.out.println("Map contents: "+poolMap);
            }
            catch(Exception e){
                e.printStackTrace();
            }
            set=poolMap.keySet();
        }
        else
            System.out.println("Object Pool Map is already loaded with connections");
    }
    public Object getObjectFromPool(){
        Connection con=null;
        if(set!=null && !set.isEmpty()) {
            iter=set.iterator();
            while(iter.hasNext()) {
                con=(Connection)(iter.next());
                if(poolMap.get(con)==connUsed)  { //if false, means connection is not used till now
                    System.out.println("This connection is unused, returning to client: "+con);
                    poolMap.put(con,true);  //Now making this connection as used.
                    return con;
                }
            }
        }
        return con;
    }
    public void putObjectBackToPool(Object obj){
        Connection con=(Connection)obj;
        if(poolMap.get(con)==true)  { //if true, means connection is is use till now
            System.out.println("Marking this connection as ready to use again: "+con);
            poolMap.put(con, false);  //Now making this connection as used.
        }
        else
            System.out.println("Nothing is done for this connection");

    }
    public static void main(String[] args) {
        ObjectPool poolObject=new ObjectPool();
        poolObject.initializePool();

        System.out.println("Total unused connections: "+poolObject.getSizeOfUnusedConnections());
        Object conn=poolObject.getObjectFromPool();
        System.out.println("Got connection Object: "+conn);

        Object conn2=poolObject.getObjectFromPool();
        System.out.println("Got again connection Object: "+conn2);
        System.out.println("Total unused connections: "+poolObject.getSizeOfUnusedConnections());

        poolObject.putObjectBackToPool(conn2);
        System.out.println("After putting object back to Pool..");
        System.out.println("Total unused connections: "+poolObject.getSizeOfUnusedConnections());
    }
    
    public int getSizeOfUnusedConnections(){
        int unusedConnections=0;
        Connection con=null;
        if(set!=null && !set.isEmpty()) {
            iter=set.iterator();
            while(iter.hasNext()) {
                con=(Connection)(iter.next());
                if(poolMap.get(con)==false)  { //if false, means connection is not used till now
                    unusedConnections++;
                }
            }
        }
        return unusedConnections;
    }
}
//Output:
DB connection successful: Total connections available: 10
Total unused connections: 10
This connection is unused, returning to client: oracle.jdbc.driver.T4CConnection@199176
Got connection Object: oracle.jdbc.driver.T4CConnection@199176
This connection is unused, returning to client: oracle.jdbc.driver.T4CConnection@1c5dbb
Got again connection Object: oracle.jdbc.driver.T4CConnection@1c5dbb
Total unused connections: 8
Marking this connection as ready to use again: oracle.jdbc.driver.T4CConnection@1c5dbb
After putting object back to Pool..
Total unused connections: 9

============ObjectPool.java END==================

No comments:

Post a Comment