Source Code : Connection Pool Basics

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Connection Pool Basics

import java.sql.Connection;
import java.util.Properties;
import java.sql.PreparedStatement;

import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.dbcp.DriverConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;

import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.KeyedObjectPoolFactory;
import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;

public class ConnectionPoolBasics {

  public static void main(String args[]) throws Exception {

    GenericObjectPool gPool = new GenericObjectPool();

    /*Class.forName("com.mysql.jdbc.Driver");

    DriverManagerConnectionFactory cf =
      new DriverManagerConnectionFactory(
        "jdbc:mysql://localhost/commons", "root", "");*/

    Properties props = new Properties();
    props.setProperty("Username", "root");
    props.setProperty("Password", "");
    ConnectionFactory cf =
      new DriverConnectionFactory(new com.mysql.jdbc.Driver(),
                                  "jdbc:mysql://localhost/commons",
                    props);


    KeyedObjectPoolFactory kopf =new GenericKeyedObjectPoolFactory(null, 8);

    PoolableConnectionFactory pcf =  new PoolableConnectionFactory(cf,
                                    gPool,
                                    kopf,
                                    null,
                                    false,
                                    true);


    for(int i = 0; i < 5; i++) {
      gPool.addObject();
    }

    // PoolingDataSource pds = new PoolingDataSource(gPool);
    PoolingDriver pd = new PoolingDriver();
    pd.registerPool("example", gPool);

    for(int i = 0; i < 5; i++) {
      gPool.addObject();
    }

    Connection conn = java.sql.DriverManager.getConnection("jdbc:apache:commons:dbcp:example");

    System.err.println("Connection: " + conn ); //": Delegate: " + ((org.apache.commons.dbcp.PoolingConnection)conn).getDelegate());

    // do some work with the connection
    PreparedStatement ps = conn.prepareStatement("Select * from customer where id = ?");

    System.err.println("Active: " + gPool.getNumActive() + ", Idle: " + gPool.getNumIdle());

    conn.close();

    System.err.println("Active: " + gPool.getNumActive() + ", Idle: " + gPool.getNumIdle());

  }
}

           
       

Thank with us