commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dirk Verbeeck <dirk.verbe...@pandora.be>
Subject Re: [DBCP] Help with PreparedStatement pooling
Date Sat, 19 Feb 2005 10:33:49 GMT
Hi Edward,

Did you close the first statement before preparing the second one?
If you didn't close it the second prepare will indeed return another stmt.

You want something like this:
   Connection conn = ds.getConnection();
   Statement stmt1 = conn.prepareStatement("select 1 from dual");
   Statement ustmt1 =
     ((DelegatingStatement) stmt1).getInnermostDelegate();
   stmt1.close();
   Statement stmt2 = conn.prepareStatement("select 1 from dual");
   Statement ustmt2 =
     ((DelegatingStatement) stmt2).getInnermostDelegate();
   stmt2.close();
   assertSame(ustmt1, ustmt2);

Regards
Dirk

Edward Q. Bridges wrote:

> Hi, I'm quite confused about how to get prepared statement pooling to work.
> 
> This is the code I'm workign with:
> 
>        ConnectionFactory connFactory = new DriverManagerConnectionFactory(
>                cfg.getUrl(),
>                cfg.getUsername(),
>                cfg.getPassword()
>        );
> 
>        ObjectPool connPool = new GenericObjectPool(null, cfg.toConfig());
>        KeyedObjectPoolFactory stmtPoolFactory = new 
> GenericKeyedObjectPoolFactory(null);
> 
>        PoolableConnectionFactory x = new PoolableConnectionFactory(
>                connFactory,
>                connPool,
>                stmtPoolFactory,
>                cfg.getValidationQuery(),
>                cfg.isDefaultReadOnly(),
>                cfg.isDefaultAutoCommit(),
>                cfg.getDefaultTransactionIsolation()
>        );
> 
>        DataSource ds = new PoolingDataSource(connPool);
> 
> And when I run this, I'm getting an assertion failed when i retrieve two 
> prepared statements for the same connection.
> 
>    assertEquals(stmt, stmt2);
>    junit.framework.AssertionFailedError:
>          
> expected:<org.apache.commons.dbcp.DelegatingPreparedStatement@17a8a02>
>        but
>    was:<org.apache.commons.dbcp.DelegatingPreparedStatement@1b3f829>
> 
> 
>    assertEquals(((DelegatingPreparedStatement)stmt).getInnermostDelegate(),
>                     
> ((DelegatingPreparedStatement)stmt2).getInnermostDelegate());
>    junit.framework.AssertionFailedError:
>        expected:<com.sybase.jdbc2.jdbc.SybPreparedStatement@5a9de6>
>        but was:<com.sybase.jdbc2.jdbc.SybPreparedStatement@10da5eb>
> 
> What should I be expecting on this?  How would I tell that the pooling 
> is working correctly with PreparedStatements?
> Can someone point out what I'm doing wrong above?
> 
> Thanks in advance
> Ed Bridges



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