redis/jedis · critical · JedisException

Could not get a resource from the pool

Error message

Could not get a resource from the pool

What it means

Pool.getResource() borrows a connection from the underlying commons-pool2 pool. JedisExceptions (e.g. connection failures) are rethrown as-is; any other Exception from borrowObject is wrapped in JedisException("Could not get a resource from the pool"). This is the classic sign that the client cannot obtain a Redis connection.

Solutions

  1. Check Redis availability: `redis-cli -h <host> -p <port> ping` should return PONG.
  2. Inspect getCause(): JedisConnectionException means connectivity; NoSuchElementException/timeout means pool exhaustion.
  3. Leak-check: ensure every getResource() has a matching returnResource/close (use try-with-resources on Jedis).
  4. Raise pool config (setMaxTotal/maxWait) if exhaustion is legitimate load.
  5. Verify host/port/firewall/timeout settings in JedisPool/ConnectionPoolConfig.

Example fix

// before
Jedis j = pool.getResource(); // leaked on exception paths
// after
try (Jedis j = pool.getResource()) {
  j.set("k", "v");
} // auto-returns connection, prevents exhaustion
Defensive patterns

Strategy: try-catch

Validate before calling

// before borrowing, verify reachability
try (Socket s = new Socket()) {
  s.connect(new InetSocketAddress(host, port), 2000); // throws if Redis unreachable
}

Try / catch

try (Jedis jedis = pool.getResource()) {
  jedis.ping();
} catch (JedisException e) {
  if (e.getCause() instanceof NoSuchElementException) {
    // pool exhausted: check leaks / raise maxTotal
  } else {
    // connectivity issue: check host/port/network
  }
  throw e;
}

Prevention

When it happens

Trigger: pool.getResource() when Redis is down/unreachable, max pool capacity (blockWhenExhausted + maxWait) is exhausted, connection timeouts, or the factory makeObject fails (bad host/port, auth failure in newer paths).

Common situations: Redis not running or wrong host/port in config; pool exhausted because connections are leaked (never returned to pool); network latency/firewall blocking port 6379; too small maxTotal for thread count under load.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of redis/jedis@6dac31d4c2 (2026-09-08). Data as JSON: /api/errors/9574f52a25efcd69. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/redis/clients/jedis/util/Pool.java:42

  public void close() {
    destroy();
  }

  public void destroy() {
    try {
      super.close();
    } catch (RuntimeException e) {
      throw new JedisException("Could not destroy the pool", e);
    }
  }

  public T getResource() {
    try {
      return super.borrowObject();
    } catch (JedisException je) {
      throw je;
    } catch (Exception e) {
      throw new JedisException("Could not get a resource from the pool", e);
    }
  }

  public void returnResource(final T resource) {
    if (resource == null) {
      return;
    }
    try {
      super.returnObject(resource);
    } catch (RuntimeException e) {
      throw new JedisException("Could not return the resource to the pool", e);
    }
  }

  public void returnBrokenResource(final T resource) {
    if (resource == null) {
      return;
    }

View on GitHub (pinned to 6dac31d4c2)