use of org.apache.commons.pool2.impl.GenericObjectPoolConfig in project tomcat by apache.
the class BasicDataSource method createConnectionPool.
/**
* Creates a connection pool for this datasource. This method only exists
* so subclasses can replace the implementation class.
*
* This implementation configures all pool properties other than
* timeBetweenEvictionRunsMillis. Setting that property is deferred to
* {@link #startPoolMaintenance()}, since setting timeBetweenEvictionRunsMillis
* to a positive value causes {@link GenericObjectPool}'s eviction timer
* to be started.
* @param factory The connection factory
*/
protected void createConnectionPool(final PoolableConnectionFactory factory) {
// Create an object pool to contain our active connections
final GenericObjectPoolConfig config = new GenericObjectPoolConfig();
updateJmxName(config);
// Disable JMX on the underlying pool if the DS is not registered.
config.setJmxEnabled(registeredJmxName != null);
GenericObjectPool<PoolableConnection> gop;
if (abandonedConfig != null && (abandonedConfig.getRemoveAbandonedOnBorrow() || abandonedConfig.getRemoveAbandonedOnMaintenance())) {
gop = new GenericObjectPool<>(factory, config, abandonedConfig);
} else {
gop = new GenericObjectPool<>(factory, config);
}
gop.setMaxTotal(maxTotal);
gop.setMaxIdle(maxIdle);
gop.setMinIdle(minIdle);
gop.setMaxWaitMillis(maxWaitMillis);
gop.setTestOnCreate(testOnCreate);
gop.setTestOnBorrow(testOnBorrow);
gop.setTestOnReturn(testOnReturn);
gop.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
gop.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
gop.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
gop.setTestWhileIdle(testWhileIdle);
gop.setLifo(lifo);
gop.setSwallowedExceptionListener(new SwallowedExceptionLogger(log, logExpiredConnections));
gop.setEvictionPolicyClassName(evictionPolicyClassName);
factory.setPool(gop);
connectionPool = gop;
}
use of org.apache.commons.pool2.impl.GenericObjectPoolConfig in project spring-framework by spring-projects.
the class CommonsPool2TargetSource method createObjectPool.
/**
* Subclasses can override this if they want to return a specific Commons pool.
* They should apply any configuration properties to the pool here.
* <p>Default is a GenericObjectPool instance with the given pool size.
* @return an empty Commons {@code ObjectPool}.
* @see GenericObjectPool
* @see #setMaxSize
*/
protected ObjectPool createObjectPool() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(getMaxSize());
config.setMaxIdle(getMaxIdle());
config.setMinIdle(getMinIdle());
config.setMaxWaitMillis(getMaxWait());
config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
config.setBlockWhenExhausted(isBlockWhenExhausted());
return new GenericObjectPool(this, config);
}
use of org.apache.commons.pool2.impl.GenericObjectPoolConfig in project cachecloud by sohutv.
the class RedisStandaloneTest method testStandaloneExample.
@Test
public void testStandaloneExample() {
long appId = 10122;
JedisPool jedisPool = null;
// 使用默认配置
// jedisPool = ClientBuilder.redisStandalone(appId).build();
/**
* 使用自定义配置
*/
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 3);
poolConfig.setMinIdle(GenericObjectPoolConfig.DEFAULT_MIN_IDLE * 2);
poolConfig.setJmxEnabled(true);
poolConfig.setMaxWaitMillis(3000);
jedisPool = ClientBuilder.redisStandalone(appId).setPoolConfig(poolConfig).setTimeout(2000).build();
Jedis jedis = jedisPool.getResource();
jedis.setnx("key2", "5");
assertEquals("10", jedis.incrBy("key2", 5));
jedis.close();
}
use of org.apache.commons.pool2.impl.GenericObjectPoolConfig in project cachecloud by sohutv.
the class PoolBenchmark method withPool.
private static void withPool() throws Exception {
final JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, "foobared");
List<Thread> tds = new ArrayList<Thread>();
final AtomicInteger ind = new AtomicInteger();
for (int i = 0; i < 50; i++) {
Thread hj = new Thread(new Runnable() {
public void run() {
for (int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS; ) {
try {
Jedis j = pool.getResource();
final String key = "foo" + i;
j.set(key, key);
j.get(key);
j.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
tds.add(hj);
hj.start();
}
for (Thread t : tds) t.join();
pool.destroy();
}
use of org.apache.commons.pool2.impl.GenericObjectPoolConfig in project cachecloud by sohutv.
the class ShardedJedisPoolTest method shouldNotShareInstances.
@Test
public void shouldNotShareInstances() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(2);
ShardedJedisPool pool = new ShardedJedisPool(config, shards);
ShardedJedis j1 = pool.getResource();
ShardedJedis j2 = pool.getResource();
assertNotSame(j1.getShard("foo"), j2.getShard("foo"));
}
Aggregations