yegor256
yegor256

Reputation: 105213

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.

Upvotes: 3

Views: 9148

Answers (2)

Robert
Robert

Reputation: 51

    Properties props = new Properties();
    props.put("validationQuery", "SELECT 1 from dual;");
    props.put("testWhileIdle","true");

    final ObjectPool connectionPool = new GenericObjectPool(null);
    final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectUri, props);
    new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
    final PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

Upvotes: 5

yael alfasi
yael alfasi

Reputation: 682

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

...

Upvotes: 2

Related Questions