How Jedis Pool works?

ipkiss picture ipkiss · Jan 20, 2014 · Viewed 53.4k times · Source

I'm using Jedis pool to manage connections to Redis server. An example code of mine as follows:

public Set<String> getTopArticleList(int start, int end) {
    Set<String> list = null;
    Jedis j = JedisFactory.getInstance().getJedisPool().getResource();
    Pipeline pipe = j.pipelined();
    try {
        // do stuff with redis
        pipe.sync();
    } catch (JedisConnectionException jex) {
        JedisFactory.getInstance().getJedisPool().returnBrokenResource(j);
    } finally {
        JedisFactory.getInstance().getJedisPool().returnResource(j);
    }
    return list;
}

Code to create and retrieve the Jedis pool:

class JedisFactory {
    private static JedisPool jedisPool;
    public JedisFactory() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        jedisPool = new JedisPool(
            poolConfig,
            RedisDBConfig.HOST,
            RedisDBConfig.PORT,
            RedisDBConfig.TIMEOUT,
            RedisDBConfig.PASSWORD
        );
    }

    public JedisPool getJedisPool() {
        return jedisPool;
    }

    public static JedisFactory getInstance() {
        if (instance == null) {
            instance = new JedisFactory();
        }
        return instance;
    }
 }            

The problem is that after reaching the number of limited connections, the web cannot be accessed anymore. Am I doing something wrong?

Answer

Alexandre L Telles picture Alexandre L Telles · Jan 20, 2014

You haven't configured the maxTotal size of the pool, and the default value is only 8. You could change the JedisFactory constructor to:


    public JedisFactory() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(128);
        jedisPool = new JedisPool(poolConfig, RedisDBConfig.HOST, RedisDBConfig.PORT, RedisDBConfig.TIMEOUT, RedisDBConfig.PASSWORD);
    }

Beware also of the default value of the WhenExhaustedAction (WHEN_EXHAUSTED_BLOCK), as it may not be your desired behavior.

Jedis uses Apache Commons Pool, and you can read about it's parameters here: GenericObjectPool