Search in sources :

Example 46 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project cdap by cdapio.

the class PostgreSqlStorageProvider method createDataSource.

/**
 * Creates a {@link DataSource} for the sql implementation to use. It optionally loads an external JDBC driver
 * to use with JDBC.
 */
@VisibleForTesting
public static DataSource createDataSource(CConfiguration cConf, SConfiguration sConf, MetricsCollectionService metricsCollectionService) {
    String storageImpl = cConf.get(Constants.Dataset.DATA_STORAGE_IMPLEMENTATION);
    if (!storageImpl.equals(Constants.Dataset.DATA_STORAGE_SQL)) {
        throw new IllegalArgumentException(String.format("The storage implementation is not %s, cannot create the " + "DataSource", Constants.Dataset.DATA_STORAGE_SQL));
    }
    if (cConf.getBoolean(Constants.Dataset.DATA_STORAGE_SQL_DRIVER_EXTERNAL)) {
        loadJDBCDriver(cConf, storageImpl);
    }
    String jdbcUrl = cConf.get(Constants.Dataset.DATA_STORAGE_SQL_JDBC_CONNECTION_URL);
    if (jdbcUrl == null) {
        throw new IllegalArgumentException("The jdbc connection url is not specified.");
    }
    Properties properties = retrieveJDBCConnectionProperties(cConf, sConf);
    LOG.info("Creating the DataSource with jdbc url: {}", jdbcUrl);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, properties);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    // The GenericObjectPool is thread safe according to the javadoc,
    // the PoolingDataSource will be thread safe as long as the connectin pool is thread-safe
    GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    connectionPool.setMaxTotal(cConf.getInt(Constants.Dataset.DATA_STORAGE_SQL_CONNECTION_SIZE));
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
    return new MetricsDataSource(dataSource, metricsCollectionService, 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) MetricsDataSource(io.cdap.cdap.spi.data.sql.jdbc.MetricsDataSource) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 47 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool 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 48 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool 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 49 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project atmosphere by Atmosphere.

the class PoolableBroadcasterFactoryTest method testImplementation.

@Test
public void testImplementation() {
    assertNotNull(factory.poolableProvider());
    assertNotNull(factory.poolableProvider().implementation());
    assertEquals(factory.poolableProvider().implementation().getClass(), GenericObjectPool.class);
    GenericObjectPool nativePool = (GenericObjectPool) factory.poolableProvider().implementation();
    assertTrue(nativePool.getLifo());
    GenericObjectPoolConfig c = new GenericObjectPoolConfig();
    c.setMaxTotal(1);
    nativePool.setConfig(c);
    assertEquals(1, nativePool.getMaxTotal());
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Test(org.testng.annotations.Test)

Example 50 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project mlib by myshzzx.

the class ThriftClientFactory method buildPooled.

/**
 * build pooled(auto close connections) and thread-safe (unblocking) client.
 * <br/>
 * WARNING: the holder needs to be closed after using.
 */
@SuppressWarnings("unchecked")
public ClientHolder<TI> buildPooled() {
    GenericObjectPoolConfig poolConf = new GenericObjectPoolConfig();
    poolConf.setMinIdle(0);
    poolConf.setMaxTotal(Integer.MAX_VALUE);
    poolConf.setMaxWaitMillis(conf.clientSocketTimeout);
    poolConf.setTimeBetweenEvictionRunsMillis(POOL_IDLE_OBJ_TIMEOUT);
    poolConf.setTestWhileIdle(true);
    GenericObjectPool<ThriftClient> pool = new GenericObjectPool(new PoolObjMaker(), poolConf);
    TI client = (TI) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class<?>[] { conf.iface }, (obj, method, args) -> {
        ThriftClient tc = pool.borrowObject();
        try {
            return tc.invokeThriftClient(method, args);
        } finally {
            pool.returnObject(tc);
        }
    });
    return new ClientHolder<>(client, pool::close);
}
Also used : BasePooledObjectFactory(org.apache.commons.pool2.BasePooledObjectFactory) Logger(org.slf4j.Logger) Proxy(java.lang.reflect.Proxy) PooledObject(org.apache.commons.pool2.PooledObject) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) org.apache.thrift.transport(org.apache.thrift.transport) LoggerFactory(org.slf4j.LoggerFactory) TException(org.apache.thrift.TException) IOException(java.io.IOException) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) SocketException(java.net.SocketException) TProtocol(org.apache.thrift.protocol.TProtocol) Closeable(java.io.Closeable) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) Method(java.lang.reflect.Method) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Aggregations

GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)79 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)32 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)27 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)23 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)19 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)16 Test (org.junit.jupiter.api.Test)13 Properties (java.util.Properties)11 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)9 SQLException (java.sql.SQLException)8 PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)8 Connection (java.sql.Connection)7 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)5 Bean (org.springframework.context.annotation.Bean)5 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)4 IOException (java.io.IOException)3 PooledObject (org.apache.commons.pool2.PooledObject)3 Test (org.junit.Test)3 TimeInterval (com.adaptris.util.TimeInterval)2 ThresholdedRandomCutForestMapper (com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper)2