Search in sources :

Example 1 with PoolingDataSource

use of org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource in project cloudstack by apache.

the class TransactionLegacy method createDataSource.

/**
 * Creates a data source
 */
private static DataSource createDataSource(String uri, String username, String password, Integer maxActive, Integer maxIdle, Long maxWait, Long timeBtwnEvictionRuns, Long minEvictableIdleTime, Boolean testWhileIdle, Boolean testOnBorrow, String validationQuery, Integer isolationLevel) {
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(uri, username, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    GenericObjectPoolConfig config = createPoolConfig(maxActive, maxIdle, maxWait, timeBtwnEvictionRuns, minEvictableIdleTime, testWhileIdle, testOnBorrow);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, config);
    poolableConnectionFactory.setPool(connectionPool);
    if (validationQuery != null) {
        poolableConnectionFactory.setValidationQuery(validationQuery);
    }
    if (isolationLevel != null) {
        poolableConnectionFactory.setDefaultTransactionIsolation(isolationLevel);
    }
    return new PoolingDataSource<>(connectionPool);
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 2 with PoolingDataSource

use of org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource in project hive by apache.

the class TestDataSourceProviderFactory method testCreateDbCpDataSource.

@Test
public void testCreateDbCpDataSource() throws SQLException {
    MetastoreConf.setVar(conf, ConfVars.CONNECTION_POOLING_TYPE, DbCPDataSourceProvider.DBCP);
    DataSourceProvider dsp = DataSourceProviderFactory.tryGetDataSourceProviderOrNull(conf);
    Assert.assertNotNull(dsp);
    DataSource ds = dsp.create(conf);
    Assert.assertTrue(ds instanceof PoolingDataSource);
}
Also used : PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource) PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) Test(org.junit.Test) MetastoreUnitTest(org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest)

Example 3 with PoolingDataSource

use of org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource in project kie-wb-common by kiegroup.

the class DBCPDataSourceProvider method deploy.

@Override
public DataSourceDeploymentInfo deploy(DataSourceDef dataSourceDef) throws Exception {
    DriverDef driverDef = null;
    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;
}
Also used : URLConnectionFactory(org.kie.workbench.common.screens.datasource.management.util.URLConnectionFactory) PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) URI(java.net.URI) DataSourceDeploymentInfo(org.kie.workbench.common.screens.datasource.management.model.DataSourceDeploymentInfo) SQLException(java.sql.SQLException) ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) URLConnectionFactory(org.kie.workbench.common.screens.datasource.management.util.URLConnectionFactory) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) DriverDef(org.kie.workbench.common.screens.datasource.management.model.DriverDef) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 4 with PoolingDataSource

use of org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource in project hive by apache.

the class DbCPDataSourceProvider method create.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DataSource create(Configuration hdpConfig) throws SQLException {
    LOG.debug("Creating dbcp connection pool for the MetaStore");
    String driverUrl = DataSourceProvider.getMetastoreJdbcDriverUrl(hdpConfig);
    String user = DataSourceProvider.getMetastoreJdbcUser(hdpConfig);
    String passwd = DataSourceProvider.getMetastoreJdbcPasswd(hdpConfig);
    BasicDataSource dbcpDs = new BasicDataSource();
    dbcpDs.setUrl(driverUrl);
    dbcpDs.setUsername(user);
    dbcpDs.setPassword(passwd);
    dbcpDs.setDefaultReadOnly(false);
    dbcpDs.setDefaultAutoCommit(true);
    DatabaseProduct dbProduct = DatabaseProduct.determineDatabaseProduct(driverUrl, hdpConfig);
    Map<String, String> props = dbProduct.getDataSourceProperties();
    for (Map.Entry<String, String> kv : props.entrySet()) {
        dbcpDs.setConnectionProperties(kv.getKey() + "=" + kv.getValue());
    }
    int maxPoolSize = hdpConfig.getInt(MetastoreConf.ConfVars.CONNECTION_POOLING_MAX_CONNECTIONS.getVarname(), ((Long) MetastoreConf.ConfVars.CONNECTION_POOLING_MAX_CONNECTIONS.getDefaultVal()).intValue());
    long connectionTimeout = hdpConfig.getLong(CONNECTION_TIMEOUT_PROPERTY, 30000L);
    int connectionMaxIlde = hdpConfig.getInt(CONNECTION_MAX_IDLE_PROPERTY, 8);
    int connectionMinIlde = hdpConfig.getInt(CONNECTION_MIN_IDLE_PROPERTY, 0);
    boolean testOnBorrow = hdpConfig.getBoolean(CONNECTION_TEST_BORROW_PROPERTY, BaseObjectPoolConfig.DEFAULT_TEST_ON_BORROW);
    long evictionTimeMillis = hdpConfig.getLong(CONNECTION_MIN_EVICT_MILLIS_PROPERTY, BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
    boolean testWhileIdle = hdpConfig.getBoolean(CONNECTION_TEST_IDLEPROPERTY, BaseObjectPoolConfig.DEFAULT_TEST_WHILE_IDLE);
    long timeBetweenEvictionRuns = hdpConfig.getLong(CONNECTION_TIME_BETWEEN_EVICTION_RUNS_MILLIS, BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
    int numTestsPerEvictionRun = hdpConfig.getInt(CONNECTION_NUM_TESTS_PER_EVICTION_RUN, BaseObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN);
    boolean testOnReturn = hdpConfig.getBoolean(CONNECTION_TEST_ON_RETURN, BaseObjectPoolConfig.DEFAULT_TEST_ON_RETURN);
    long softMinEvictableIdleTimeMillis = hdpConfig.getLong(CONNECTION_SOFT_MIN_EVICTABLE_IDLE_TIME, BaseObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
    boolean lifo = hdpConfig.getBoolean(CONNECTION_LIFO, BaseObjectPoolConfig.DEFAULT_LIFO);
    ConnectionFactory connFactory = new DataSourceConnectionFactory(dbcpDs);
    PoolableConnectionFactory poolableConnFactory = new PoolableConnectionFactory(connFactory, null);
    GenericObjectPool objectPool = new GenericObjectPool(poolableConnFactory);
    objectPool.setMaxTotal(maxPoolSize);
    objectPool.setMaxWaitMillis(connectionTimeout);
    objectPool.setMaxIdle(connectionMaxIlde);
    objectPool.setMinIdle(connectionMinIlde);
    objectPool.setTestOnBorrow(testOnBorrow);
    objectPool.setTestWhileIdle(testWhileIdle);
    objectPool.setMinEvictableIdleTimeMillis(evictionTimeMillis);
    objectPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRuns);
    objectPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    objectPool.setTestOnReturn(testOnReturn);
    objectPool.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
    objectPool.setLifo(lifo);
    String stmt = dbProduct.getPrepareTxnStmt();
    if (stmt != null) {
        poolableConnFactory.setValidationQuery(stmt);
    }
    return new PoolingDataSource(objectPool);
}
Also used : PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) DatabaseProduct(org.apache.hadoop.hive.metastore.DatabaseProduct) ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) DataSourceConnectionFactory(org.apache.commons.dbcp2.DataSourceConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) DataSourceConnectionFactory(org.apache.commons.dbcp2.DataSourceConnectionFactory) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) Map(java.util.Map) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 5 with PoolingDataSource

use of org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource in project datanucleus-rdbms by datanucleus.

the class DBCP2BuiltinConnectionPoolFactory method createConnectionPool.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.datasource.ConnectionPoolFactory#createConnectionPool(org.datanucleus.store.StoreManager)
     */
public ConnectionPool createConnectionPool(StoreManager storeMgr) {
    // Load the database driver
    String dbDriver = storeMgr.getConnectionDriverName();
    if (!StringUtils.isWhitespace(dbDriver)) {
        loadDriver(dbDriver, storeMgr.getNucleusContext().getClassLoaderResolver(null));
    }
    String dbURL = storeMgr.getConnectionURL();
    PoolingDataSource ds = null;
    GenericObjectPool<PoolableConnection> connectionPool;
    try {
        // Create a factory to be used by the pool to create the connections
        Properties dbProps = getPropertiesForDriver(storeMgr);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dbURL, dbProps);
        // Wrap the connections and statements with pooled variants
        PoolableConnectionFactory poolableCF = null;
        poolableCF = new PoolableConnectionFactory(connectionFactory, null);
        String testSQL = null;
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TEST_SQL)) {
            testSQL = storeMgr.getStringProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TEST_SQL);
            poolableCF.setValidationQuery(testSQL);
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_VALIDATION_TIMEOUT)) {
            int validationTimeout = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_VALIDATION_TIMEOUT);
            if (validationTimeout >= 0) {
                poolableCF.setValidationQueryTimeout(validationTimeout);
            }
        }
        // Create the actual pool of connections, and apply any properties
        connectionPool = new GenericObjectPool(poolableCF);
        poolableCF.setPool(connectionPool);
        if (testSQL != null) {
            connectionPool.setTestOnBorrow(true);
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_IDLE)) {
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_IDLE);
            if (value > 0) {
                connectionPool.setMaxIdle(value);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_IDLE)) {
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_IDLE);
            if (value > 0) {
                connectionPool.setMinIdle(value);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_ACTIVE)) {
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_ACTIVE);
            if (value > 0) {
                connectionPool.setMaxTotal(value);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_WAIT)) {
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_WAIT);
            if (value > 0) {
                connectionPool.setMaxWaitMillis(value);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TIME_BETWEEN_EVICTOR_RUNS_MILLIS)) {
            // how often should the evictor run (if ever, default is -1 = off)
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TIME_BETWEEN_EVICTOR_RUNS_MILLIS);
            if (value > 0) {
                connectionPool.setTimeBetweenEvictionRunsMillis(value);
                // in each eviction run, evict at least a quarter of "maxIdle" connections
                int maxIdle = connectionPool.getMaxIdle();
                int numTestsPerEvictionRun = (int) Math.ceil((double) maxIdle / 4);
                connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
            }
        }
        if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS)) {
            // how long may a connection sit idle in the pool before it may be evicted
            int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS);
            if (value > 0) {
                connectionPool.setMinEvictableIdleTimeMillis(value);
            }
        }
        // Create the datasource
        ds = new org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource(connectionPool);
    } catch (Exception e) {
        throw new DatastorePoolException("DBCP2", dbDriver, dbURL, e);
    }
    return new DBCPConnectionPool(ds, connectionPool);
}
Also used : PoolingDataSource(org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource) DriverManagerConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.DriverManagerConnectionFactory) Properties(java.util.Properties) GenericObjectPool(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPool) DriverManagerConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.DriverManagerConnectionFactory) PoolableConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnectionFactory) ConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.ConnectionFactory) PoolableConnection(org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnection) PoolingDataSource(org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource) PoolableConnectionFactory(org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnectionFactory)

Aggregations

PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)5 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)4 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)4 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)4 Properties (java.util.Properties)2 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)2 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)2 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 URI (java.net.URI)1 SQLException (java.sql.SQLException)1 Map (java.util.Map)1 DataSource (javax.sql.DataSource)1 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)1 DataSourceConnectionFactory (org.apache.commons.dbcp2.DataSourceConnectionFactory)1 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)1 DatabaseProduct (org.apache.hadoop.hive.metastore.DatabaseProduct)1 MetastoreUnitTest (org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest)1 ConnectionFactory (org.datanucleus.store.rdbms.datasource.dbcp2.ConnectionFactory)1 DriverManagerConnectionFactory (org.datanucleus.store.rdbms.datasource.dbcp2.DriverManagerConnectionFactory)1 PoolableConnection (org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnection)1