Search code examples
javajdbcapache-commons-dbcp

How to configure DBCP PoolableConnectionFactory?


This is how I create a DataSource with DBCP 1.4 connection factory:

PoolableConnectionFactory factory = new PoolableConnectionFactory(
  new DriverManagerConnectionFactory("jdbc:h2:mem:db", "", ""),
  new GenericObjectPool(null),
  null,
  "SELECT 1",
  false,
  true
);
DataSource src = new PoolingDataSource(factory.getPool());

Works fine, but I don't know how to configure it, with parameters listed here: http://commons.apache.org/dbcp/configuration.html. For example, I need to set testWhileIdle to true.


Solution

  • BasicDataSource has these attributes, can you switch to use that ?

    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(JDBCDriver);
    ds.setUrl(JDBCUrl);
    ds.setUsername(JDBCUser);
    ds.setPassword(JDBCPassword);
    ds.setInitialSize(initSize);
    ds.setTestOnBorrow(false);
    ds.setTestWhileIdle(true);
    

    ...