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);
}
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;
}
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);
}
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());
}
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);
}
Aggregations