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

问题描述 投票:1回答:2

在多线程环境中使用带有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();
redis jedis spring-data-redis
2个回答
1
投票

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

redisTemplate.setEnableTransactionSupport(true);

0
投票

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

GitHub上查看更多信息

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

© www.soinside.com 2019 - 2024. All rights reserved.