1717import redis .clients .jedis .util .JedisClusterHashTag ;
1818
1919import java .sql .SQLException ;
20- import java .util .HashMap ;
2120import java .util .Map ;
2221import java .util .Set ;
2322
@@ -27,7 +26,6 @@ public class RedisJedisClusterClient extends RedisClientBase {
2726
2827
2928 private final JedisCluster jedisCluster ;
30- private final Map <String , RedisJedisClient > jedisClusterNodes = new HashMap <>();
3129
3230 public RedisJedisClusterClient (@ NotNull RedisJedisClusterURI uri ) throws SQLException {
3331 try {
@@ -58,18 +56,20 @@ public Object execute(@NotNull RedisQuery query) throws SQLException {
5856
5957 private synchronized Object execute (@ NotNull HostAndPort nodeHostAndPort ,
6058 @ NotNull RedisQuery query ) throws SQLException {
61- String nodeKey = JedisClusterInfoCache .getNodeKey (nodeHostAndPort );
62- RedisJedisClient jedisNode = jedisClusterNodes .get (nodeKey );
63- if (jedisNode == null ) {
64- Map <String , ConnectionPool > nodes = jedisCluster .getClusterNodes ();
65- ConnectionPool nodePool = nodes .get (nodeKey );
66- Connection nodeConnection = nodePool != null ? nodePool .getResource () : null ;
67- if (nodeConnection == null )
68- throw new SQLException (String .format ("Cluster node not found: %s" , nodeHostAndPort ));
69- jedisNode = new RedisJedisClient (nodeConnection );
70- jedisClusterNodes .put (nodeKey , jedisNode );
59+ Connection nodeConnection = getNodeConnection (nodeHostAndPort );
60+ try (RedisJedisClient nodeClient = new RedisJedisClient (nodeConnection )) {
61+ return nodeClient .execute (query );
7162 }
72- return jedisNode .execute (query );
63+ }
64+
65+ private synchronized @ NotNull Connection getNodeConnection (@ NotNull HostAndPort nodeHostAndPort ) throws SQLException {
66+ String nodeKey = JedisClusterInfoCache .getNodeKey (nodeHostAndPort );
67+ Map <String , ConnectionPool > nodes = jedisCluster .getClusterNodes ();
68+ ConnectionPool nodePool = nodes .get (nodeKey );
69+ Connection nodeConnection = nodePool != null ? nodePool .getResource () : null ;
70+ if (nodeConnection == null )
71+ throw new SQLException (String .format ("Cluster node not found: %s" , nodeHostAndPort ));
72+ return nodeConnection ;
7373 }
7474
7575
0 commit comments