commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Trenton D. Adams" <tre...@athabascau.ca>
Subject Re: [dbcp] abandoned connection removal and logging not working
Date Fri, 25 Feb 2005 22:26:14 GMT
Joe Reger, Jr. wrote:
> Hi Trenton,
> 
> I agree with you... it's quick enough to add.  Thanks for the response.  I
> think I'm almost there.
> 
> First, where do I put the "if (config != null &&..." code to cleanup
> abandoned connection?

You misunderstood me.  That's the code I found in dbcp that did the 
work.  So, it will work if you use my code.  You just have to do 
something that will trigger that logic as indicated on the bottom of the 
configuration page. 
http://jakarta.apache.org/commons/dbcp/configuration.html

Here, I'll repost my latest code so that I know it works for you. :)

       ResourceBundle dbProperties = ResourceBundle.getBundle("server");
       removeAbandoned = Boolean.getBoolean(
           dbProperties.getString("dbcp.removeAbandoned"));
       logAbandoned = Boolean.getBoolean(
           dbProperties.getString("dbcp.logAbandoned"));
       removeAbandonedTimeout = Integer.parseInt(
           dbProperties.getString("dbcp.removeAbandonedTimeout"));
       freeConnectionWait = Integer.parseInt(
           dbProperties.getString("dbcp.freeConnectionWait"));
       exaustedAction =  dbProperties.getString("dbcp.exaustedAction");


       AbandonedConfig abandonedConfig;
       AbandonedObjectPool connectionPool;
       ConnectionFactory connectionFactory;
       PoolingDriver driver;

       // setup the abandoned configuration
       abandonedConfig = new AbandonedConfig();
       abandonedConfig.setLogAbandoned(logAbandoned);
       abandonedConfig.setRemoveAbandoned(removeAbandoned);
       abandonedConfig.setRemoveAbandonedTimeout(removeAbandonedTimeout);

       // setup the AbandonedObjectPool
       connectionPool = new AbandonedObjectPool(null, abandonedConfig);
       connectionPool.setMaxActive(maxConn);
       connectionPool.setMaxIdle(maxFree);
       connectionPool.setTestWhileIdle(true);
       if (exaustedAction.equals("WHEN_EXAUSTED_BLOCK"))
       {
         connectionPool.setWhenExhaustedAction(
             GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
         connectionPool.setMaxWait(freeConnectionWait);
       }
       else if (exaustedAction.equals("WHEN_EXAUSTED_GROW"))
       {
         connectionPool.setWhenExhaustedAction(
             GenericObjectPool.WHEN_EXHAUSTED_GROW);
       }
       else
       { // all other cases, including unrecognized ones
         connectionPool.setWhenExhaustedAction(
             GenericObjectPool.WHEN_EXHAUSTED_FAIL);
       }

       connectionPool.setTestOnBorrow(true);
       connectionPool.setTestOnReturn(true);

       /**
        * setup a poolable connection factory with all of the above objects
        * Take note that this object's constructor calls
        * connectionPool.setFactory(), which is what causes it to work.
        */

       new PoolableConnectionFactory(
           new DriverManagerConnectionFactory(url, user, password),
           connectionPool,null,
           "SELECT 'ping' FROM dual",false,false,
           Connection.TRANSACTION_SERIALIZABLE, null, abandonedConfig);

       Class.forName("org.apache.commons.dbcp.PoolingDriver");
       dbcpURL = "jdbc:apache:commons:dbcp:" + name;
       driver = (PoolingDriver) 
DriverManager.getDriver("jdbc:apache:commons:dbcp:");
       driver.registerPool(name, connectionPool);
     }
     catch (Exception exception)
     {
       RemoteBannerServer.log(exception, "error creating DBCP connection 
pool");
     }


Then you can get a connection like so, and print out some stats.

       connection = DriverManager.getConnection(dbcpURL);
       PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(
           "jdbc:apache:commons:dbcp:");
       ObjectPool connectionPool = driver.getConnectionPool(poolName);

       RemoteBannerServer.log(poolName + " POOL - out: " + 
connectionPool.getNumActive() +
           " - free : " + connectionPool.getNumIdle());



If you don't need the extra options above, someone named Kle Miller sent 
me the following code, which is a lot simpler...

B_DATASOURCE = new BasicDataSource();
	B_DATASOURCE.setDriverClassName(DRIVER);
	B_DATASOURCE.setUrl(uri);
	B_DATASOURCE.setUsername(user);
	B_DATASOURCE.setPassword(password);
	
	B_DATASOURCE.setRemoveAbandoned(true);
	B_DATASOURCE.setInitialSize(1);
	B_DATASOURCE.setMaxActive(20);
	B_DATASOURCE.setValidationQuery("select top 1 role_type from
roles");
	B_DATASOURCE.setTestOnBorrow(true);
	B_DATASOURCE.setTestOnReturn(true);
	B_DATASOURCE.setLogAbandoned(true);


And get a connection like so...

DBCONNECTION = B_DATASOURCE.getConnection();
System.out.println("#### num active:" +
   B_DATASOURCE.getNumActive() + ":: num idle:" +
   B_DATASOURCE.getNumIdle());


-- 
Trenton D. Adams
Web Programmer Analyst
Navy Penguins at your service!
Athabasca University
(780) 675-6195
:wq!

__ 
    This communication is intended for the use of the recipient to whom it
    is addressed, and may contain confidential, personal, and or privileged
    information. Please contact us immediately if you are not the intended
    recipient of this communication, and do not copy, distribute, or take
    action relying on it. Any communications received in error, or
    subsequent reply, should be deleted or destroyed.
---

---------------------------------------------------------------------
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