最近项目的Redis遇到一个问题,报的错是
java.lang.ClassCastException: java.util.ArrayList cannot be cast to....
项目运行中突然有个查询出了问题,推测是数据量太大导致buffer不够用之类的原因,需要把坏掉的connection关闭。
原来出错的代码是在这个查询的方法
public Set<V> sget(final String redisKey) {
if (StringUtils.isEmpty(redisKey)){
return null;
}
SetOperations setOper = redisTemplate.opsForSet();
return setOper.members(redisKey);
}
现在问题来了,方法里没有connection,如何关闭这个方法查询错误的连接(不然会一直报错,可能这个连接的buffer没有被清理),试着重写setOper.members并取得连接关闭,都会出错,最后成功的方法是用Spring提供的工具类RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
PS 我们项目是没有开事物的
public Set<V> sget(final String redisKey) {
if (StringUtils.isEmpty(redisKey)){
return null;
}
Set members = null;
try {
SetOperations setOper = redisTemplate.opsForSet();
members = setOper.members(redisKey);
} catch (Exception e){
e.printStackTrace();
RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
}
return members;
}
这个方法是当事物进行时,先不关闭,如果事物结束了,就获取当前线程的连接,并关闭
源码如下:
相关文章可以看这篇博客
https://hellojimmy.iteye.com/blog/1197543
但是这篇博客没有用redisTemplate,且
returnBrokenResource(jedis); 已经被close方法取代了