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

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

Introduction

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

Prototype

public PoolableConnectionFactory(ConnectionFactory connFactory, ObjectName dataSourceJmxName) 

Source Link

Document

Create a new PoolableConnectionFactory .

Usage

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);//www .  j  a  v  a  2  s.  c  o  m
        dataSource = new PoolingDataSource<PoolableConnection>(pool);
    }

    return dataSource.getConnection();
}

From source file:org.apache.zeppelin.jdbc.JDBCInterpreter.java

private void createConnectionPool(String url, String user, String propertyKey, Properties properties)
        throws SQLException, ClassNotFoundException {
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, properties);

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);/* w w w . j ava2  s .  c om*/
    ObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);

    poolableConnectionFactory.setPool(connectionPool);
    Class.forName(properties.getProperty(DRIVER_KEY));
    PoolingDriver driver = new PoolingDriver();
    driver.registerPool(propertyKey + user, connectionPool);
    getJDBCConfiguration(user).saveDBDriverPool(propertyKey, driver);
}

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

private static DataSource setupDataSource(String connectUri, String uname, String passwd) {
    ////w  w w.  j a v  a  2 s  .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.jboss.narayana.quickstart.spring.config.DatabaseConfig.java

@Bean
public DataSource dataSource() {
    DataSourceXAConnectionFactory dataSourceXAConnectionFactory = new DataSourceXAConnectionFactory(tm,
            h2DataSource());/* w  w w. j av a 2 s.co  m*/
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
            dataSourceXAConnectionFactory, null);
    GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    return new ManagedDataSource<>(connectionPool, dataSourceXAConnectionFactory.getTransactionRegistry());
}

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;//from  ww w .ja 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.moneta.config.ConnectionPoolFactory.java

public static ObjectPool<PoolableConnection> createConnectionPool(MonetaDataSource dataSourceType) {
    Validate.notNull(dataSourceType, "Null MonetaDataSource not allowed.");

    Validate.notEmpty(dataSourceType.getDataSourceName(), "Null or blank name not allowed");
    Validate.notEmpty(dataSourceType.getConnectionUrl(), "Null or blank url not allowed");
    Validate.notNull(dataSourceType.getDriver(), "Null driver not allowed");

    try {/*from   www.  ja v  a 2 s.co m*/
        dataSourceType.getDriver().newInstance();
    } catch (Exception e) {
        throw new MonetaException("Data source JDBC driver can't be instantiated", e)
                .addContextValue("JDBC Driver class", dataSourceType.getDriver().getName());
    }

    Properties connectionProps = new Properties();
    connectionProps.putAll(dataSourceType.getJdbcConnectionProperties());
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dataSourceType.getConnectionUrl(),
            connectionProps);

    ObjectName poolName = null;
    try {
        poolName = new ObjectName("org.moneta", "connectionPool", dataSourceType.getDataSourceName());
    } catch (MalformedObjectNameException e) {
        throw new MonetaException("Data source name not valid", e).addContextValue("Data source name",
                dataSourceType.getDataSourceName());
    }
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            poolName);

    Set<String> unusedConnectionPoolPropSet = new HashSet<String>();
    Map<String, String> propMap = new HashMap<String, String>(dataSourceType.getConnectionPoolProperties());

    PropertyDescriptor pDesc = null;
    for (String propName : propMap.keySet()) {
        pDesc = assignProperty(dataSourceType.getDataSourceName(), poolableConnectionFactory, propName,
                propMap.get(propName));
        if (pDesc == null) {
            unusedConnectionPoolPropSet.add(propName);
        }
    }

    GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    poolableConnectionFactory.setPool(pool);

    for (String propName : new HashSet<String>(unusedConnectionPoolPropSet)) {
        pDesc = assignProperty(dataSourceType.getDataSourceName(), pool, propName, propMap.get(propName));
        if (pDesc != null) {
            unusedConnectionPoolPropSet.remove(propName);
        }
    }

    if (unusedConnectionPoolPropSet.size() > 0) {
        MonetaException me = new MonetaException("Invalid connection pool properties detected");
        for (String propName : unusedConnectionPoolPropSet) {
            me.addContextValue("pool property name", propName);
        }

        throw me;
    }
    return pool;
}

From source file:org.moneta.healthcheck.DbcpConnectionPoolHealthCheckTest.java

@Before
public void setUp() throws Exception {
    org.hsqldb.jdbc.JDBCDriver.class.newInstance();
    Properties connectionProps = new Properties();
    connectionProps.put("user", "SA");
    connectionProps.put("password", "");
    connectionProps.put("create", "true");

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:hsqldb:mem:my-sample",
            connectionProps);//ww  w.ja  va2  s .  c  o  m

    ObjectName poolName = poolName = new ObjectName("org.moneta", "connectionPool", "TestPool");
    poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, poolName);
    poolableConnectionFactory.setDefaultCatalog("PUBLIC");
    poolableConnectionFactory.setValidationQuery(VALIDATION_SQL);

    connectionPool = new GenericObjectPool<PoolableConnection>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    connectionPool.setMaxTotal(2);
    connectionPool.setMaxWaitMillis(400);

    healthCheck = new DbcpConnectionPoolHealthCheck(connectionPool, "mine");
}

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

@Test
public void testPoolableConnectionFactory() throws Exception {
    Map<String, String> props = new HashMap<String, String>();
    props.put("validationQuery", "dummyQuery");
    props.put("validationQueryTimeout", "1");
    PoolableConnectionFactory pcf = new PoolableConnectionFactory(null, null);
    BeanConfig.configure(pcf, props);/*from  w  w  w .  j a va 2 s  .  c  o  m*/

    PoolableConnection connection = EasyMock.createMock(PoolableConnection.class);
    connection.validate(EasyMock.eq("dummyQuery"), EasyMock.eq(1));
    EasyMock.expectLastCall();
    EasyMock.expect(connection.isClosed()).andReturn(false);
    EasyMock.replay(connection);

    pcf.validateConnection(connection);

    EasyMock.verify(connection);
}

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 v  a  2  s.  co 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);
        }
    }
}

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 . j ava2s .  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);
        }
    }
}