Sunday, 12 April 2015

How to write custom JDBC Connection Pool in java

Here have another interview programming solved question that give you pretty good idea to solve such kind of cases. below section we created a Connection pool that hold 10 connection instance to give the connection object to different different context. Typically, pool is basically a set of data that belong to same type like Connection Pool,String pool. effectively it increase the performance of system.

Interview question to write a custom Connection pool to prevent costly creation of connection object every time.


As below example i will demonstrate how we can achieve it in java language...

Create a configuration java file that contains all configuration related setup of Database..


package com.connection.pool;

public class Configuration {
    public String DB_USER_NAME;
    public String DB_PASSWORD;
    public String DB_URL;
    public String DB_DRIVER;
    public int DB_MAX_CONNECTIONS;

    private Configuration(){
        init();
    }
   
    private static Configuration config = new Configuration();
    public static  Configuration getInstance(){
        return config;
    }
   
    private void init() {
          DB_USER_NAME = "root";
          DB_PASSWORD = "root";
          DB_URL = "jdbc:mysql://localhost:3306/test";
          DB_DRIVER = "com.mysql.jdbc.Driver";
          DB_MAX_CONNECTIONS = 5; // Maximun nos of Connection in Pool
         }
}


Create a ConnectionPool java file contains all logic to create a connection...

package com.connection.pool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class JdbcConnectionPool {

    private List<Connection> connectionPool = new ArrayList<Connection>();
    private Configuration config = Configuration.getInstance();

    public JdbcConnectionPool(){
        try {
            initilizeConnectionPool();
            System.out.println(" Default Pool Size is "+connectionPool.size());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    private void initilizeConnectionPool() throws ClassNotFoundException, SQLException {
        while(!isConnectionPoolFull()){
            connectionPool.add(getNewConnection());
        }
    }

    private synchronized boolean isConnectionPoolFull() {
        if (connectionPool.size() > config.DB_MAX_CONNECTIONS) {
            return true;
        }
        return false;
    }
   
    public synchronized Connection getConnectionPool(){
        Connection connection = null;   
        if(connectionPool.size() >0 ){
            connection =  connectionPool.get(0);
            connectionPool.remove(0);
        }
        return connection;
    }

    private Connection getNewConnection() throws SQLException,
            ClassNotFoundException {
        Class.forName(config.DB_DRIVER);
        Connection connection = DriverManager.getConnection(config.DB_URL,
                config.DB_USER_NAME, config.DB_PASSWORD);
        return connection;
    }
}
 
End here...




No comments:

Post a Comment