Wednesday, September 24, 2014

Basic understanding of Connection Pool With Example

Connection Pool



Baciscally Connection Pool usage is to Avoid making and destroying the Connection object repeatedly.
Connection Pool is like keeping a door of home open instead of locking it and opening it all time whenever need to go Inside.

Connection Pool means Set of Connections which are cashed and shared.
So whenever a resource need a database communication then it will ask the connection pool for the connection and if free connection is available then it will get connection from the pool and will use it.  Once it done with the need of connection it will give the connection back to the Connection Pool, so it will be available for next time.

So here it will not require to create the connection object all the time whenever required. 
and creating and destroying connection object hits the performance of Application

Download following jar files

commons-dbcp.jar
commons-pool.jar
mysql-connector-java-version.n.n.n.jar

Example Code

package pool;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;

public class DataSourceEngine {

    private static DataSourceEngine     datasource;
    private BasicDataSource basicDataSource;

    private DataSource() throws IOException, SQLException, PropertyVetoException {
        basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        basicDataSource.setUsername("root");
        basicDataSource.setPassword("root");
        basicDataSource.setUrl("jdbc:mysql://localhost/SampleData");
       
        /* Below configurations are Optional. */
        basicDataSource.setMinIdle(1);
        basicDataSource.setMaxIdle(10);
        basicDataSource.setMaxOpenPreparedStatements(100);

    }

    public static DataSourceEngine getInstance() throws IOException, SQLException, PropertyVetoException {
        if (datasource == null) {
            datasource = new DataSourceEngine();
            return datasource;
        } else {
            return datasource;
        }
    }

    public Connection getConnection() throws SQLException {
        return this.basicDataSource.getConnection();
    }

}

===========
package pool;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DataSourceMain {

public static void main(String[] bag) {

Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        
        try {
            connection = DataSourceEngine.getInstance().getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT * FROM STUDENT");
             while (resultSet.next()) {
                 System.out.println("ROLL_NUMBER: " + resultSet.getString("ROLL_NUMBER"));
                 System.out.println("NAME: " + resultSet.getString("NAME"));
             }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
e.printStackTrace();
} catch (PropertyVetoException e) {
e.printStackTrace();
} finally {
            if (resultSet != null) {
            try {
            resultSet.close(); 
            }  catch (SQLException e) { }
            }
            if (statement != null) {
            try { 
            statement.close(); 
            } catch (SQLException e) { }
            }
            if (connection != null) {
            try { 
            connection.close(); 
            } catch (SQLException e) {}
            }
        }

}
}

Keep Visiting :)

No comments:

Post a Comment

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...