redis/jedis · error · JedisException

Could not return the resource to the pool

Error message

Could not return the resource to the pool

What it means

Pool.returnResource(resource) gives a connection back to the pool via commons-pool2 returnObject. If that throws a RuntimeException it is wrapped in JedisException("Could not return the resource to the pool"). A failure here usually means the resource state is inconsistent with the pool's bookkeeping.

Solutions

  1. Prefer try-with-resources on the Jedis object over manual returnResource so each resource is returned exactly once.
  2. Remove duplicate return/close calls for the same connection.
  3. Verify the resource came from the same pool instance you're returning it to.
  4. Use returnBrokenResource (or jedis.close() on error paths) when the connection is in a bad state instead of returning it as healthy.

Example fix

// before
Jedis j = pool.getResource();
j.get("k");
pool.returnResource(j);
pool.returnResource(j); // double return -> throws
// after
try (Jedis j = pool.getResource()) {
  j.get("k");
} // returned exactly once
Defensive patterns

Strategy: try-catch

Try / catch

try {
  pool.returnResource(resource);
} catch (JedisException e) {
  // resource likely already returned/invalid: drop reference, don't reuse
  log.warn("Resource return failed", e.getCause());
}

Prevention

When it happens

Trigger: Calling returnResource with a connection that was already returned (double return), a resource that was invalidated/broken previously, or an object not borrowed from this pool instance; also factory passivate/destroy errors during return.

Common situations: Manual pool management code paths that both close the Jedis and return it; sharing one Jedis across threads so two threads return the same object; returning resources after pool.destroy().

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

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

  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;
    }
    try {
      super.invalidateObject(resource);
    } catch (Exception e) {
      throw new JedisException("Could not return the broken resource to the pool", e);
    }
  }

  @Override
  public void addObjects(int count) {
    try {
      for (int i = 0; i < count; i++) {

View on GitHub (pinned to 6dac31d4c2)