【问题记录2】spring boot redis @Cacheable报错InvalidDataAccessApiUsageException: EXECABORT Transaction disca

错误:

org.springframework.dao.InvalidDataAccessApiUsageException: EXECABORT Transaction discarded because of previous errors.; nested exception is redis.clients.jedis.exceptions.JedisDataException: EXECABORT Transaction discarded because of previous errors.
at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:64) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:41) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:37) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:37) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:241) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at org.springframework.data.redis.connection.jedis.JedisConnection.exec(JedisConnection.java:793) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
at org.springframework.data.redis.core.CloseSuppressingInvocationHandler.invoke(CloseSuppressingInvocationHandler.java:57) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at com.sun.proxy.$Proxy107.exec(Unknown Source) ~[na:na]
at org.springframework.data.redis.cache.RedisCache$RedisCachePutCallback.doInRedis(RedisCache.java:795) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at org.springframework.data.redis.cache.RedisCache$RedisCachePutCallback.doInRedis(RedisCache.java:767) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at org.springframework.data.redis.cache.RedisCache$AbstractRedisCacheCallback.doInRedis(RedisCache.java:565) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:207) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:169) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:157) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at org.springframework.data.redis.cache.RedisCache.put(RedisCache.java:226) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at org.springframework.data.redis.cache.RedisCache.put(RedisCache.java:194) ~[spring-data-redis-1.8.10.RELEASE.jar:na]
at org.springframework.cache.interceptor.AbstractCacheInvoker.doPut(AbstractCacheInvoker.java:85) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.cache.interceptor.CacheAspectSupport$CachePutRequest.apply(CacheAspectSupport.java:781) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:414) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:324) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.14.RELEASE.jar:4.3.14.RELEASE]
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) ~[spring-aop-4.3.14.RELEASE.jar:4.3.14.RELEASE]

问题说明:spring boot(1.5.10版本)集成redis使用cache的时候,会报上述错误。redis配置如下(配置来源:网络):



debug跟踪到问题出现位置:


问题解决过程中发现不进行redis配置,直接开启@EnableCaching的情况下也可以使用@Cacheable等注解并且底层使用的cacheManger也是RedisCacheManger,并且此种情况下可以正常使用。通过对上图中的this对象(实际是一个Intercetpor)在两种情况下的属性对比,发现在异常情况下cacheManger的属性值usePrefix为false,而正常情况下为true。


经验证,异常原因就是在这个地方,修改RedisConfig类中的cacheManger对象即可:

原因分析:prefix作为关键字,继续向下debug,最终找到以下方法:


当usePrefix=false时prefix为null(可以查看cacheMacger对象),此处会执行zAdd方法,而且zAdd方法的其中一个参数为double类型,而默认配置下的redis不支持非String类型数据的操作,因此问题根源即在redis的配置上,如果想要redis支持非String类型数据,需要设置value的序列化方式(网上随便找了一下,试了两个没有成功)。简单处理就是直接设置usePrefix为true。至于其它更详细深入的原理分析,我个人也没有深入,如果有大神可以深入细致的讲解一番的话就好了。。。


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值