You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Green, Kay" <Ka...@McKesson.com> on 2009/05/01 17:23:40 UTC
How to tell how TestOnBorrow is set
I am trying tell what testOnBorrow is set to for my GenericObjectPool.
Here is how I am constructing my GenericObjectPool
ObjectPool connectionPool =
new GenericObjectPool(
null,
8, // maxActive
GenericObjectPool.WHEN_EXHAUSTED_GROW, //
whenExhaustedAction
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
to.
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 {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Done.");
//
// 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 =
"jdbc:oracle:thin:hcs/hcs@atldevmm10.mckesson.com:1521:l101000d";
String sql = "select masterpatientid from masterpatient where
masterpatientid = 'KG003'";
DataSource dataSource = setupDataSource(uri);
System.out.println("Done.");
//
// 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);
System.out.println("Results:");
int numcols = rset.getMetaData().getColumnCount();
while(rset.next()) {
for(int i=1;i<=numcols;i++) {
System.out.print("\t" + rset.getString(i));
}
System.out.println("");
}
} catch(SQLException e) {
e.printStackTrace();
} 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
BasePoolableObjectFactory();
GenericObjectPool.Config genericObjectPoolConfig = new
GenericObjectPool.Config();
genericObjectPoolConfig.testOnReturn = true;
ObjectPool connectionPool =
new GenericObjectPool(
null,
8, // maxActive
GenericObjectPool.WHEN_EXHAUSTED_GROW, //
whenExhaustedAction
GenericObjectPool.DEFAULT_MAX_WAIT, //maxWait
true, //testOnBorrow
true); // testOnReturn
// look at what testOnBorrow is
System.out.println("DEFAULT_TEST_ON_BORROW
connectionPool.testOnBorrow " +
GenericObjectPool.DEFAULT_TEST_ON_BORROW);
System.out.println("DEFAUTL_MAX_WAIT " +
GenericObjectPool.DEFAULT_MAX_WAIT);
System.out.println("DEFAULT_MAX_ACTIVE " +
GenericObjectPool.DEFAULT_MAX_ACTIVE);
System.out.println("connectionPool numActive " +
connectionPool.getNumActive());
//System.out.println("connectionPool testOnBorrow " +
GenericObjectPool.Config.testOnBorrow);
// set testOnBorrow and testOnReturn
//connectionPool.new
GenericObjectPool.Config().setTestOnBorrow(true);
//
// 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
DriverManagerConnectionFactory(connectURI,null);
//
// 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(
connectionFactory,
connectionPool,
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
PoolingDataSource(connectionPool);
System.out.println("connectionPool numActive " +
connectionPool.getNumActive());
return dataSource;
}
}
I am basing this off the example code on commons.apache.org
http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/ManualPooling
DataSourceExample.java?view=log
Kay Green
11000 Westmoor Circle
Westminster, CO 80021
Office: 720 239-4639
Email: <ma...@mckesson.com> kay.green@mckesson.com
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
ride!"
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.
RE: How to tell how TestOnBorrow is set
Posted by "Green, Kay" <Ka...@McKesson.com>.
Phil
Thanks for the reply. I will try the suggestions
Kay Green
11000 Westmoor Circle
Westminster, CO 80021
Office: 720 239-4639
Email: kay.green@mckesson.com
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
ride!"
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.
-----Original Message-----
From: Phil Steitz [mailto:phil.steitz@gmail.com]
Sent: Wednesday, May 06, 2009 3:34 AM
To: Commons Users List
Subject: Re: How to tell how TestOnBorrow is set
Green, Kay wrote:
> I am trying tell what testOnBorrow is set to for my GenericObjectPool.
>
>
>
> Here is how I am constructing my GenericObjectPool
>
>
>
> ObjectPool connectionPool =
>
> new GenericObjectPool(
>
> null,
>
> 8, // maxActive
>
> GenericObjectPool.WHEN_EXHAUSTED_GROW, //
> whenExhaustedAction
>
> 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
> to.
>
>
The constructor call above sets the parameters. To verify, you could
cast connectionPool back to GenericObjectPool
((GenericObjectPool) connectionPool).getXxx()
>
>
> What I am trying to do is create a database connection pool.
>
Unless you need the control provided by the "manual" setup, it might be
easier to just use BasicDataSource, which exposes the configuration
properties.
Phil
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: How to tell how TestOnBorrow is set
Posted by Phil Steitz <ph...@gmail.com>.
Green, Kay wrote:
> I am trying tell what testOnBorrow is set to for my GenericObjectPool.
>
>
>
> Here is how I am constructing my GenericObjectPool
>
>
>
> ObjectPool connectionPool =
>
> new GenericObjectPool(
>
> null,
>
> 8, // maxActive
>
> GenericObjectPool.WHEN_EXHAUSTED_GROW, //
> whenExhaustedAction
>
> 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
> to.
>
>
The constructor call above sets the parameters. To verify, you could
cast connectionPool back to GenericObjectPool
((GenericObjectPool) connectionPool).getXxx()
>
>
> What I am trying to do is create a database connection pool.
>
Unless you need the control provided by the "manual" setup, it might be
easier to just use BasicDataSource, which exposes the configuration
properties.
Phil
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org