commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Green, Kay" <>
Subject How to tell how TestOnBorrow is set
Date Fri, 01 May 2009 15:23:40 GMT
I am trying tell what testOnBorrow is set to for my GenericObjectPool.


Here is how I am constructing my GenericObjectPool


ObjectPool connectionPool = 

                new GenericObjectPool(


                8, // maxActive

                GenericObjectPool.WHEN_EXHAUSTED_GROW, //

                GenericObjectPool.DEFAULT_MAX_WAIT, //maxWait

                true, //testOnBorrow

                true); // testOnReturn


but ObjectPool does not have getTestOnBorrow.


I am creating ObjectPool object because PoolingDataSource needs
ObjectPool in its connection


Any help would be appreciated on how to tell what the parameters are set


What I am trying to do is create a database connection pool.


Here is the entire code


import javax.sql.DataSource;

import java.sql.Connection;

import java.sql.Statement;

import java.sql.ResultSet;

import java.sql.SQLException;


import org.apache.commons.dbcp.ConnectionFactory;

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

import org.apache.commons.dbcp.PoolingDataSource;

import org.apache.commons.dbcp.PoolableConnectionFactory;

import org.apache.commons.dbcp.DriverManagerConnectionFactory;

import org.apache.commons.pool.ObjectPool;


public class ManualPoolingDataSourceExample {


    public static void main(String[] args) {


        // First we load the underlying JDBC driver.

        // You need this if you don't use the jdbc.drivers

        // system property.


        System.out.println("Loading underlying JDBC driver.");

        try {


        } catch (ClassNotFoundException e) {






        // Then, we set up the PoolingDataSource.

        // Normally this would be handled auto-magically by

        // an external configuration, but in this example we'll

        // do it manually.


        System.out.println("Setting up data source.");

        String uri =

        String sql = "select masterpatientid from masterpatient where
masterpatientid = 'KG003'";

        DataSource dataSource = setupDataSource(uri);




        // Now, we can use JDBC DataSource as we normally would.


        Connection conn = null;

        Statement stmt = null;

        ResultSet rset = null;


        try {

            System.out.println("Creating connection.");

            conn = dataSource.getConnection();

            System.out.println("Creating statement.");

            stmt = conn.createStatement();

            System.out.println("Executing statement.");

            rset = stmt.executeQuery(sql);


            int numcols = rset.getMetaData().getColumnCount();

            while( {

                for(int i=1;i<=numcols;i++) {

                    System.out.print("\t" + rset.getString(i));




        } catch(SQLException e) {


        } finally {

            try { rset.close(); } catch(Exception e) { }

            try { stmt.close(); } catch(Exception e) { }

            try { conn.close(); } catch(Exception e) { }




    public static DataSource setupDataSource(String connectURI) {


        // First, we'll need a ObjectPool that serves as the

        // actual pool of connections.


        // We'll use a GenericObjectPool instance, although

        // any ObjectPool implementation will suffice.


        // get PoolableObjectFactory

        //PoolableObjectFactory poolableObjectFactory = new

        GenericObjectPool.Config genericObjectPoolConfig = new

        genericObjectPoolConfig.testOnReturn = true;


        ObjectPool connectionPool = 

                new GenericObjectPool(


                8, // maxActive

                GenericObjectPool.WHEN_EXHAUSTED_GROW, //

                GenericObjectPool.DEFAULT_MAX_WAIT, //maxWait

                true, //testOnBorrow

                true); // testOnReturn

        // look at what testOnBorrow is

connectionPool.testOnBorrow " +


        System.out.println("DEFAUTL_MAX_WAIT " +

        System.out.println("DEFAULT_MAX_ACTIVE " +

        System.out.println("connectionPool numActive " +

        //System.out.println("connectionPool testOnBorrow " +

        // set testOnBorrow and testOnReturn



        // Next, we'll create a ConnectionFactory that the

        // pool will use to create Connections.

        // We'll use the DriverManagerConnectionFactory,

        // using the connect string passed in the command line

        // arguments.


        ConnectionFactory connectionFactory = new



        // Now we'll create the PoolableConnectionFactory, which wraps

        // the "real" Connections created by the ConnectionFactory with

        // the classes that implement the pooling functionality.


        String valQuery = "SELECT '1' from dual";

        PoolableConnectionFactory poolableConnectionFactory =

                new PoolableConnectionFactory(



                null, //KeyedObjectPoolFactory

                valQuery, //validationQuery

                false, //defaultReadOnly

                true );//defaultAutoCommit

        //set the validationQuery so that testOnBrowwow works


        // Finally, we create the PoolingDriver itself,

        // passing in the object pool we created.


        PoolingDataSource dataSource = new

        System.out.println("connectionPool numActive " +


        return dataSource;




I am basing this off the example code on


Kay Green

11000 Westmoor Circle

Westminster, CO 80021

Office: 720 239-4639

Email:  <>


McKesson Provider Technologies


"Remember Life should NOT be a journey to the grave with the intention
of arriving safely in an attractive and well preserved body, but rather
to skid in sideways, chocolate in one hand, Mojito in the other, body
thoroughly used up, totally worn out and screaming "WOO HOO what a

Confidentiality Notice: This email message, including any attachments,
is for the sole use of the intended recipient(s) and may contain
confidential and privileged information. Any unauthorized review, use,
disclosure or distribution is prohibited. If you are not the intended
recipient, please contact the sender by reply email and destroy all
copies of the original message.


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