3

这就是我使用 DBCP 1.4 连接工厂创建数据源的方式:

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

工作正常,但我不知道如何配置它,这里列出的参数:http: //commons.apache.org/dbcp/configuration.html。例如,我需要设置testWhileIdletrue.

4

2 回答 2

5
    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);
于 2012-11-26T12:42:39.970 回答
2

BasicDataSource 有这些属性,你可以切换使用吗?

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

...

于 2012-02-19T13:41:20.653 回答