{"record":{"id":"35d9d42dfa9e40e8","repo":"tursodatabase/turso","slug":"exception-while-resetting-statement","errorCode":null,"errorMessage":"Exception while resetting statement","messagePattern":"Exception while resetting statement","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"bindings/java/src/main/java/tech/turso/core/TursoStatement.java","lineNumber":297,"sourceCode":"   *\n   * @throws SQLException If a database access error occurs\n   */\n  public int parameterCount() throws SQLException {\n    final int result = parameterCount(statementPointer);\n    if (result == -1) {\n      throw new SQLException(\"Exception while retrieving parameter count\");\n    }\n\n    return result;\n  }\n\n  private native int parameterCount(long statementPointer) throws SQLException;\n\n  /** Resets this statement so it's ready for re-execution */\n  public void reset() throws SQLException {\n    final int result = reset(statementPointer);\n    if (result == -1) {\n      throw new SQLException(\"Exception while resetting statement\");\n    }\n    this.resultSet = TursoResultSet.of(this);\n  }\n\n  private native int reset(long statementPointer) throws SQLException;\n\n  /**\n   * Checks if the statement is closed.\n   *\n   * @return true if the statement is closed, false otherwise.\n   */\n  public boolean isClosed() {\n    return closed;\n  }\n\n  @Override\n  public String toString() {\n    return (\"tursoStatement{\"","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/tursodatabase/turso/blob/bad083fafbefdeae9a42ec19bdaaad8918dcf411/bindings/java/src/main/java/tech/turso/core/TursoStatement.java#L279-L315","documentation":"Thrown when the native reset call returns -1. The native implementation returns -1 either when the statement pointer cannot be resolved (closed/finalized statement) or when stmt.reset() itself errors. Reset is meant to clear bindings and results of a live statement so it can be re-executed; it cannot revive a closed one.","triggerScenarios":"stmt.reset() after stmt.close(); reset after the connection was closed; reset racing with another thread closing the statement; native reset erroring on an aborted/interrupted statement.","commonSituations":"Batch loops that reset a statement per iteration but whose error handler closed the statement on a previous failure; connection pools that reset statements on return after already closing them; timeout/abort paths that invalidate the statement before the retry loop resets it.","solutions":["Check isClosed() before reset() and re-prepare the statement if closed.","In error paths, drop the statement and prepare a fresh one instead of resetting the old handle.","Ensure the connection is still open before resetting.","Keep reset() inside the same try block that owns the statement's execution loop."],"exampleFix":"// before\nstmt.close();\nstmt.reset(); // throws: cannot reset a closed statement\n\n// after\nif (!stmt.isClosed()) {\n    stmt.reset();\n} else {\n    stmt = conn.prepare(sql); // re-prepare instead of resetting a dead handle\n}","handlingStrategy":"validation","validationCode":"if (stmt.isClosed()) {\n    stmt = conn.prepare(sql); // fresh statement instead of resetting a dead one\n} else {\n    stmt.reset();\n}","typeGuard":null,"tryCatchPattern":"try {\n    stmt.reset();\n} catch (SQLException e) {\n    // handle is stale or reset failed: discard and re-prepare\n    stmt = conn.prepare(sql);\n}","preventionTips":["Check isClosed() before reset() in every batch iteration.","In error paths, drop the statement and prepare a new one; do not reset a possibly-invalidated handle.","Confirm the connection is open before resetting.","Keep the reset-prepare-bind-execute cycle within a single owning try block."],"tags":["java","jdbc","reset","statement-reuse","statement-lifecycle","use-after-close"],"backgroundTag":"closed-statement-access","analyzedSha":"bad083fafbefdeae9a42ec19bdaaad8918dcf411","analyzedAt":"2026-08-16T23:12:11.798Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}