{"record":{"id":"58ec820a14648f59","repo":"prestodb/presto","slug":"e-getmessage","errorCode":null,"errorMessage":"e.getMessage()","messagePattern":"e\\.getMessage\\(\\)","errorType":"exception","errorClass":"BatchUpdateException","httpStatus":null,"severity":"error","filePath":"presto-jdbc/src/main/java/com/facebook/presto/jdbc/PrestoPreparedStatement.java","lineNumber":468,"sourceCode":"        checkOpen();\n        batchValues.clear();\n        isBatch = false;\n    }\n\n    @Override\n    public int[] executeBatch()\n            throws SQLException\n    {\n        try {\n            int[] batchUpdateCounts = new int[batchValues.size()];\n            for (int i = 0; i < batchValues.size(); i++) {\n                try {\n                    super.execute(getExecuteSql(statementName, batchValues.get(i)));\n                    batchUpdateCounts[i] = getUpdateCount();\n                }\n                catch (SQLException e) {\n                    long[] updateCounts = Arrays.stream(batchUpdateCounts).mapToLong(j -> j).toArray();\n                    throw new BatchUpdateException(e.getMessage(), e.getSQLState(), e.getErrorCode(), updateCounts, e.getCause());\n                }\n            }\n            return batchUpdateCounts;\n        }\n        finally {\n            clearBatch();\n        }\n    }\n\n    @Override\n    public void setCharacterStream(int parameterIndex, Reader reader, int length)\n            throws SQLException\n    {\n        throw new NotImplementedException(\"PreparedStatement\", \"setCharacterStream\");\n    }\n\n    @Override\n    public void setRef(int parameterIndex, Ref x)","sourceCodeStart":450,"sourceCodeEnd":486,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-jdbc/src/main/java/com/facebook/presto/jdbc/PrestoPreparedStatement.java#L450-L486","documentation":"When executeBatch encounters a failure for one statement in the batch, it wraps the SQLException in a java.sql.BatchUpdateException, copying message, SQLState, errorCode and cause, with update counts for statements executed so far. The message shown is just the underlying failure's message re-raised as a batch exception.","triggerScenarios":"Calling executeBatch() after addBatch()/addBatch(sql) on a PrestoPreparedStatement where any batched statement fails (e.g. syntax error, type mismatch, constraint/verification failure on statement i).","commonSituations":"Bulk inserts where one row's value violates a column type; partial batches where the first N statements succeeded and later ones failed — inspect getUpdateCounts() to see how far execution got.","solutions":["Inspect BatchUpdateException.getUpdateCounts() to identify which statement(s) failed.","Log e.getCause() — the original SQLException carries the real server error detail.","Validate parameter types/values before addBatch to reject bad rows early.","Execute statements individually to pinpoint the failing statement when diagnosing."],"exampleFix":"// before\nps.executeBatch();\n// after\ntry {\n    ps.executeBatch();\n} catch (BatchUpdateException e) {\n    long[] counts = e.getUpdateCounts();\n    LOG.error(\"batch failed at index {} cause: {}\", counts.length, e.getCause(), e);\n}","handlingStrategy":"try-catch","validationCode":"// validate each batched parameter before addBatch\nfor (Object p : row) {\n    if (p != null && !(p instanceof String || p instanceof Number || p instanceof java.sql.Date || p instanceof java.sql.Time || p instanceof java.sql.Timestamp)) {\n        throw new IllegalArgumentException(\"bad batch param type: \" + p.getClass());\n    }\n}","typeGuard":null,"tryCatchPattern":"try { ps.executeBatch(); } catch (BatchUpdateException e) { long[] counts = e.getUpdateCounts(); SQLException cause = (SQLException) e.getCause(); LOG.error(\"batch failed; executed={} sqlState={} cause={}\", counts.length, e.getSQLState(), cause, e); }","preventionTips":["Inspect getUpdateCounts() to find the failing statement index","Log e.getCause() for the underlying server error","Validate parameter types/values before addBatch","Use executeBatch in a transaction so partial results can be rolled back"],"tags":["jdbc","presto","batch"],"backgroundTag":"batch-update-exception","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"}