{"record":{"id":"972d1314263b46fc","repo":"redis/jedis","slug":"cannot-use-jedis-when-in-multi-please-use-transac","errorCode":null,"errorMessage":"Cannot use Jedis when in Multi. Please use Transaction or reset jedis state.","messagePattern":"Cannot use Jedis when in Multi\\. Please use Transaction or reset jedis state\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/redis/clients/jedis/Jedis.java","lineNumber":431,"sourceCode":"      @Override\n      protected void onAfterDiscard() {\n        resetState();\n      }\n    };\n    return transaction;\n  }\n\n  // Legacy\n  public Pipeline pipelined() {\n    pipeline = new Pipeline(this);\n    return pipeline;\n  }\n\n  // Legacy\n  protected void checkIsInMultiOrPipeline() {\n//    if (connection.isInMulti()) {\n    if (transaction != null) {\n      throw new IllegalStateException(\n          \"Cannot use Jedis when in Multi. Please use Transaction or reset jedis state.\");\n    } else if (pipeline != null && pipeline.hasPipelinedResponse()) {\n      throw new IllegalStateException(\n          \"Cannot use Jedis when in Pipeline. Please use Pipeline or reset jedis state.\");\n    }\n  }\n\n  public int getDB() {\n    return this.db;\n  }\n\n  /**\n   * @return <code>PONG</code>\n   */\n  @Override\n  public String ping() {\n    checkIsInMultiOrPipeline();\n    connection.sendCommand(Command.PING);","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/redis/jedis/blob/6dac31d4c224fb3257c216f3985340c6f500cdcb/src/main/java/redis/clients/jedis/Jedis.java#L413-L449","documentation":"Jedis's legacy single-connection client cannot run interactive commands while a MULTI transaction is open on it. checkIsInMultiOrPipeline throws IllegalStateException when the transaction field is non-null to prevent commands from being interleaved into the transaction unintentionally. The message directs you to use the Transaction object for commands or reset the Jedis state.","triggerScenarios":"Calling ping/select/swapDB/flushDB/flushAll/copy on a Jedis instance after jedis.multi() was called and before the Transaction was committed/aborted, e.g. doing health-check pings or a flushAll inside the transaction window.","commonSituations":"Shared Jedis instance used by monitoring code that pings while another thread has opened a transaction; forgetting to exec() or close() a Transaction so the Jedis stays in multi state; calling administrative commands (flushDB) inside transaction blocks for tests.","solutions":["Execute the commands on the Transaction object instead of the Jedis instance (tx.ping() etc.)","Commit (tx.exec()) or abort (tx.close()) the transaction before using jedis directly","Discard/reset state: call jedis.resetState() to clear the multi flag","Avoid sharing one Jedis instance between transactional and direct use; use JedisPool"],"exampleFix":"// before\nTransaction tx = jedis.multi();\njedis.flushDB(); // IllegalStateException: in Multi\n// after\nTransaction tx = jedis.multi();\ntx.flushDB();\ntx.exec();\n// or, if the transaction is abandoned:\njedis.resetState();","handlingStrategy":"validation","validationCode":"if (jedis.isInMulti()) { // or track your own Transaction reference\n  throw new IllegalStateException(\"complete or reset the transaction first\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  jedis.ping();\n} catch (IllegalStateException e) {\n  if (e.getMessage().startsWith(\"Cannot use Jedis when in Multi\")) {\n    jedis.resetState();\n    jedis.ping();\n  } else throw e;\n}","preventionTips":["Never share a Jedis instance between transactional and direct command use","Always commit/close Transactions in try-with-resources","Call resetState() on error paths that abandon a transaction","Route health-check pings through a separate pooled connection"],"tags":["transaction","multi","illegal-state","redis"],"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"}