一、JedisPool释放连接
在使用JedisPool连接池时,连接需在使用结束后释放,不然会导致连接池泄漏,最终无法提供可用连接。释放连接即将连接放回连接池中,以便下次使用。
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
try(Jedis jedis = pool.getResource()){
// do something
} finally {
if (pool != null) {
pool.close();
}
}
上述代码中,finally语句块用于释放连接。如果没有释放连接,连接池中的可用连接数量会减少。
二、JedisPoolConfig
JedisPoolConfig配置对象包含连接池中连接的各种属性,如最大连接数、最大空闲连接数、连接超时时间等。
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(8); // 设置最大连接数
poolConfig.setMaxIdle(8); // 设置最大空闲连接数
poolConfig.setMinIdle(0); // 设置最小空闲连接数
poolConfig.setMaxWaitMillis(10000); // 设置超时时间
JedisPool pool = new JedisPool(poolConfig, "localhost");
三、JedisPool自动重连
JedisPool自动重连是指当连接池中的连接因网络原因或其他原因关闭时,连接池会自动尝试重新建立连接,避免因连接关闭导致的无法提供可用连接的问题。
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
try(Jedis jedis = pool.getResource()){
// do something
} catch (Exception e) {
// 连接异常,等待10秒后重试
Thread.sleep(10000);
}
四、JedisPool连接耗尽
当连接池中的连接都被占用时,如果再次请求连接,则会出现连接耗尽的情况。可通过设置等待时间或设置异常处理方式解决连接耗尽问题。
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
try(Jedis jedis = pool.getResource()){
// do something
} catch (JedisException e) {
if (e instanceof JedisConnectionException) {
// 连接异常,等待10秒后重试
Thread.sleep(10000);
// 继续尝试获取连接
} else {
// 抛出异常,不再继续尝试获取连接
throw e;
}
}
五、JedisPoolConfig详解
JedisPoolConfig包含一些配置属性,如最大连接数、最大空闲连接数等,用于控制连接池中连接对象的生产和消费。以下是一些常用的配置属性:
- maxTotal:最大连接数
- maxIdle:最大空闲连接数
- minIdle:最小空闲连接数
- maxWaitMillis:最大等待时间,单位毫秒
- testOnBorrow:获取连接时是否检查连接的有效性
- testOnReturn:返回连接时是否检查连接的有效性
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(8);
poolConfig.setMaxIdle(8);
poolConfig.setMinIdle(0);
poolConfig.setMaxWaitMillis(10000);
poolConfig.setTestOnBorrow(true); // 获取连接时检查有效性
poolConfig.setTestOnReturn(true); // 返回连接时检查有效性
JedisPool pool = new JedisPool(poolConfig, "localhost");
六、JedisPool获取实例失败
获取Jedis实例时,可能出现获取失败的情况,导致无法使用连接池的功能。可通过捕获异常并重新尝试获取实例来解决。
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
Jedis jedis = null;
boolean success = false;
int retryTimes = 0;
while (!success && retryTimes < 3) {
try {
jedis = pool.getResource();
success = true;
} catch (Exception e) {
retryTimes++;
Thread.sleep(1000);
}
}
if (jedis == null) {
// 获取连接失败,抛出异常
throw new JedisException("Failed to get Jedis instance");
}
// 使用连接对象
jedis.set("key", "value");
七、JedisPool哨兵cant against
JedisPool在哨兵模式下,可能会出现“cant against”错误。该错误通常是由于哨兵服务没能获取到主节点引起的。此时,需要重新获取连接。
JedisPool pool = new JedisSentinelPool("mymaster", sentinels, config, timeout, password);
try(Jedis jedis = pool.getResource()) {
// do something
} catch (Exception e) {
// 响应错误,重新获取连接
jedis = pool.getResource();
// 使用连接对象
jedis.set("key", "value");
}
八、JedisPool Timeout
在使用JedisPool连接池时,由于网络等原因,可能会出现超时问题。Jedis提供了严格的超时设置,可在连接池创建时进行设置。
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost", 6379, 10000);
Jedis jedis = pool.getResource();
jedis.set("key", "value");
jedis.expire("key", 60);
以上代码中,超时时间设置为10秒。如果在10秒内获取或使用连接失败,则会抛出JedisConnectionException异常。
九、JedisPool.getResource()
JedisPool.getResource()用于获取连接,它提供了一种简单、方便的获取连接的方式,并确保可用连接的数量不超过最大连接数。如果超出最大连接数,将会等待,直到有连接可用。每次获取连接时,都会从连接池中选择一个最先入池的空闲连接。
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
try(Jedis jedis = pool.getResource()){
// do something
}
以上代码中,我们使用JedisPool.getResource()方法获取连接对象Jedis。使用完毕后,Jedis会自动返回连接池。
原创文章,作者:RGKI,如若转载,请注明出处:https://www.506064.com/n/148459.html