{"record":{"id":"dc7ce8ebf17f4aee","repo":"pentaho/pentaho-kettle","slug":"unable-to-clear-batch-for-prepared-statement","errorCode":null,"errorMessage":"Unable to clear batch for prepared statement","messagePattern":"Unable to clear batch for prepared statement","errorType":"exception","errorClass":"KettleDatabaseException","httpStatus":null,"severity":"warning","filePath":"core/src/main/java/org/pentaho/di/core/database/Database.java","lineNumber":1642,"sourceCode":"    }\n  }\n\n  /**\n   * Clears batch of insert prepared statement\n   *\n   * @throws KettleDatabaseException\n   * @deprecated\n   */\n  @Deprecated\n  public void clearInsertBatch() throws KettleDatabaseException {\n    clearBatch( prepStatementInsert );\n  }\n\n  public void clearBatch( PreparedStatement preparedStatement ) throws KettleDatabaseException {\n    try {\n      preparedStatement.clearBatch();\n    } catch ( SQLException e ) {\n      throw new KettleDatabaseException( \"Unable to clear batch for prepared statement\", e );\n    }\n  }\n\n  public void executeAndClearBatch( PreparedStatement preparedStatement ) throws KettleDatabaseException {\n    try {\n      if ( written > 0 && getDatabaseMetaData().supportsBatchUpdates() ) {\n        preparedStatement.executeBatch();\n      }\n\n      written = 0;\n      preparedStatement.clearBatch();\n    } catch ( SQLException e ) {\n      throw new KettleDatabaseException( \"Unable to clear batch for prepared statement\", e );\n    }\n  }\n\n  public void insertFinished( boolean batch ) throws KettleDatabaseException {\n    insertFinished( prepStatementInsert, batch );","sourceCodeStart":1624,"sourceCodeEnd":1660,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/database/Database.java#L1624-L1660","documentation":"clearBatch(PreparedStatement) wraps a SQLException from PreparedStatement.clearBatch() as 'Unable to clear batch for prepared statement'. It means the JDBC driver rejected clearing the batch, usually because the statement or its connection is in a bad or closed state.","triggerScenarios":"Calling Database.clearBatch(preparedStatement) when the underlying statement/connection is closed, the connection was lost, or the driver considers the statement no longer usable (e.g. after an aborted batch execution).","commonSituations":"Connection killed by network idle timeout mid-batch; statement closed elsewhere before clearBatch; cleanup code running after an earlier executeBatch failure left the statement invalid; driver-specific state restrictions.","solutions":["Check the cause SQLException — 'closed statement/connection' means the real issue happened earlier","Reopen the database connection and re-prepare the statement instead of reusing the stale one","Guard cleanup: treat clearBatch failure during error handling as non-fatal","Investigate any preceding executeBatch exception in the logs","Verify connection validity (isValid/ping) before continuing batch processing"],"exampleFix":"// before\ndatabase.clearBatch(preparedStatement);\n// after\ntry {\n  database.clearBatch(preparedStatement);\n} catch (KettleDatabaseException e) {\n  log.logMinimal(\"Discarding batch after clear failure: \" + e.getMessage());\n}","handlingStrategy":"try-catch","validationCode":"if (preparedStatement == null || preparedStatement.isClosed()) {\n  return; // nothing to clear\n}","typeGuard":null,"tryCatchPattern":"try {\n  database.clearBatch(preparedStatement);\n} catch (KettleDatabaseException e) {\n  log.logDebug(\"Batch clear skipped: \" + e.getMessage()); // non-fatal during cleanup\n}","preventionTips":["Treat cleanup failures as warnings, not fatal errors","Check connection validity before continuing batches","Investigate preceding executeBatch errors in logs","Re-prepare statements after connection loss"],"tags":["jdbc","batch","resource-cleanup"],"backgroundTag":"database-query-failed","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}