Why JDBC executeBatch Sends Single Queries and How to Enable True Batch Execution in MySQL
This article explains why MySQL 8.0’s JDBC driver processes executeBatch() calls as separate statements, demonstrates the root cause in the driver’s rewriteBatchedStatements logic, and shows how configuring rewriteBatchedStatements=true (and optionally allowMultiQueries=true) enables genuine batch execution.
In routine development, developers often use
JDBC executeBatch()to perform bulk inserts or updates. However, the author observed that with MySQL 8.0.18 the server log still recorded each statement individually, contrary to the expected batch behavior.
To reproduce the issue, a test loop executed three UPDATE statements via PreparedStatement and added them to a batch. Server logs (Figure 1) showed three separate SQL arrivals, each spaced in time. Debugging the client side revealed that the driver performed three round‑trips, sending one SQL per request (Figure 2).
Example code used in the test:
String sql = "update test set name='test' where id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
for (int i = 1; i < n; i++) {
ps.setInt(1, i);
ps.addBatch();
}
ps.executeBatch();Investigation of the MySQL driver source (version 8.0.18) showed that both Statement and PreparedStatement implementations check the connection property rewriteBatchedStatements. The driver only rewrites the batch into a single multi‑value statement when the batch size exceeds a threshold (more than three statements for PreparedStatement, more than four for Statement) and when the batch contains no empty statements.
Therefore, adding the parameter rewriteBatchedStatements=true to the JDBC URL resolves the problem. After updating the URL to:
jdbc:mysql://ip:port/mytest?rewriteBatchedStatements=trueand increasing the batch to four statements, the server log (Figure 5) shows a single batch request, and the client performs only one round‑trip (Figure 6), confirming true batch execution.
The driver also evaluates the allowMultiQueries property. This flag does not affect addBatch() or executeBatch(), but it enables the use of semicolon‑separated multiple queries in a single statement. Enabling it can reduce the extra enable/disable requests that the driver otherwise sends, which may impact distributed transaction workloads.
Best practice recommendation: configure both rewriteBatchedStatements=true and allowMultiQueries=true in the JDBC connection string to ensure efficient batch processing and avoid unnecessary extra queries.
Conclusion: For JDBC batch execution to work as intended with MySQL, the batch must contain more than three statements, contain no empty statements, and the connection URL must include rewriteBatchedStatements=true. Adding allowMultiQueries=true is also advisable.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
dbaplus Community
Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
