commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "John Jason Brzozowski" <jjmb...@gmail.com>
Subject Apache DBCP Question
Date Tue, 05 Aug 2008 13:04:56 GMT
Hello I am using the following code for database connection pooling using
Apache DBCP.

Here is the routine I use to setup the pool so:

public static void setupDriver(String connectURI) throws Exception {
        logger.trace("Creating connection pool using URI, " + connectURI);

        Config gkopc = new Config();
        gkopc.maxActive = 10;
        gkopc.maxIdle = 10;
        gkopc.maxWait = 2000;
        gkopc.whenExhaustedAction = 2;
        gkopc.testOnBorrow = false;
        gkopc.testOnReturn = false;
        gkopc.timeBetweenEvictionRunsMillis = 10000;
        gkopc.numTestsPerEvictionRun = 5;
        gkopc.minEvictableIdleTimeMillis = 5000;
        gkopc.testWhileIdle = true;
        gkopc.maxTotal = 10;

        logger.trace("Generic Keyed Object Pool Config = " +
gkopc.toString());

        connectionPool = new GenericObjectPool(null);

        DriverManagerConnectionFactory connFactory = new
DriverManagerConnectionFactory(mDbUrl, mDbUser, mDbPass);

        KeyedObjectPoolFactory kopf = new
GenericKeyedObjectPoolFactory(null, gkopc);

        logger.trace("Keyed Object Pool Factory = " + kopf.toString());

        PoolableConnectionFactory poolableConnectionFactory = new
PoolableConnectionFactory(connFactory,connectionPool,kopf,"select count(*)
from dual",false,true);

        logger.trace("Poolable Connection Factory = " +
poolableConnectionFactory.toString());

        Class.forName("org.apache.commons.dbcp.PoolingDriver");
        PoolingDriver driver = (PoolingDriver)
DriverManager.getDriver("jdbc:apache:commons:dbcp:");

        driver.registerPool("foo",connectionPool);

        logger.trace("Driver = " + driver.toString());

        logger.trace("Initial connection pool idle connections, " +
connectionPool.getNumIdle() + ", active connections, " +
connectionPool.getNumActive());
    }

Here is how I call it:

        try {
            logger.info("Setting up driver for, " + completeMDbUrl);
            setupDriver(completeMDbUrl);
        } catch (Exception e) {
            logger.fatal(e);
            e.printStackTrace();
        }

Here is some output from the routing itself:

TRACE [main] (?:?) - Initial connection pool idle connections, 0, active
connections, 0

Zero connections...however, once I issue the following as I would with
typical JDBC:

        try {

            logger.info("Loading MySQL driver");
            Class.forName("com.mysql.jdbc.Driver");
            logger.debug("Connecting to " + mDbUrl + " as " + mDbUser +
"/(Password not displayed)");
            mconn =
DriverManager.getConnection("jdbc:apache:commons:dbcp:foo");
            if(!mconn.isClosed()) {
                logger.trace("Connected to MySQL database");
            }
            ...
                }
            }
          } catch (Exception e) {
              logger.fatal(e);
              e.printStackTrace();
          }

When I print the number of connections active and idle I get the following:

TRACE [main] (?:?) - Connection pool idle connections, 0, active
connections, 1

The number of active connections never increases past one.  I thought the
following would cause 10 connections to be created:

        gkopc.maxActive = 10;

Any comments on what I missing would be greatly appreciated.  To provide
additional context I wish to use connection pooling when making large
quantities of database updates.  Most examples I see are for SELECTs not
INSERTs.

Thanks,

John
-- 
===================================
John Jason Brzozowski
===================================

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message