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:com.github.brandtg.switchboard.TestMysqlReplicationApplier.java

private PoolingDataSource<PoolableConnection> getDataSource() throws Exception {
    // DBCP2 pool
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbc, "root", "");
    ObjectName poolName = new ObjectName(JdbcBasedLogIndex.class.getCanonicalName(),
            URLEncoder.encode(jdbc, "UTF-8"), "replicatorConnectionPool");
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            poolName);/*w ww  .j  av a 2 s  .co m*/
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    return new PoolingDataSource<PoolableConnection>(connectionPool);
}

From source file:de.alexandria.cms.config.SpringConfigBackendDatabase.java

@Bean
public DataSource pooledDataSource() {
    ObjectPool<PoolableConnection> pool = getObjectPool();
    PoolingDataSource ds = new PoolingDataSource<>(pool);
    return ds;//from  w  ww  .j  av a 2 s. c  om
}

From source file:eu.peppol.persistence.jdbc.OxalisDataSourceFactoryDbcpImplTest.java

private PoolingDataSource createPoolingDataSource(ConnectionFactory driverConnectionFactory) {

    PoolableConnectionFactory poolableConnectionFactory = null;
    try {//w w w  . java2s . c  o m

        poolableConnectionFactory = new PoolableConnectionFactory(driverConnectionFactory,
                new ObjectName("no.difi.oxalis", "connectionPool", "TestPool"));

        GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(
                poolableConnectionFactory);
        poolableConnectionFactory.setPool(pool);
        poolableConnectionFactory.setValidationQuery("select 1");
        return new PoolingDataSource(pool);

    } catch (MalformedObjectNameException e) {
        throw new IllegalStateException("Unable to create poolable conneciton factory: " + e.getMessage(), e);
    }

}

From source file:com.github.brandtg.switchboard.JdbcBasedLogIndex.java

@Override
public void start() throws Exception {
    Class.forName(driverClass);//w  ww  . j  a va  2 s  .c  o  m
    LOG.info("Loaded driver {}", driverClass);

    // DBCP2 pool
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionString, user, password);
    ObjectName poolName = new ObjectName(JdbcBasedLogIndex.class.getCanonicalName(),
            URLEncoder.encode(connectionString, ENCODING), "indexConnectionPool");
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            poolName);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    this.dataSource = new PoolingDataSource<PoolableConnection>(connectionPool);
    LOG.info("Opened connection pool to {} as {}", connectionString, user);

    createTable();
}

From source file:JDBCPool.dbcp.demo.sourcecode.BasicDataSource.java

/**
 * Creates the actual data source instance.  This method only exists so
 * that subclasses can replace the implementation class.
 *
 * @throws SQLException if unable to create a datasource instance
 *///  ww  w  . j  a v  a  2  s . c o m
protected DataSource createDataSourceInstance() throws SQLException {
    PoolingDataSource<PoolableConnection> pds = new PoolingDataSource<>(connectionPool);
    pds.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    return pds;
}

From source file:no.sr.ringo.persistence.jdbc.RingoDataSourceFactoryDbcpImpl.java

private PoolingDataSource getPoolingDataSource(JdbcConfiguration configuration, String connectURI,
        String userName, String password, Driver driver) {
    Properties properties = new Properties();
    properties.put("user", userName);
    properties.put("password", password);

    // DBCP factory which will produce JDBC Driver instances
    ConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, connectURI, properties);

    // DBCP Factory holding the pooled connection, which are created by the driver connection factory and held in the supplied pool
    ObjectName dataSourceJmxName;
    try {//w  w  w  .j a v a  2 s  .c  o  m
        dataSourceJmxName = new ObjectName("no.difi.oxalis", "connectionPool", "OxalisDB");
    } catch (MalformedObjectNameException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(driverConnectionFactory,
            dataSourceJmxName);

    if (configuration.getValidationQuery().isPresent()) {
        poolableConnectionFactory.setValidationQuery(configuration.getValidationQuery().get());
    }
    // DBCP object pool holding our driver connections
    GenericObjectPool<PoolableConnection> genericObjectPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    poolableConnectionFactory.setPool(genericObjectPool);
    genericObjectPool.setMaxTotal(100);
    genericObjectPool.setMaxIdle(30);
    genericObjectPool.setMaxWaitMillis(10000);

    genericObjectPool.setTestOnBorrow(true); // Test the connection returned from the pool

    genericObjectPool.setTestWhileIdle(true); // Test idle instances visited by the pool maintenance thread and destroy any that fail validation
    genericObjectPool.setTimeBetweenEvictionRunsMillis(60 * 60 * 1000); // Test every hour

    // Creates the actual DataSource instance
    return new PoolingDataSource(genericObjectPool);
}

From source file:org.aludratest.cloud.impl.app.LogDatabase.java

private Connection getConnection() throws SQLException {
    if (dataSource == null) {
        EmbeddedDataSource ds = new EmbeddedDataSource();
        ds.setDatabaseName("acm");
        ConnectionFactory connectionFactory = new DataSourceConnectionFactory(ds);
        PoolableConnectionFactory objFactory = new PoolableConnectionFactory(connectionFactory, null);
        objFactory.setValidationQuery("VALUES 1");
        objFactory.setDefaultAutoCommit(true);
        // max 10 minutes lifetime
        objFactory.setMaxConnLifetimeMillis(1000l * 60 * 10);
        // must be fast, because is local
        objFactory.setValidationQueryTimeout(5);

        GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(objFactory);
        pool.setMaxIdle(2);/* w  w w  . ja v a 2  s  .  com*/
        dataSource = new PoolingDataSource<PoolableConnection>(pool);
    }

    return dataSource.getConnection();
}

From source file:org.apdplat.superword.tools.MySQLUtils.java

private static DataSource setupDataSource(String connectUri, String uname, String passwd) {
    ///*from   ww  w  .ja va2s. co  m*/
    // First, we'll create a ConnectionFactory that the
    // pool will use to create Connections.
    // We'll use the DriverManagerConnectionFactory,
    // using the connect string passed in the command line
    // arguments.
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectUri, uname, passwd);

    //
    // Next we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);

    //
    // Now we'll need a ObjectPool that serves as the
    // actual pool of connections.
    //
    // We'll use a GenericObjectPool instance, although
    // any ObjectPool implementation will suffice.
    //
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);

    return dataSource;
}

From source file:org.kie.workbench.common.screens.datasource.management.backend.core.dbcp.DBCPDataSourceProvider.java

@Override
public DataSourceDeploymentInfo deploy(DataSourceDef dataSourceDef) throws Exception {

    DriverDef driverDef = null;/*  w w w.j a  v a 2 s  .  co m*/
    for (DriverDef _driverDef : driverProvider.getDeployments()) {
        if (_driverDef.getUuid().equals(dataSourceDef.getDriverUuid())) {
            driverDef = _driverDef;
            break;
        }
    }

    if (driverDef == null) {
        throw new Exception("Required driver: " + dataSourceDef.getDriverUuid() + " is not deployed");
    }

    final URI uri = artifactResolver.resolve(driverDef.getGroupId(), driverDef.getArtifactId(),
            driverDef.getVersion());
    if (uri == null) {
        throw new Exception("Unable to get driver library artifact for driver: " + driverDef);
    }

    final Properties properties = new Properties();
    properties.setProperty("user", dataSourceDef.getUser());
    properties.setProperty("password", dataSourceDef.getPassword());
    final URLConnectionFactory urlConnectionFactory = buildConnectionFactory(uri, driverDef.getDriverClass(),
            dataSourceDef.getConnectionURL(), properties);

    //Connection Factory that the pool will use for creating connections.
    ConnectionFactory connectionFactory = new DBCPConnectionFactory(urlConnectionFactory);

    //Poolable connection factory
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);

    //The pool to be used by the ConnectionFactory
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    //Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //Finally create DataSource
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);

    DataSourceDeploymentInfo deploymentInfo = new DataSourceDeploymentInfo(dataSourceDef.getUuid(), true,
            dataSourceDef.getUuid(), false);

    deploymentRegistry.put(deploymentInfo.getDeploymentId(), new DBCPDataSource(dataSource));
    deploymentInfos.put(deploymentInfo.getDeploymentId(), deploymentInfo);
    deployedDataSources.put(deploymentInfo.getDeploymentId(), dataSourceDef);

    return deploymentInfo;
}

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

@Override
public DataSource create(DataSourceFactory dsf, Properties props) throws SQLException {
    try {//  w  w  w .  j  a va 2s.c  o m
        DataSource ds = dsf.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);
        }
    }
}