我应该总是在业务逻辑结束时调用 jedis 实例的关闭吗?
Should I always call close of jedis instance at the end of business logic?
我正在使用 jedis 作为与 spring 引导的 redis 连接。
jedis池配置如下:
@Configuration
@ConditionalOnClass(JedisPool.class)
@EnableConfigurationProperties(JedisProperties.class)
public class JedisAutoConfiguration {
private JedisProperties jedisProperties;
public JedisAutoConfiguration(JedisProperties jedisProperties) {
this.jedisProperties = jedisProperties;
}
@Bean
@ConditionalOnMissingBean(JedisPool.class)
public JedisPool jedisPool() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(jedisProperties.getPool().getMaxIdle());
poolConfig.setMaxTotal(jedisProperties.getPool().getMaxTotal());
poolConfig.setMaxWaitMillis(jedisProperties.getPool().getMaxWaitMillis() * 1000);
JedisPool jedisPool = new JedisPool(poolConfig, jedisProperties.getHost(), jedisProperties.getPort(),
jedisProperties.getTimeout()*1000, jedisProperties.getPassword(), 0);
return jedisPool;
}
}
每次需要连接redis的时候,我都会去jedis pool租一个资源:
@Service
public class UserService {
@Autowired
private JedisPool jedisPool;
public User FindUserFromCache(Integer id){
Jedis resource = jedisPool.getResource()
// business logic
// resource.close()
}
}
问题:
1.Should 我return 业务逻辑结束时资源池? spring 启动或 java 运行时可以自动处理这种情况吗?
2.If 资源没有关闭那么资源永远不会 return 到池中?
Should I return the resource to pool at the end of business logic?
是的。
您也可以使用 Java 的 try-with-resources 功能。
try (Jedis resource = jedisPool.getResource()) {
// business logic
}
Could spring boot or java runtime handle this situation automatically?
JedisPool 基于 Apache commons-pool2
项目,它既不是 spring(启动)的一部分,也不是核心 java。所以这些不能t/don自动处理这种情况。
If resource not closed then the resource would never return to the pool?
准确地说,如果资源不是returned,那么它永远不会回到池中。还有其他方法可以 return 将资源添加到池中。但是 close()
应该是用户友好的方式。
与其直接使用 Jedis 连接池,不如使用 RedisTemplate.
这样,您就不会为在业务逻辑代码中释放 Jedis 连接资源而烦恼。意味着更好的抽象。
此外,您将来可以在不触及您的业务逻辑的情况下将驱动程序从“Jedis”更改为更新的“Lettuce”。
在此处查看更多内容:https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis:template
我正在使用 jedis 作为与 spring 引导的 redis 连接。
jedis池配置如下:
@Configuration
@ConditionalOnClass(JedisPool.class)
@EnableConfigurationProperties(JedisProperties.class)
public class JedisAutoConfiguration {
private JedisProperties jedisProperties;
public JedisAutoConfiguration(JedisProperties jedisProperties) {
this.jedisProperties = jedisProperties;
}
@Bean
@ConditionalOnMissingBean(JedisPool.class)
public JedisPool jedisPool() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(jedisProperties.getPool().getMaxIdle());
poolConfig.setMaxTotal(jedisProperties.getPool().getMaxTotal());
poolConfig.setMaxWaitMillis(jedisProperties.getPool().getMaxWaitMillis() * 1000);
JedisPool jedisPool = new JedisPool(poolConfig, jedisProperties.getHost(), jedisProperties.getPort(),
jedisProperties.getTimeout()*1000, jedisProperties.getPassword(), 0);
return jedisPool;
}
}
每次需要连接redis的时候,我都会去jedis pool租一个资源:
@Service
public class UserService {
@Autowired
private JedisPool jedisPool;
public User FindUserFromCache(Integer id){
Jedis resource = jedisPool.getResource()
// business logic
// resource.close()
}
}
问题:
1.Should 我return 业务逻辑结束时资源池? spring 启动或 java 运行时可以自动处理这种情况吗?
2.If 资源没有关闭那么资源永远不会 return 到池中?
Should I return the resource to pool at the end of business logic?
是的。
您也可以使用 Java 的 try-with-resources 功能。
try (Jedis resource = jedisPool.getResource()) {
// business logic
}
Could spring boot or java runtime handle this situation automatically?
JedisPool 基于 Apache commons-pool2
项目,它既不是 spring(启动)的一部分,也不是核心 java。所以这些不能t/don自动处理这种情况。
If resource not closed then the resource would never return to the pool?
准确地说,如果资源不是returned,那么它永远不会回到池中。还有其他方法可以 return 将资源添加到池中。但是 close()
应该是用户友好的方式。
与其直接使用 Jedis 连接池,不如使用 RedisTemplate.
这样,您就不会为在业务逻辑代码中释放 Jedis 连接资源而烦恼。意味着更好的抽象。
此外,您将来可以在不触及您的业务逻辑的情况下将驱动程序从“Jedis”更改为更新的“Lettuce”。
在此处查看更多内容:https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis:template