Search in sources :

Example 6 with DefaultPooledObject

use of org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject in project cachecloud by sohutv.

the class JedisPoolTest method returnResourceDestroysResourceOnException.

@Test
public void returnResourceDestroysResourceOnException() {
    class CrashingJedis extends Jedis {

        @Override
        public void resetState() {
            throw new RuntimeException();
        }
    }
    final AtomicInteger destroyed = new AtomicInteger(0);
    class CrashingJedisPooledObjectFactory implements PooledObjectFactory<Jedis> {

        @Override
        public PooledObject<Jedis> makeObject() throws Exception {
            return new DefaultPooledObject<Jedis>(new CrashingJedis());
        }

        @Override
        public void destroyObject(PooledObject<Jedis> p) throws Exception {
            destroyed.incrementAndGet();
        }

        @Override
        public boolean validateObject(PooledObject<Jedis> p) {
            return true;
        }

        @Override
        public void activateObject(PooledObject<Jedis> p) throws Exception {
        }

        @Override
        public void passivateObject(PooledObject<Jedis> p) throws Exception {
        }
    }
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
    pool.initPool(config, new CrashingJedisPooledObjectFactory());
    Jedis crashingJedis = pool.getResource();
    try {
        crashingJedis.close();
    } catch (Exception ignored) {
    }
    assertEquals(destroyed.get(), 1);
}
Also used : Jedis(redis.clients.jedis.Jedis) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) PooledObjectFactory(org.apache.commons.pool2.PooledObjectFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) URISyntaxException(java.net.URISyntaxException) JedisException(redis.clients.jedis.exceptions.JedisException) InvalidURIException(redis.clients.jedis.exceptions.InvalidURIException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) Test(org.junit.Test)

Aggregations

PooledObject (org.apache.commons.pool2.PooledObject)3 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)3 DefaultPooledObject (org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject)3 URISyntaxException (java.net.URISyntaxException)2 SQLException (java.sql.SQLException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 PooledConnection (javax.sql.PooledConnection)2 PooledObjectFactory (org.apache.commons.pool2.PooledObjectFactory)2 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)2 Test (org.junit.Test)2 Jedis (redis.clients.jedis.Jedis)2 JedisPool (redis.clients.jedis.JedisPool)2 InvalidURIException (redis.clients.jedis.exceptions.InvalidURIException)2 JedisException (redis.clients.jedis.exceptions.JedisException)2 Connection (java.sql.Connection)1 Connection (javax.jms.Connection)1 JMSException (javax.jms.JMSException)1 QueueConnection (javax.jms.QueueConnection)1 TopicConnection (javax.jms.TopicConnection)1 ObjectName (javax.management.ObjectName)1