Search in sources :

Example 1 with DataSourceConnectionFactory

use of org.apache.commons.dbcp2.DataSourceConnectionFactory in project tomee by apache.

the class DbcpManagedDataSource method createConnectionFactory.

@Override
protected ConnectionFactory createConnectionFactory() throws SQLException {
    if (ds instanceof XADataSource) {
        // Create the XAConectionFactory using the XA data source
        final XADataSource xaDataSourceInstance = (XADataSource) ds;
        final XAConnectionFactory xaConnectionFactory = new DataSourceXAConnectionFactory(getTransactionManager(), xaDataSourceInstance, getUsername(), getPassword());
        setTransactionRegistry(xaConnectionFactory.getTransactionRegistry());
        return xaConnectionFactory;
    }
    // If xa data source is not specified a DriverConnectionFactory is created and wrapped with a LocalXAConnectionFactory
    final ConnectionFactory connectionFactory = new DataSourceConnectionFactory(DataSource.class.cast(ds), getUsername(), getPassword());
    final XAConnectionFactory xaConnectionFactory = new LocalXAConnectionFactory(getTransactionManager(), connectionFactory);
    setTransactionRegistry(xaConnectionFactory.getTransactionRegistry());
    return xaConnectionFactory;
}
Also used : XADataSource(javax.sql.XADataSource) ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) DataSourceConnectionFactory(org.apache.commons.dbcp2.DataSourceConnectionFactory) DataSourceXAConnectionFactory(org.apache.commons.dbcp2.managed.DataSourceXAConnectionFactory) LocalXAConnectionFactory(org.apache.commons.dbcp2.managed.LocalXAConnectionFactory) XAConnectionFactory(org.apache.commons.dbcp2.managed.XAConnectionFactory) DataSourceConnectionFactory(org.apache.commons.dbcp2.DataSourceConnectionFactory) DataSourceXAConnectionFactory(org.apache.commons.dbcp2.managed.DataSourceXAConnectionFactory) DataSourceXAConnectionFactory(org.apache.commons.dbcp2.managed.DataSourceXAConnectionFactory) LocalXAConnectionFactory(org.apache.commons.dbcp2.managed.LocalXAConnectionFactory) XAConnectionFactory(org.apache.commons.dbcp2.managed.XAConnectionFactory) LocalXAConnectionFactory(org.apache.commons.dbcp2.managed.LocalXAConnectionFactory) CommonDataSource(javax.sql.CommonDataSource) DataSource(javax.sql.DataSource) XADataSource(javax.sql.XADataSource)

Example 2 with DataSourceConnectionFactory

use of org.apache.commons.dbcp2.DataSourceConnectionFactory 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)

Aggregations

ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)2 DataSourceConnectionFactory (org.apache.commons.dbcp2.DataSourceConnectionFactory)2 Map (java.util.Map)1 CommonDataSource (javax.sql.CommonDataSource)1 DataSource (javax.sql.DataSource)1 XADataSource (javax.sql.XADataSource)1 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)1 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)1 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)1 DataSourceXAConnectionFactory (org.apache.commons.dbcp2.managed.DataSourceXAConnectionFactory)1 LocalXAConnectionFactory (org.apache.commons.dbcp2.managed.LocalXAConnectionFactory)1 XAConnectionFactory (org.apache.commons.dbcp2.managed.XAConnectionFactory)1 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)1 DatabaseProduct (org.apache.hadoop.hive.metastore.DatabaseProduct)1