RedisSystemException:java.lang.ClassCastException:[B不能强制转换为java.lang.Long

时间:2018-06-01 07:57:48

标签: redis jedis spring-data-redis

在多线程环境中使用带有spring-data-redis的jedis时,我遇到了这个异常:

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版本: 2.9.0

spring-data-redis版本: 1.8.12.RELEASE

redis服务器版本: 3.0.6

我的客户端Java代码:

// 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();

2 个答案:

答案 0 :(得分:1)

最后,在阅读spring-data的源代码之后,我通过删除以下行来解决了我的问题:

redisTemplate.setEnableTransactionSupport(true);

答案 1 :(得分:0)

您应该共享池并在每个线程中从中获取不同的Jedis。

详情请见GitHub

这是Samebug上的重复模式。尝试search with your stack trace