Use Connection pool with Jedis

Abhijeet Behare picture Abhijeet Behare · Jun 15, 2017 · Viewed 7.6k times · Source

I am using Jedis to connect with a Redis server in a REST service.

When I am calling the web service I want to do operations like jedis.hmget , jedis.exits and hgetALL.

For example:

jedis.hmget("employee:data:" + emp_user_id, "employee_id").get(0);

The configuration that I am using for Redis is:

Jedis jedis;

    JedisShardInfo shardInfo;

    @PostConstruct
    public void init() {

        try {

            shardInfo = new JedisShardInfo(Config.getRedisHost(), Config.getRedisPort());
            shardInfo.setPassword(Config.getRedisPassword());
            jedis = new Jedis(shardInfo);
            jedis.select(2);
        //jedis.se
        } catch (Exception e) {
            logger.error("Exception in init ------- > " + e);
        }

    }

I know that Jedis is NOT thread safe. When I am using 1000 threads at once to call the service at that time I am getting an exception as Unexpected end of stream. I want to know Jedis pool is thread safe? Unable to find a specific solution for it.

Thanks. Any Help would be appreciated.

Answer

Radhika Mantri picture Radhika Mantri · Jun 19, 2017
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost", portno, 10000,
            "password");

See here: https://github.com/xetorthio/jedis/wiki/Getting-started