Hola Ignacio,

With DBCP, you *can* have a pool of PreparedStatements associated with each
Connection, although you don't strictly *have* to.

Like the pooling of Connections itself, you need to configure the DBCP to
pool PreparedStatements, but once you've set up the pool, the actual pooling
is transparent.  (You just call prepareStatement/close the way you normally
would.)

Since each PreparedStatment is intimately tied to its Connection, DBCP
really uses a number of PreparedStatement pools--one for each Connection.
Hence to set up the PreparedStatement pooling, you give DBCP a factory for
creating pools.  

To set the factory used to create pools used to pool PreparedStatements, you
pass a KeyedObjectPoolFactory to the PoolableConnectionFactory. This is a
simple class the creates arbitrary KeyedObjectPool instances.  The DBCP is
responsible for creating the statements to put into the pool--the pools
themselves simply act as a container.

Taking the ManualPoolingDriverExample (from dbcp/doc), note the line in
setupDriver() that reads:

 PoolableConnectionFactory poolableConnectionFactory = new
PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,t
rue);

simply pass in some instance of KeyedObjectPoolFactory as the third argument
(the first null).  E.g.,:

 PoolableConnectionFactory poolableConnectionFactory = new
PoolableConnectionFactory(connectionFactory,connectionPool,new
StackKeyedObjectPoolFactory(),null,false,true);

You can of course use any KeyedObjectPool instance.  The PreparedStatment
pooling will then happen automatically.

For the JOCL example (dbcp/doc/JOCLPoolingDriverExample.java), change the
line in poolingDriverExample.jocl that reads:

  <object class="org.apache.commons.pool.KeyedObjectPoolFactory"
null="true"/>

to something like:

  <object class="org.apache.commons.pool.impl.StackKeyedObjectPoolFactory"/>

Again, the statment pooling should then happen automatically.

Let me know if you'd like more detail.

 - Rod

Reply via email to