检查连接Redis

use*_*702 5 java redis jedis

我正在使用 Jedis 客户端在 Java 中使用 Redis。我正在创建一个 JedisPool 并且想知道连接是否成功,有没有办法在不获取对象的情况下做到这一点?

Aug*_*ust 7

您可以尝试JedisJedisPool. JedisConnectionException如果尚未建立连接,将抛出A :

JedisPool pool = new JedisPool(...);
try {
    Jedis jedis = pool.getResource();
    // Is connected
} catch (JedisConnectionException e) {
    // Not connected
}
Run Code Online (Sandbox Code Playgroud)

  • 使用完该实例后,您应该返回资源。 (2认同)