parpar8090
parpar8090

Reputation: 76

JedisPool vs JedisPooled

Looking at the Jedis Getting Started, I understand it's better to use connection pools to be threadsafe. To do so, I would need a JedisPool and a try-with-resources statement like so:

try (Jedis jedis = pool.getResource()) {
  jedis.set("hello", "world");
}

But then there's JedisPooled, which is the same as JedisPool just without the try-with-resources.

JedisPooled jedis = new JedisPooled("localhost", 6379);
jedis.sadd("hellow", "world");

So the question is, is there any other difference between JedisPool/Pooled, and why should I prefer JedisPool over JedisPooled (or vice versa)?

Upvotes: 3

Views: 5454

Answers (2)

signonsridhar
signonsridhar

Reputation: 165

My understanding is that each command in JedisPooled

JedisPooled jedis = new JedisPooled("localhost", 6379);
jedis.sadd("hellow", "world");//fetch and release the resource
jedis.sadd("hellow", "what");//fetch and release the resource

will get the resource from the pool and release the resource into the pool without having to do try-with-resources. JedisPool needs explicit try-with-resources

JedisPool pool = new JedisPool("localhost", 6379);
try (Jedis jedis = pool.getResource()) {
    jedis.set("foo", "bar"); // jedis resource is used
    System.out.println(jedis.get("foo")); // same jedis resource is used
}//here resource is released

REF : https://redis.io/docs/clients/java/

Upvotes: 3

Mαx Φ
Mαx Φ

Reputation: 134

With JedisPool you can do transactions, e.g., something like this:

try (Jedis jedis = pool.getResource()) {
  Transaction t = jedis.multi();
  t.sadd("planets", "Venus");
  t.sadd("planets", "Mars");
  t.exec();
}

It doesn't seem like you can do anything similar with JedisPooled.

Probably, there's more to that, but that's what I've stumbled on by myself.

Hope that helps!

Upvotes: 2

Related Questions