use of org.apache.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);
}
use of org.apache.commons.pool2.impl.GenericObjectPool in project ofbiz-framework by apache.
the class DBCPConnectionFactory method getConnection.
public Connection getConnection(GenericHelperInfo helperInfo, JdbcElement abstractJdbc) throws SQLException, GenericEntityException {
String cacheKey = helperInfo.getHelperFullName();
DebugManagedDataSource mds = dsCache.get(cacheKey);
if (mds != null) {
return TransactionUtil.getCursorConnection(helperInfo, mds.getConnection());
}
if (!(abstractJdbc instanceof InlineJdbc)) {
throw new GenericEntityConfException("DBCP requires an <inline-jdbc> child element in the <datasource> element");
}
InlineJdbc jdbcElement = (InlineJdbc) abstractJdbc;
// connection properties
TransactionManager txMgr = TransactionFactoryLoader.getInstance().getTransactionManager();
String driverName = jdbcElement.getJdbcDriver();
String jdbcUri = helperInfo.getOverrideJdbcUri(jdbcElement.getJdbcUri());
String jdbcUsername = helperInfo.getOverrideUsername(jdbcElement.getJdbcUsername());
String jdbcPassword = helperInfo.getOverridePassword(EntityConfig.getJdbcPassword(jdbcElement));
// pool settings
int maxSize = jdbcElement.getPoolMaxsize();
int minSize = jdbcElement.getPoolMinsize();
int maxIdle = jdbcElement.getIdleMaxsize();
// maxIdle must be greater than pool-minsize
maxIdle = maxIdle > minSize ? maxIdle : minSize;
// load the driver
Driver jdbcDriver;
synchronized (DBCPConnectionFactory.class) {
// Sync needed for MS SQL JDBC driver. See OFBIZ-5216.
try {
jdbcDriver = (Driver) Class.forName(driverName, true, Thread.currentThread().getContextClassLoader()).newInstance();
} catch (Exception e) {
Debug.logError(e, module);
throw new GenericEntityException(e.getMessage(), e);
}
}
// connection factory properties
Properties cfProps = new Properties();
cfProps.put("user", jdbcUsername);
cfProps.put("password", jdbcPassword);
// create the connection factory
org.apache.commons.dbcp2.ConnectionFactory cf = new DriverConnectionFactory(jdbcDriver, jdbcUri, cfProps);
// wrap it with a LocalXAConnectionFactory
XAConnectionFactory xacf = new LocalXAConnectionFactory(txMgr, cf);
// create the pool object factory
PoolableConnectionFactory factory = new PoolableManagedConnectionFactory(xacf, null);
factory.setValidationQuery(jdbcElement.getPoolJdbcTestStmt());
factory.setDefaultReadOnly(false);
factory.setRollbackOnReturn(false);
factory.setEnableAutoCommitOnReturn(false);
String transIso = jdbcElement.getIsolationLevel();
if (!transIso.isEmpty()) {
if ("Serializable".equals(transIso)) {
factory.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
} else if ("RepeatableRead".equals(transIso)) {
factory.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
} else if ("ReadUncommitted".equals(transIso)) {
factory.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
} else if ("ReadCommitted".equals(transIso)) {
factory.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
} else if ("None".equals(transIso)) {
factory.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE);
}
}
// configure the pool settings
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(maxSize);
// settings for idle connections
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMinIdle(minSize);
poolConfig.setTimeBetweenEvictionRunsMillis(jdbcElement.getTimeBetweenEvictionRunsMillis());
// disabled in favour of setSoftMinEvictableIdleTimeMillis(...)
poolConfig.setMinEvictableIdleTimeMillis(-1);
poolConfig.setSoftMinEvictableIdleTimeMillis(jdbcElement.getSoftMinEvictableIdleTimeMillis());
// test all the idle connections
poolConfig.setNumTestsPerEvictionRun(maxSize);
// settings for when the pool is exhausted
// the thread requesting the connection waits if no connection is available
poolConfig.setBlockWhenExhausted(true);
// throw an exception if, after getPoolSleeptime() ms, no connection is available for the requesting thread
poolConfig.setMaxWaitMillis(jdbcElement.getPoolSleeptime());
// settings for the execution of the validation query
poolConfig.setTestOnCreate(jdbcElement.getTestOnCreate());
poolConfig.setTestOnBorrow(jdbcElement.getTestOnBorrow());
poolConfig.setTestOnReturn(jdbcElement.getTestOnReturn());
poolConfig.setTestWhileIdle(jdbcElement.getTestWhileIdle());
GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(factory, poolConfig);
factory.setPool(pool);
mds = new DebugManagedDataSource(pool, xacf.getTransactionRegistry());
mds.setAccessToUnderlyingConnectionAllowed(true);
// cache the pool
dsCache.putIfAbsent(cacheKey, mds);
mds = dsCache.get(cacheKey);
return TransactionUtil.getCursorConnection(helperInfo, mds.getConnection());
}
use of org.apache.commons.pool2.impl.GenericObjectPool in project ddf by codice.
the class LdapModuleTest method startup.
@Before
public void startup() throws LdapException {
server = TestServer.getInstance();
LdapLoginConfig ldapLoginConfig = new LdapLoginConfig(null);
ldapLoginConfig.setSecurityLogger(mock(SecurityLogger.class));
ConnectionFactory ldapConnectionFactory = ldapLoginConfig.createLdapConnectionFactory(TestServer.getUrl("ldap"), false);
module = TestModule.getInstance(TestServer.getClientOptions(), new GenericObjectPool<>(new LdapConnectionPooledObjectFactory(ldapConnectionFactory)));
}
use of org.apache.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 org.apache.commons.pool2.impl.GenericObjectPool in project cuba by cuba-platform.
the class KryoSerialization method initPool.
protected void initPool() {
// assume that application context is already initialized
Configuration configuration = AppBeans.get(Configuration.NAME);
config = configuration.getConfig(KryoSerializationConfig.class);
int poolSize = config.getMaxPoolSize();
GenericObjectPoolConfig<Kryo> poolConfig = new GenericObjectPoolConfig<>();
poolConfig.setMaxIdle(poolSize);
poolConfig.setMaxTotal(poolSize);
poolConfig.setMaxWaitMillis(config.getMaxBorrowWaitMillis());
String jmxName = "kryo-" + AppContext.getProperty("cuba.webContextName");
poolConfig.setJmxNamePrefix(jmxName);
PooledObjectFactory<Kryo> factory = new KryoObjectFactory(this);
pool = new GenericObjectPool<>(factory, poolConfig);
log.debug("Kryo context pool created");
}
Aggregations