{"record":{"id":"1c01a1c3f78a7393","repo":"redis/jedis","slug":"could-not-return-the-resource-to-the-pool","errorCode":null,"errorMessage":"Could not return the resource to the pool","messagePattern":"Could not return the resource to the pool","errorType":"exception","errorClass":"JedisException","httpStatus":null,"severity":"error","filePath":"src/main/java/redis/clients/jedis/util/Pool.java","lineNumber":53,"sourceCode":"\n  public T getResource() {\n    try {\n      return super.borrowObject();\n    } catch (JedisException je) {\n      throw je;\n    } catch (Exception e) {\n      throw new JedisException(\"Could not get a resource from the pool\", e);\n    }\n  }\n\n  public void returnResource(final T resource) {\n    if (resource == null) {\n      return;\n    }\n    try {\n      super.returnObject(resource);\n    } catch (RuntimeException e) {\n      throw new JedisException(\"Could not return the resource to the pool\", e);\n    }\n  }\n\n  public void returnBrokenResource(final T resource) {\n    if (resource == null) {\n      return;\n    }\n    try {\n      super.invalidateObject(resource);\n    } catch (Exception e) {\n      throw new JedisException(\"Could not return the broken resource to the pool\", e);\n    }\n  }\n\n  @Override\n  public void addObjects(int count) {\n    try {\n      for (int i = 0; i < count; i++) {","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/redis/jedis/blob/6dac31d4c224fb3257c216f3985340c6f500cdcb/src/main/java/redis/clients/jedis/util/Pool.java#L35-L71","documentation":"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.","triggerScenarios":"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.","commonSituations":"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().","solutions":["Prefer try-with-resources on the Jedis object over manual returnResource so each resource is returned exactly once.","Remove duplicate return/close calls for the same connection.","Verify the resource came from the same pool instance you're returning it to.","Use returnBrokenResource (or jedis.close() on error paths) when the connection is in a bad state instead of returning it as healthy."],"exampleFix":"// before\nJedis j = pool.getResource();\nj.get(\"k\");\npool.returnResource(j);\npool.returnResource(j); // double return -> throws\n// after\ntry (Jedis j = pool.getResource()) {\n  j.get(\"k\");\n} // returned exactly once","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  pool.returnResource(resource);\n} catch (JedisException e) {\n  // resource likely already returned/invalid: drop reference, don't reuse\n  log.warn(\"Resource return failed\", e.getCause());\n}","preventionTips":["Use try-with-resources instead of manual returnResource.","Never share a borrowed connection across threads.","Return each resource exactly once."],"tags":["pool","resource-return","double-return"],"backgroundTag":"invalid-state-transition","analyzedSha":"6dac31d4c224fb3257c216f3985340c6f500cdcb","analyzedAt":"2026-09-08T04:55:01.204Z","contentChangedAt":"2026-09-08T04:55:01.204Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}