use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project bboss by bbossgroups.
the class PerUserPoolDataSource method registerPool.
private synchronized void registerPool(final String userName, final String password) throws NamingException, SQLException {
final ConnectionPoolDataSource cpds = testCPDS(userName, password);
// Set up the factory we will use (passing the pool associates
// the factory with the pool, so we do not have to do so
// explicitly)
final CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, getValidationQuery(), getValidationQueryTimeout(), isRollbackAfterValidation(), userName, password);
factory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis());
// Create an object pool to contain our PooledConnections
final GenericObjectPool<PooledConnectionAndInfo> pool = new GenericObjectPool<PooledConnectionAndInfo>(factory);
factory.setPool(pool);
pool.setBlockWhenExhausted(getPerUserBlockWhenExhausted(userName));
pool.setEvictionPolicyClassName(getPerUserEvictionPolicyClassName(userName));
pool.setLifo(getPerUserLifo(userName));
pool.setMaxIdle(getPerUserMaxIdle(userName));
pool.setMaxTotal(getPerUserMaxTotal(userName));
pool.setMaxWaitMillis(getPerUserMaxWaitMillis(userName));
pool.setMinEvictableIdleTimeMillis(getPerUserMinEvictableIdleTimeMillis(userName));
pool.setMinIdle(getPerUserMinIdle(userName));
pool.setNumTestsPerEvictionRun(getPerUserNumTestsPerEvictionRun(userName));
pool.setSoftMinEvictableIdleTimeMillis(getPerUserSoftMinEvictableIdleTimeMillis(userName));
pool.setTestOnCreate(getPerUserTestOnCreate(userName));
pool.setTestOnBorrow(getPerUserTestOnBorrow(userName));
pool.setTestOnReturn(getPerUserTestOnReturn(userName));
pool.setTestWhileIdle(getPerUserTestWhileIdle(userName));
pool.setTimeBetweenEvictionRunsMillis(getPerUserTimeBetweenEvictionRunsMillis(userName));
pool.setSwallowedExceptionListener(new SwallowedExceptionLogger(log));
final Object old = managers.put(getPoolKey(userName), factory);
if (old != null) {
throw new IllegalStateException("Pool already contains an entry for this user/password: " + userName);
}
}
use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project logging-log4j2 by pwntester.
the class PoolingDriverConnectionSource method setupDriver.
private void setupDriver(final String connectionString, final PoolableConnectionFactoryConfig poolableConnectionFactoryConfig) throws SQLException {
//
// First, we'll create a ConnectionFactory that the
// pool will use to create Connections.
// We'll use the DriverManagerConnectionFactory,
// using the connect string passed in the command line
// arguments.
//
final Property[] properties = getProperties();
final char[] userName = getUserName();
final char[] password = getPassword();
final ConnectionFactory connectionFactory;
if (properties != null && properties.length > 0) {
if (userName != null || password != null) {
throw new SQLException("Either set the userName and password, or set the Properties, but not both.");
}
connectionFactory = new DriverManagerConnectionFactory(connectionString, toProperties(properties));
} else {
connectionFactory = new DriverManagerConnectionFactory(connectionString, toString(userName), toString(password));
}
//
// Next, we'll create the PoolableConnectionFactory, which wraps
// the "real" Connections created by the ConnectionFactory with
// the classes that implement the pooling functionality.
//
final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
if (poolableConnectionFactoryConfig != null) {
poolableConnectionFactoryConfig.init(poolableConnectionFactory);
}
//
// Now we'll need a ObjectPool that serves as the
// actual pool of connections.
//
// We'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice.
//
@SuppressWarnings("resource") final ObjectPool<PoolableConnection> // This GenericObjectPool will be closed on shutdown
connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
// Set the factory's pool property to the owning pool
poolableConnectionFactory.setPool(connectionPool);
loadDriver(poolingDriverClassName);
final PoolingDriver driver = getPoolingDriver();
if (driver != null) {
getLogger().debug("Registering DBCP pool '{}' with pooling driver {}: {}", poolName, driver, connectionPool);
driver.registerPool(poolName, connectionPool);
}
//
// Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
// to access our pool of Connections.
//
}
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 java-learn by laidu.
the class PoolApp method main.
public static void main(String[] args) throws Exception {
ProxyPooledFactory factory = new ProxyPooledFactory();
GenericObjectPoolConfig<ProxyInfo> poolConfig = new GenericObjectPoolConfig<>();
poolConfig.setMaxIdle(500);
poolConfig.setMaxTotal(20);
poolConfig.setMinIdle(150);
poolConfig.setMaxWaitMillis(50);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestWhileIdle(true);
poolConfig.setTestOnCreate(false);
poolConfig.setMaxWaitMillis(100);
poolConfig.setTimeBetweenEvictionRunsMillis(1000);
ObjectPool<ProxyInfo> pool = new GenericObjectPool<>(factory, poolConfig);
while (true) {
ProxyInfo proxyInfo = pool.borrowObject();
Thread.sleep(1000000);
pool.returnObject(proxyInfo);
System.out.println(pool.getNumActive());
System.out.println(pool.getNumIdle());
}
}
use of com.frameworkset.commons.pool2.impl.GenericObjectPool 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);
}
Aggregations