我试图用spring Boot实现redis,我在本地主机上随机遇到下面的异常:
redis.clients.jedis.exceptions.JedisConnectionException:无法获取 来自池的资源;
我已经尝试过JedisPoolConfig的各种属性组合。但他们都没有帮助不确定哪里出错了。
@Configuration
public class RedisConfigurationSetup {
@Bean
public RedisConnectionFactory jedisConnectionFactory() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(10000);
poolConfig.setMinIdle(1000);
poolConfig.setMaxIdle(-1);
poolConfig.setMaxWaitMillis(500);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
JedisConnectionFactory ob = new JedisConnectionFactory(poolConfig);
ob.setHostName("127.0.0.1");
ob.setPort(6379);
ob.setUsePool(true);
return ob;
}
@SuppressWarnings({ "rawtypes" })
@Bean(name = "redisTemplate")
public RedisTemplate stringRedisTemplate() {
RedisTemplate rt = new RedisTemplate();
rt.setConnectionFactory(jedisConnectionFactory());
rt.setEnableTransactionSupport(true);
return rt;
}
}
public class GeoLocationCacheServiceImpl implements GeoLocationCacheService {
@Autowired
@Qualifier("redisTemplate")
RedisTemplate geoObjectRedisTemplate;
@Override
public void saveUpdateGeoLoc(GeoObject geoObject) {
if (geoObject != null) {
// Some Business logics
geoObjectRedisTemplate.opsForValue().set(geoObject.getObjectID(), geoObject);
// Some Business logics
}
}
}
答案 0 :(得分:0)
因为rt.setEnableTransactionSupport(true); 从源代码中,我们可以看到
if (!enableTransactionSupport) {
RedisConnectionUtils.releaseConnection(conn, factory);
}