commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "EShin Yen" <eshin_...@hotmail.com>
Subject [pool] - maintain the some idle projects "forever" without shrinking :
Date Fri, 09 Aug 2002 22:51:00 GMT

Hi !
     I am using the GenericObjctPool (in commons-pool project) to pool the 
Database Connections. The JDBC driver we are using talks to a middle layer 
server which communicate with Ingres. The connection will object will be 
disconnected every 30 minutes (can be set up but we don't want to set it up 
to be infinite time).  My PoolableObjectFactory implementation is :

/*
* DBConnectionFactory.java
*
* Created on July 29, 2002, 5:59 PM
*/
package com.memec.util.dbconnectionpool;
/**
*
* @author  dyen
*/
import org.apache.commons.pool.PoolableObjectFactory;
/** the java database sql package for database operations */
import java.sql.*;
public class DBConnectionFactory
implements PoolableObjectFactory {

    /** Creates a new instance of DBConnectionFactory */
    public DBConnectionFactory() {
    }

    public void activateObject(Object obj) throws java.lang.Exception {
    /**    Connection con=(Connection)obj;
        try
        {
            if(con.isClosed())
            {
                System.out.println("Re-initialize object to pool");
                Class.forName(this.dbDriver);
                con = 
DriverManager.getConnection(this.dbURL,this.dbUserName, this.dbPassword);
            }
            else
            {
               System.out.println("Dont need to Re-initialize object to 
pool");
            }
        }// end of try
        catch(Exception e)
        {
           Class.forName(this.dbDriver);
           con = DriverManager.getConnection(this.dbURL,this.dbUserName, 
this.dbPassword);
        } */
    }

    public void destroyObject(Object obj) throws java.lang.Exception {
     /** try
      {
        if(obj !=null)
        {
          Connection con=(Connection)obj;
          con.close();
        }
      }
      catch(Exception e)
      {
        throw e;
      } */

    }

    public Object makeObject() throws java.lang.Exception {
      /** The database connection created */
     Connection con = null;
        try{
        System.out.println("Making object to pool");
        Class.forName(this.dbDriver);
        con = DriverManager.getConnection(this.dbURL,this.dbUserName, 
this.dbPassword);
        }
        catch(Exception e)
        {
            throw e;
        }
        return (Object)con;
    }

    public void passivateObject(Object obj) throws java.lang.Exception {
    }

    public boolean validateObject(Object obj) {
      /** The database connection created */
      Connection con = null;
      con=(Connection)obj;
        try{
            if(con.isClosed())
            {
                System.out.println("connection is closed");
                return false;
            }
            else
            {
                System.out.println("connection is fine");
                return true;
            }
        }
        catch(Exception e)
        {
            System.out.println("validateObject exception : " + e);
            return false;
        }

        // Code could arrive here only if something is wrong
        // return false;
    }
    /** set the database connection URL */
    public void setDBDriver(String dbDriver)
    {
        this.dbDriver=dbDriver;
    }
    /** set the database connection URL */
    public void setDBURL(String dbURL)
    {
        this.dbURL=dbURL;
    }
    /** set the database username */
    public void setDBUserName(String dbUserName)
    {
        this.dbUserName=dbUserName;
    }
    /** set the database password */
    public void setDBPassword(String password)
    {
        this.dbPassword=password;
    }
    private String dbDriver=null;
    private String dbURL=null;
    private String dbUserName=null;
    private String dbPassword=null;

}

As we known, I need to pass the PoolableObjectFactory  into 
GenericObjectPool.

The wish : I could keep a minimum number of connection "alive" in pool "all 
the time". I always turn on the "validation".
Problem :
(1) If I turn on the Evictor, the evictor will kill all the connection after 
a period of idle time. So the pool became empty.
(2) If I turn off the evictor, a bunch of request coming in after a period 
of idle time will try to "validate" the object before return. Eventually, 
the pool will kick out conneciton one by one and start from the beginning.
(3) For a low hit web site, the scenario is really bad because the time 
between each hit may be hours. Everytime a request coming in, a new 
connection need to be created and it is not "hold" for next request.

Bottom line, is there any way to keep some number of connection "always" 
alive in the GenericObjectPool? I don't think maxIdle is for this purpose.


                                                             David.


_________________________________________________________________
Chat with friends online, try MSN Messenger: http://messenger.msn.com


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message