Properly close jedis connection in case of exceptions

This commit is contained in:
Nelson Rodrigues
2014-07-28 11:45:34 -07:00
parent d52cc06b72
commit c81bdc0849

View File

@@ -126,19 +126,23 @@ public class JedisSentinelPool extends Pool<Jedis> {
log.fine("Connecting to Sentinel " + hap); log.fine("Connecting to Sentinel " + hap);
Jedis jedis = null;
try { try {
Jedis jedis = new Jedis(hap.getHost(), hap.getPort()); jedis = new Jedis(hap.getHost(), hap.getPort());
if (master == null) { if (master == null) {
master = toHostAndPort(jedis master = toHostAndPort(jedis
.sentinelGetMasterAddrByName(masterName)); .sentinelGetMasterAddrByName(masterName));
log.fine("Found Redis master at " + master); log.fine("Found Redis master at " + master);
jedis.disconnect();
break outer; break outer;
} }
} catch (JedisConnectionException e) { } catch (JedisConnectionException e) {
log.warning("Cannot connect to sentinel running @ " + hap log.warning("Cannot connect to sentinel running @ " + hap
+ ". Trying next one."); + ". Trying next one.");
} finally {
if (jedis != null) {
jedis.close();
}
} }
} }