2016-11-04 7 views
0

でのRedis(JRedis)プールの接続を再利用し、私はJava.Thisで私のデータベースのデータをキャッシュするには、Windows用のRedis(3.2.100)を使用していますが、私のRedisのは、コードを初期化しますどのようにJavaの

private static Dictionary<Integer, JedisPool> pools = new Hashtable(); 

    static { 
     JedisPoolConfig config = new JedisPoolConfig(); 
     config.setMaxIdle(2); 
     config.setMaxTotal(10); 
     config.setTestOnBorrow(true); 
     config.setMaxWaitMillis(2000); 
     for (int i = 0; i < 16; i++) { 
      JedisPool item = new JedisPool(config, "127.0.0.1", 6379,10*1000); 
      pools.put(i, item); 
     } 
    } 

これは、キャッシュコード:

public static String get(String key, Integer db) { 
     JedisPool poolItem = pools.get(db); 
     Jedis jredis = poolItem.getResource(); 
     String result = jredis.get(key); 
     return result; 
    } 

問題はしばらくすると、プログラムの実行である、のgetResourceメソッドは、スロー:

redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool 

コを再利用するために、どのようにクライアントが最大値に達したことを確認するには、このコマンドを使用しています。

D:\Program Files\Redis>redis-cli.exe info clients 
# Clients 
connected_clients:11 
client_longest_output_list:0 
client_biggest_input_buf:0 
blocked_clients:0 

どのように修正するのですか?

+0

JedisExceptionが原因ではNoSuchElementExceptionをスローする必要があります。あなたはそれについてもっと情報を与えることができますか? – herokingsley

答えて

0

はこのように、この機能を変更し、Redisの接続を閉じることを忘れないでください:

public static String get(String key, Integer db) { 
     JedisPool poolItem = pools.get(db); 
     Jedis jredis = null; 
     String result = null; 
     try { 
      jredis = poolItem.getResource(); 
      result = jredis.get(key); 
     } catch (Exception e) { 
      log.error("get value error", e); 
     } finally { 
      if (jredis != null) { 
       jredis.close(); 
      } 
     } 
     return result; 
    }