Is JedisPool the recommend way connecting to redis

2,992 views
Skip to first unread message

Dong-xu Gu

unread,
Jun 27, 2014, 2:03:38 AM6/27/14
to jedis...@googlegroups.com
Hi

We have a tcp serevr and rest server both using Redis&Jedis as persistence layer.
Follow the documents from Jedis github page, we are using JedisPool connecting to redis server. (Just redis normal setup, no cluster at the moment)
So my questions are:
1. Is JedisPool a recommend way with a no cluster redis?
2. Follow the document, we repeate include this piece of code in every method of our Data Access Layer. It seems very inconvenient, is it really the correct way to doing this?
    public void addUser(String user) {
        Jedis jedis = this.jedisPool.getResource();
        try {
            ... do something here
        } catch (Exception e) {
            if (null != jedis) {
                this.jedisPool.returnBrokenResource(jedis);
                jedis = null;
            }
        } finally {
            if (null != jedis)
                this.jedisPool.returnResource(jedis);
        }
    }


Regards
LongkerDandy

Guy Lubovitch

unread,
Jun 29, 2014, 8:59:13 AM6/29/14
to jedis...@googlegroups.com
Yes this is the recommended way to open a connection to redis. even if you only use a single redis instance. the reason is that will provide a thread safe access to jedis instance. 

i would advise to use the access with sentinel and not directly access redis. reason is to allow future slave and more control. 

임정택

unread,
Jun 30, 2014, 7:58:57 PM6/30/14
to jedis...@googlegroups.com
Hello.

1. Yes, JedisPool would help you. It's thread-safe (Jedis instance itself is not thread-safe) so you can enjoy with multi-threaded environment.
2. Recent version it's just one of approaches.
You can try-with-resource with jdk 1.7 or higher, or try with finally jedis.close() with jdk 1.6 or lower.
Please see https://github.com/xetorthio/jedis/pull/563 for more details.

I'll update wiki when I have some leisure time.

Hope this helps.

Regards
Jungtaek Lim (HeartSaVioR)

2014년 6월 27일 금요일 오후 3시 3분 38초 UTC+9, Dong-xu Gu 님의 말:

Patrick

unread,
Dec 9, 2014, 4:05:13 AM12/9/14
to jedis...@googlegroups.com
Alternatively you may refactor out the try finally stuff like this:
   /**
     * The abstract RedisCommand.
     */
    public abstract class RedisCommand {
        public abstract Object doWith(Jedis redisClient);
    }
----- 8< -----
protected Object doWithClient(RedisCommand command) {
        Jedis jedis = pool.getResource();
        try {
           
            return command.doWith(jedis);
           
        } catch (JedisException je) {
            if (jedis != null) {
                pool.returnBrokenResource(jedis);
                jedis = null; // very important see below...
            }
            return null;
        } finally {
            if (jedis != null) {
                pool.returnResource(jedis);
            }
        }
    }
--- 8< ---
you would use it like this
for example to access the length of a list:
...
public Long length() {
        return (Long) doWithClient(new RedisCommand() {
            public Object doWith(Jedis redisClient) {
                return redisClient.llen("myListName".getBytes());
            }
        });
    }
...
of course this would look much nicer with java8 Lambdas...
Reply all
Reply to author
Forward
0 new messages