jedis: ClassCastException - [B cannot be cast to java.lang.Long

I am using Jesque which uses Jedis as its Redis client, so I am not entirely sure if this is an issue in Jedis or Jesque.

I am receiving this error:

java.lang.ClassCastException

[B cannot be cast to java.lang.Long
    at redis.clients.jedis.Connection.getIntegerReply(Connection.java:178)
    at redis.clients.jedis.Jedis.incr(Jedis.java:605)
    at net.greghaines.jesque.worker.WorkerImpl.success(WorkerImpl.java:520)
    at net.greghaines.jesque.worker.WorkerImpl.execute(WorkerImpl.java:503)
    at net.greghaines.jesque.worker.WorkerImpl.process(WorkerImpl.java:465)
    at net.greghaines.jesque.worker.WorkerImpl.poll(WorkerImpl.java:405)
    at net.greghaines.jesque.worker.WorkerImpl.run(WorkerImpl.java:216)
    at java.lang.Thread.run(Thread.java:679)

I read that Jedis isnt thread-safe and in fact I dont try to re-use any Jedis clients in my Threads, I create new instances (I am refactoring this to use JedisPool).

The exception is not consistently reproducible.

About this issue

  • Original URL
  • State: closed
  • Created 13 years ago
  • Comments: 41

Commits related to this issue

Most upvoted comments

I really don’t know nothing about Gresque and how it uses Jedis underneath. But I am almost 100% sure that this error is because of reusing jedis instance from different threads. If you are using JedisPool, make sure to return Jedis either using returnResource or returnBrokenResource. Also if you are opening a pipeline, remember to close it before returning the resource to the pool. Try to find a script that reproduce this error and send it to me!

On Tue, Jul 26, 2011 at 2:43 PM, ruckus < reply@reply.github.com>wrote:

I am using Gresque which uses Jedis as its Redis client, so I am not entirely sure if this is an issue in Jedis or Gresque.

I am receiving this error:

java.lang.ClassCastException

[B cannot be cast to java.lang.Long at redis.clients.jedis.Connection.getIntegerReply(Connection.java:178) at redis.clients.jedis.Jedis.incr(Jedis.java:605) at net.greghaines.jesque.worker.WorkerImpl.success(WorkerImpl.java:520) at net.greghaines.jesque.worker.WorkerImpl.execute(WorkerImpl.java:503) at net.greghaines.jesque.worker.WorkerImpl.process(WorkerImpl.java:465) at net.greghaines.jesque.worker.WorkerImpl.poll(WorkerImpl.java:405) at net.greghaines.jesque.worker.WorkerImpl.run(WorkerImpl.java:216) at java.lang.Thread.run(Thread.java:679)

I read that Jedis isnt thread-safe and in fact I dont try to re-use any Jedis clients in my Threads, I create new instances (I am refactoring this to use JedisPool).

The exception is not consistently reproducible.

Reply to this email directly or view it on GitHub: https://github.com/xetorthio/jedis/issues/186

I got the exact same error a couple times an hour when I used the same connection in a runnable called 20ms later. This is what I originally had.

try (Jedis jedis = pool.getResource()) {
    jedis.hgetAll("test").forEach((key, val) -> {
        // Project-specific runnable that's executed 20ms later (some code is redacted)
        @Override public void run() {
            jedis.hgetAll("test2").forEach((key2, val2) -> { // This is the line that had the error
                // Stuff that uses these keys/values
            }
        }
    }
} catch (Exception e) {/*Log error*/}

I changed it to this and it’s been working flawlessly for a few days now.

try (Jedis jedis = pool.getResource()) {
    jedis.hgetAll("test").forEach((key, val) -> {
        // Project-specific runnable that's executed 20ms later (some code is redacted)
        @Override public void run() {
            try (Jedis jedis2 = pool.getResource()) {
                jedis2.hgetAll("test2").forEach((key2, val2) -> {
                    // Stuff that uses these keys/values
                }
            } catch (Exception e2) {/*Log error*/}
        }
    }
} catch (Exception e) {/*Log error*/}

@hozouhing @AmaranthLIS I’m not sure what your situation is, but if you’re using the same connection more than once then I would try getting a new one. It seems like (at least in my case), it’s a usage error rather than a bug in Jedis.

I wasn’t able to reproduce it easily. It happens on my server after a few hours of continuous running. I guess it occurs because I’m not closing disconnected clients but try to reconnect them: if (connection == null) { connection = jedisPool.getResource(); } if (!connection.isConnected()) { connection.connect(); }

@SKras @guperrot returnXXXResource has been deprecated since Jedis 2.5 onwards. Please use jedis.close() in your finally block instead.

@SKras @guperrot is correct in his comment. Seems like you’re returning broken jedis instances to the pool. Again, use jedis.close() to return instances propertly.

+1 changing the pool.returnResource(jedis) to jedis.close() fixed my issue as well

Yes, change the pool.returnResource(jedis) to jedis.close() fixed the problem for me.