question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:41

github_iconTop GitHub Comments

4reactions
xetorthiocommented, Jul 28, 2011

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

2reactions
theoiliecommented, Nov 11, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix this java.lang.ClassCastException: [B cannot be ...
You are trying to cast your in.readObject() file to a List. This does not work in Java because the file is written as...
Read more >
How to fix java.lang.classcastexception cannot be cast to in Java
As name suggests ClassCastException in Java comes when we try to type cast an object and object is not of the type we...
Read more >
B cannot be cast to java.lang.String - MariaDB's JIRA
The last line raises an error: java.lang.ClassCastException: [B cannot be cast to java.lang.String. The output of our stored procedure is a ...
Read more >
B cannot be cast to java.util.List Multithreading environnement
java.lang.ClassCastException: [B cannot be cast to java.util.List. at redis.clients.jedis.Connection.getBinaryMultiBulkReply(Connection.java:202).
Read more >
How to Fix java.lang.classcastexception in Java?
ClassCastException in Java occurs when we try to convert the data type of entry into another. This is related to the type conversion...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found