{"record":{"id":"d4052a3842352fa6","repo":"prestodb/presto","slug":"batch-prepared-statement-must-be-executed-using-ex","errorCode":null,"errorMessage":"Batch prepared statement must be executed using executeBatch method","messagePattern":"Batch prepared statement must be executed using executeBatch method","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"presto-jdbc/src/main/java/com/facebook/presto/jdbc/PrestoPreparedStatement.java","lineNumber":821,"sourceCode":"\n    private static List<String> toValues(Map<Integer, String> parameters)\n            throws SQLException\n    {\n        ImmutableList.Builder<String> values = ImmutableList.builder();\n        for (int index = 0; index < parameters.size(); index++) {\n            if (!parameters.containsKey(index)) {\n                throw new SQLException(\"No value specified for parameter \" + (index + 1));\n            }\n            values.add(parameters.get(index));\n        }\n        return values.build();\n    }\n\n    private void requireNonBatchStatement()\n            throws SQLException\n    {\n        if (isBatch) {\n            throw new SQLException(\"Batch prepared statement must be executed using executeBatch method\");\n        }\n    }\n\n    private static String getExecuteSql(String statementName, List<String> values)\n    {\n        StringBuilder sql = new StringBuilder();\n        sql.append(\"EXECUTE \").append(statementName);\n        if (!values.isEmpty()) {\n            sql.append(\" USING \");\n            Joiner.on(\", \").appendTo(sql, values);\n        }\n        return sql.toString();\n    }\n\n    private static String formatLiteral(String type, String x)\n    {\n        return type + \" \" + formatStringLiteral(x);\n    }","sourceCodeStart":803,"sourceCodeEnd":839,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-jdbc/src/main/java/com/facebook/presto/jdbc/PrestoPreparedStatement.java#L803-L839","documentation":"requireNonBatchStatement() guards every single-execution entry point (executeQuery, executeUpdate, executeLargeUpdate, execute). If addBatch() has been called (isBatch is true), the accumulated batch must be run with executeBatch(); mixing single-shot execution with a pending batch is rejected to avoid silently dropping the queued batch rows.","triggerScenarios":"Calling executeQuery/executeUpdate/execute on a PreparedStatement after at least one addBatch() call, without calling executeBatch() (or clearBatch()) first.","commonSituations":"Code paths that sometimes batch and sometimes execute directly; retry/error-handling logic that abandons a batch halfway and re-executes as a single statement; reused statement objects where a previous operation left a pending batch.","solutions":["Call executeBatch() (and process its results) instead of execute/executeUpdate when batching","Call clearBatch() to discard a pending batch before switching to single-statement execution","Restructure code so a given statement instance is used either for batches or single execution, not both","Reuse a fresh PreparedStatement (or clearBatch) when falling back from a failed batch to per-row execution"],"exampleFix":"// before\nps.setInt(1, 1);\nps.addBatch();\nint n = ps.executeUpdate(); // throws\n// after\nps.setInt(1, 1);\nps.addBatch();\nint[] counts = ps.executeBatch();","handlingStrategy":"validation","validationCode":"if (hasPendingBatch) {\n    ps.executeBatch(); // or ps.clearBatch() before single execution\n} else {\n    ps.executeUpdate();\n}","typeGuard":null,"tryCatchPattern":"catch (SQLException e) {\n    if (e.getMessage().contains(\"must be executed using executeBatch\")) {\n        ps.clearBatch(); // discard pending batch, then retry as single statement\n    }\n    throw e;\n}","preventionTips":["Track batch state: once addBatch() is called, only executeBatch() is valid","Call clearBatch() to reset a statement after an abandoned batch","Use separate statement instances for batch vs single-row workloads"],"tags":["jdbc","batch","prepared-statement"],"backgroundTag":"jdbc-batch-execution-mismatch","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}