commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Keith Chiu" <kc...@extracomp.com>
Subject [DBCP] Help on gathering pool information
Date Fri, 26 Sep 2003 20:52:32 GMT
Hi,
   I am using common dbcp 1.0
   I followed the ManualPoolingDriverExample.java to implement the
connection pooling in the project.

  Source code: Basically there are 2 classes


  1) DatabaseUtil.java (for setting up connection pool by passing the
poolName. The project that I did have multiple modules, each module has its
own pool, that's why I using the poolName as an identifier)
  2) SomeClass.java (for call the databaseUtil and using the connection)

  Here are the Question

  1) Is there any way I can gather the information for the exiting
connections in the the pool (e.g. connection has been used, remaining
connections....)? I would like to get it from the SomeClass.
  2) Design issue: Did I implement right. In the databaseUtil, do I need to
make Pooldriver as static so that every modules can share the same driver??


// SomeClass
public class SomeClass {
  //....
  public boolean init() {
    if (DatabaseUtil.setupPool(name, driver, url, maxsize)){
      return (true);
    }
    return(false);
  }
  public void getSomething() {
    java.sql.Connection con = null;
    try {
      con = java.sql.DriverManager.getConnection(DBPoolURL);
      // database connection operation
      // e.g. update, add, delete...
      }
    }catch (java.sql.SQLException ex) {
    }finally {
      try { if (con!=null) con.close(); } catch (java.sql.SQLException ex)
{logger.error(ex);}
    }
  }
}

// DatabaseUtil
public class DatabaseUtil {
  private static Set set = new HashSet();
  public DatabaseUtil() {
  }

  public static boolean setupPool(String poolName, String jdbcDriver, String
connectURI, String maxPoolSizeStr) {
    try{
      return (setupPool(poolName, jdbcDriver, connectURI,
Integer.parseInt(maxPoolSizeStr)));
    }
    catch(Exception ex){
      return(false);
    }
  }
  public static boolean setupPool(String poolName, String jdbcDriver, String
connectURI, int maxPoolSize) {
    if (set.contains(poolName)){
      return (true);
    }
    try{
      Class.forName(jdbcDriver);
    }
    catch(Exception ex){
      return(false);
    }
    ObjectPool connectionPool = new GenericObjectPool(null, maxPoolSize);
    ConnectionFactory connectionFactory = new
DriverManagerConnectionFactory(connectURI,null);
    try{
      PoolableConnectionFactory poolableConnectionFactory = new
PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,t
rue);
    }
    catch (Exception ex){
      logger.error("Exception poolableConnectionFactory", ex);
      return(false);
    }
    PoolingDriver driver = new PoolingDriver();
    driver.registerPool(poolName, connectionPool);
    set.add(poolName);
    logger.debug("PoolName: " + poolName + " added into the pool");
    return (true);
  }
} // class DatabaseUtil


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message