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

不问归期 提交于 2021-01-28 03:05:40

问题


I meet this exception when using jedis with spring-data-redis in multi threading environment:

org.springframework.data.redis.RedisSystemException: Unknown redis exception; nested exception is java.lang.ClassCastException: [B cannot be cast to java.lang.Long
        at org.springframework.data.redis.FallbackExceptionTranslationStrategy.getFallback(FallbackExceptionTranslationStrategy.java:48)
        at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:38)
        at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:241)
        at org.springframework.data.redis.connection.jedis.JedisConnection.rPush(JedisConnection.java:1705)
        at org.springframework.data.redis.core.DefaultListOperations$14.doInRedis(DefaultListOperations.java:187)
        at org.springframework.data.redis.core.DefaultListOperations$14.doInRedis(DefaultListOperations.java:184)
        at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:207)
        at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:169)
        at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:91)
        at org.springframework.data.redis.core.DefaultListOperations.rightPush(DefaultListOperations.java:184)
        at XXXXXXXXXXXXXXX
Caused by: java.lang.ClassCastException: [B cannot be cast to java.lang.Long
        at redis.clients.jedis.Connection.getIntegerReply(Connection.java:265)
        at redis.clients.jedis.BinaryJedis.rpush(BinaryJedis.java:1053)
        at org.springframework.data.redis.connection.jedis.JedisConnection.rPush(JedisConnection.java:1703)
    ... 19 common frames omitted

jedis version: 2.9.0

spring-data-redis version: 1.8.12.RELEASE

redis server version: 3.0.6

My Client Java Code:

// Init JedisConnectionFactory
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisPoolConfig.setMaxTotal(maxActive);
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWait);
jedisPoolConfig.setTestOnBorrow(true);

jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
jedisConnectionFactory.setHostName(host);
jedisConnectionFactory.setPort(port);
jedisConnectionFactory.setTimeout(timeout);
jedisConnectionFactory.setPassword(password);
jedisConnectionFactory.afterPropertiesSet();

// Create RedisTemplate
redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(jedisConnectionFactory);
redisTemplate.setEnableTransactionSupport(true);
StringRedisSerializer serializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(serializer);
redisTemplate.setValueSerializer(serializer);
redisTemplate.setHashKeySerializer(serializer);
redisTemplate.setHashValueSerializer(serializer);
redisTemplate.afterPropertiesSet();

回答1:


Finally, I solved my problem by remove this line, after I read the source code of spring-data:

redisTemplate.setEnableTransactionSupport(true);



回答2:


You should share the pool and get a different Jedis from it in every thread.

See more on GitHub

This is a recurring pattern on Samebug. Try to search with your stack trace.



来源:https://stackoverflow.com/questions/50638699/redissystemexception-java-lang-classcastexception-b-cannot-be-cast-to-java-la

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!