Example usage for org.apache.commons.dbcp2 PoolingDataSource PoolingDataSource

List of usage examples for org.apache.commons.dbcp2 PoolingDataSource PoolingDataSource

Introduction

In this page you can find the example usage for org.apache.commons.dbcp2 PoolingDataSource PoolingDataSource.

Prototype

public PoolingDataSource(ObjectPool<C> pool) 

Source Link

Usage

From source file:org.ops4j.pax.jdbc.pool.dbcp2.impl.ds.DbcpPooledDataSourceFactory.java

@Override
public DataSource createDataSource(Properties props) throws SQLException {
    try {//from w  w  w  .  ja  v a 2 s  . c  om
        DataSource ds = dsFactory.createDataSource(getNonPoolProps(props));
        DataSourceConnectionFactory connFactory = new DataSourceConnectionFactory((DataSource) ds);
        PoolableConnectionFactory pcf = new PoolableConnectionFactory(connFactory, null);
        GenericObjectPoolConfig conf = new GenericObjectPoolConfig();
        BeanConfig.configure(conf, getPoolProps(props));
        BeanConfig.configure(pcf, getPrefixed(props, FACTORY_PREFIX));
        GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(pcf, conf);
        pcf.setPool(pool);
        return new PoolingDataSource<PoolableConnection>(pool);
    } catch (Throwable e) {
        LOG.error("Error creating pooled datasource" + e.getMessage(), e);
        if (e instanceof SQLException) {
            throw (SQLException) e;
        } else if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}