4


I got "java.lang.ClassCastException: java.lang.Long cannot be cast to [B" while running this code:

JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(10);
config.setMinIdle(1);
config.setMaxWaitMillis(30000);
JedisPool jedisPool = new JedisPool(config, "localhost", 6379);

Jedis jedis = null;
jedis = jedisPool.getResource();

String msisdn = "3331122333";
Long balance = new Long(1000);
int balanceValidity = 30;

Transaction t = jedis.multi();
t.watch(msisdn);
t.set(msisdn, balance.toString());
t.expire(msisdn, balanceValidity);
t.exec();

Everythings works fine running this code:

Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();

        jedis.watch(msisdn);
        jedis.set(msisdn, balance.toString());      
        jedis.expire(msisdn, balanceValidity);
        jedis.publish("myChannel", msisdn + " " + balance.toString());

    } finally {
        if (jedis != null)
            jedis.close();
    }

These the used vesions:

  • Redis 3.2.4
  • Jedis client 2.9.0
  • JDK 1.6
Alessio Fiore
  • 467
  • 1
  • 7
  • 18

2 Answers2

5

Jedis is not thread safe.

get the jedis instance every time from the pool when you do some operation. and after that close jedis instance so that it goes back to the pool

user3156108
  • 51
  • 1
  • 3
  • Just adding to user3156108 answer: Close the Jedis instance when you are done with operations. Finally, it should be returned to the pool. If needed close the pool as well. – Ranjan Apr 10 '19 at 06:47
  • sounds expensive to close the entire connection everytime, but I am getting this error as well. mostly when I have a breakpoint in development. feels like I should be able to recover but this error keeps being thrown. – mjs Sep 19 '20 at 19:43
  • I am using jedis cluster and seeing this error. There is no need to get resource when using cluster... don't know what is best thing to do – ante.sabo Jun 07 '21 at 23:20
0

The watch command inside the transaction is not supported by redis. Add the watch command before the transaction using the jedis object.

https://github.com/redis/jedis/pull/2033