Hi, 

I want to use pooled PreparedStatement feature of PoolingConnection, 
I try to write the following code:
-----------------------------------------------------
  try
  {
      con = DriverManager.getConnection(url);
  }
  catch (Exception e)
  {
      throw new RuntimeException(e);
  }
  con = new PoolingConnection(con);
--------------------------------------------------------

Later I am trying to get a PreparedStatement from this connection, 
however there is an error occurred.

After I change the above code to:
---------------------------------------------------------
  try
  {
      con = DriverManager.getConnection(url);
  }
  catch (Exception e)
  {
      throw new RuntimeException(e);
  }
  con = new PoolingConnection(con);
  KeyedObjectPool stmtPool =
      new GenericKeyedObjectPool(
          (PoolingConnection) con,
          8, //max active
          GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 
          1 * 1000, //max wait
          1 //max idle
          );
  con = new PoolingConnection(con, stmtPool);
-----------------------------------------------------------------

Everything is ok, however, it is ugly that a PoolingConnection
is created twice in order to make it work!

Take a look at the source code of PoolingConnection:
-----------------------------------------------------------------
    /**
     * Constructor.
     * @param c the underlying {@link Connection}.
     */
    public PoolingConnection(Connection c) {
        super(c);
    }

    /**
     * Constructor.
     * @param c the underlying {@link Connection}.
     * @param maxSleepingPerKey the maximum number of {@link PreparedStatement}s that 
may sit idle in my pool (per type)
     */
    public PoolingConnection(Connection c, KeyedObjectPool pool) {
        super(c);
        _pstmtPool = pool;
    }
-----------------------------------------------------------------

I wonder why the first constructor do not create a default stmtPool instead of 
leave it null, isn't it better to write first constructor like this:
    /**
     * Constructor.
     * @param c the underlying {@link Connection}.
     */
    public PoolingConnection(Connection c) {
        super(c);
        _pstmtPool = new GenericKeyedObjectPool(
          this,
          DEF_MAX_ACTIVE, //max active
          GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW, 
          DEF_MAX_WAITE, //max wait
          DEF_MAX_IDLE //max idle
          );
    }


Best Regards,
Green


Reply via email to