Search in sources :

Example 41 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project cachecloud by sohutv.

the class ShardedJedisPoolTest method checkJedisIsReusedWhenReturned.

@Test
public void checkJedisIsReusedWhenReturned() {
    ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards);
    ShardedJedis jedis = pool.getResource();
    jedis.set("foo", "0");
    jedis.close();
    jedis = pool.getResource();
    jedis.incr("foo");
    jedis.close();
    pool.destroy();
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ShardedJedis(redis.clients.jedis.ShardedJedis) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) Test(org.junit.Test)

Example 42 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project cachecloud by sohutv.

the class ShardedJedisPoolTest method returnResourceShouldResetState.

@Test
public void returnResourceShouldResetState() throws URISyntaxException {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380")));
    shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6379")));
    ShardedJedisPool pool = new ShardedJedisPool(config, shards);
    ShardedJedis jedis = pool.getResource();
    jedis.set("pipelined", String.valueOf(0));
    jedis.set("pipelined2", String.valueOf(0));
    ShardedJedisPipeline pipeline = jedis.pipelined();
    pipeline.incr("pipelined");
    pipeline.incr("pipelined2");
    jedis.resetState();
    pipeline = jedis.pipelined();
    pipeline.incr("pipelined");
    pipeline.incr("pipelined2");
    List<Object> results = pipeline.syncAndReturnAll();
    assertEquals(2, results.size());
    jedis.close();
    pool.destroy();
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ShardedJedis(redis.clients.jedis.ShardedJedis) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) ArrayList(java.util.ArrayList) ShardedJedisPipeline(redis.clients.jedis.ShardedJedisPipeline) JedisShardInfo(redis.clients.jedis.JedisShardInfo) URI(java.net.URI) Test(org.junit.Test)

Example 43 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.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();
}
Also used : Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) JedisPool(redis.clients.jedis.JedisPool)

Example 44 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project cachecloud by sohutv.

the class JedisPoolTest method checkResourceIsCloseable.

@Test
public void checkResourceIsCloseable() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
    Jedis jedis = pool.getResource();
    try {
        jedis.set("hello", "jedis");
    } finally {
        jedis.close();
    }
    Jedis jedis2 = pool.getResource();
    try {
        assertEquals(jedis, jedis2);
    } finally {
        jedis2.close();
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) Test(org.junit.Test)

Example 45 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project cachecloud by sohutv.

the class JedisPoolTest method returnResourceShouldResetState.

@Test
public void returnResourceShouldResetState() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
    Jedis jedis = pool.getResource();
    try {
        jedis.set("hello", "jedis");
        Transaction t = jedis.multi();
        t.set("hello", "world");
    } finally {
        jedis.close();
    }
    Jedis jedis2 = pool.getResource();
    try {
        assertTrue(jedis == jedis2);
        assertEquals("jedis", jedis2.get("hello"));
    } finally {
        jedis2.close();
    }
    pool.destroy();
    assertTrue(pool.isClosed());
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) Test(org.junit.Test)

Aggregations

GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)127 Test (org.junit.Test)77 Jedis (redis.clients.jedis.Jedis)41 ShardedJedis (redis.clients.jedis.ShardedJedis)40 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)40 JedisPool (redis.clients.jedis.JedisPool)31 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)19 ArrayList (java.util.ArrayList)15 JedisShardInfo (redis.clients.jedis.JedisShardInfo)15 URI (java.net.URI)11 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)9 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 BaseTest (com.sohu.tv.test.base.BaseTest)6 IOException (java.io.IOException)6 Transaction (redis.clients.jedis.Transaction)6 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)5 PooledObject (org.apache.commons.pool2.PooledObject)5 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)5 Test (org.testng.annotations.Test)5