title: Jedis : Could not get a resource from the pool
body: <h2>Background</h2>
  
  <p>Our application uses <code>Jedis-2.2.1</code> and connects to <code>Redis-2.6</code>, here's how I get jedis resource : </p>
  
  <pre><code>protected static JedisWrapper getRedisUserWrite(String UDID) {
          if (redisUserWritePools.get(0) == null) init();
  
          int hash = hash(UDID);
          Jedis jedis = redisUserWritePools.get(hash).getResource();
          jedis.select(dbs.get("redisUserWritePools" + hash));
  
          return new JedisWrapper(jedis, redisUserWritePools.get(hash));
      }
  </code></pre>
  
  <p>And this is my <code>JedisWrapper</code>(Unify the management of resources):</p>
  
  <pre><code>public class JedisWrapper {
      private Jedis jedis;
      private JedisPool pool;
  
      public JedisWrapper(Jedis jedis, JedisPool pool) {
          this.jedis = jedis;
          this.pool = pool;
      }
  
      public Jedis get(){
          return this.jedis;
      }
  
      public void returnResource() {
          if(null != this.jedis){
              this.pool.returnResource(this.jedis);
          }
      }
  
      public void returnBrokenResource() {
          if(null != this.jedis) {
              this.pool.returnBrokenResource(this.jedis);
          }
          this.jedis = null;
      }
  }
  </code></pre>
  
  <p><code>JedisWrapper</code> is the container if Jedis instance, here's how I use it :</p>
  
  <pre><code>private static void cacheSDKIDs(String UDID, String[] SDKIDs) {
  
          JedisWrapper wrapper = getRedisUserWrite(UDID);
          try {
              if (SDKIDs != null) {
                  wrapper.get().del(UDID);
                  wrapper.get().sadd(UDID, SDKIDs);
              }
          } catch (JedisConnectionException e) {
              e.printStackTrace();
              wrapper.returnBrokenResource();
          }catch (Exception e) {
              e.printStackTrace();
          } finally {
              wrapper.returnResource();
          }
      }
  </code></pre>
  
  <p>Note that, <code>SKDIDs</code> maybe very large(e.g. could reach the maximum of 8KB).</p>
  
  <h2>Here's the problem</h2>
  
  <p>Every time I restart our application, all redis connections are normal, but several hours
   later, the <code>Could not get a resource from the pool</code> Exception comes out. And frequency become higher and higher, then all the connections to Redis are disconnected and can create new connection.</p>
  
  <p>Here's my configuration : </p>
  
  <pre><code>&lt;bean id = "redisConfig" class="redis.clients.jedis.JedisPoolConfig"&gt;
      &lt;property name="maxActive"  value="400" /&gt;
      &lt;property name="maxIdle" value="100" /&gt;
      &lt;property name="minIdle" value="20" /&gt;
      &lt;property name="maxWait" value="4000" /&gt;
      &lt;property name="testOnBorrow"  value="true"/&gt;
      &lt;property name="testOnReturn" value="true" /&gt;
    &lt;/bean&gt;
  </code></pre>
  
  <p>Exception Stacktrace:</p>
  
  <pre><code>Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
      at redis.clients.util.Pool.getResource(Pool.java:40)
      at com.xxxice.redis.BaseRedis.getRedisUserWrite(BaseRedis.java:158)
      at com.xxx.service.redis.DeviceRedis.cacheSDKIds(DeviceRedis.java:128)
      at com.xxx.redis.DeviceRedis.cacheDevice(DeviceRedis.java:65)
      at com.xxx.service.DeviceService.update(DeviceService.java:88)
      at com.xxx.controller.Devices.update(Devices.java:25)
      ... 32 more
  Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
      at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1174)
      at redis.clients.util.Pool.getResource(Pool.java:38)
      ... 37 more
  </code></pre>