commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From miten mehta <>
Subject Re: [dbcp] newbie preparedStatement pooling question
Date Sat, 29 Sep 2012 12:31:21 GMT

Once you get connection from datasource/driver and call prepareStatement (sql) it will create
/ obtain one from pool as per API.

Also stackoverflow has discussion on it.  The sql will be key in this case for the key pool
and PreparedStatement the value.



 From: AJ Weber <>
Sent: Friday, September 28, 2012 12:04 AM
Subject: [dbcp] newbie preparedStatement pooling question
I _did_ try Google, I promise.  There are a few examples of this, but they're not documented
enough that I fully understand proper use of this...

It appears that I can enable pooling of preparedStatements by "simply" including an ObjectPoolFactory
in the PoolableConnectionFactory constructor:

KeyedObjectPoolFactory stmtPoolFactory = new GenericKeyedObjectPoolFactory(null);
new PoolableConnectionFactory(connectionFactory, connectionPool,
                        stmtPoolFactory, null, false, true, Connection.TRANSACTION_READ_COMMITTED);

(I suppose the first question is please confirm that is all I need to do for initial-setup?)

But how do I add & retrieve preparedStatements to/from the pool?

Should my method always call conn.prepareStatement("update..."); and in the background if
a previously-prepared statement is available, it'll be used otherwise it's sent to the DB
as normal?  Do I just "close" the prepared statement normally, and again, the prepared statement
will be returned (or added) to the pool automatically?

When managing my own jdbc connection, I would typically prepare the statements once, in some
kind of init method and tuck them away in a private variable, so they were available throughout
the remainder of the class/program's lifetime.  Calling conn.prepareStatement( ) every time
I want to [re-]use a statement just feels counter-intuitive somehow.

Thanks for bearing-with my newbie questions.


To unsubscribe, e-mail:
For additional commands, e-mail:
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message