Batch Operations with MariaDB Connector/J
This page is part of MariaDB's Documentation.
The parent of this page is: MariaDB Connector/J
Topics on this page:
Overview
Java developers can use MariaDB Connector/J to perform batch operations with MariaDB database products.
Batch Operations
SQL statements and prepared statements can involve multiple calls to the database, which can incur the performance overhead of multiple round trips between the application and the database.
Batch operations group multiple SQL commands for the application to send to the database as a single call. Using batch operations can improve performance by reducing the number of calls and subsequently the number of round trips to the database an SQL statement requires.
By default, MariaDB Connector/J does not batch SQL statements, instead running statements one query at a time.
Create a Batch
A batch can be created by calling Statement.addBatch(String sql)
for each SQL command to be added to a batch:
Statement stmt = conn.createStatement();
stmt.addBatch("INSERT INTO ... VALUES (...)");
stmt.addBatch("INSERT INTO ... VALUES (...)");
stmt.addBatch("UPDATE .... SET ... WHERE ...");
When using a prepared statement, a batch can be created by calling PreparedStatement.addBatch()
for each set of parameters:
PreparedStatement ps = conn.prepareStatement(...);
stmnt.setString(1, ...);
stmnt.setString(2, ...);
stmnt.setString(3, ...);
ps.addBatch();
stmnt.setString(1, ...);
stmnt.setString(2, ...);
stmnt.setString(3, ...);
ps.addBatch();
Run a Batch
A batch can be run by calling Statement.executeBatch()
:
stmt.executeBatch();
ps.executeBatch();
Clear a Batch
A batch can be cleared, or emptied, by calling Statement.clearBatch()
:
stmt.clearBatch();
ps.clearBatch();
Related Connection Options
The following connection options/parameters are related to batch operations:
Option Name | Description | Data Type | Default Value | Version Added | Version Removed |
---|---|---|---|---|---|
| Use dedicated |
|
| 2.1.0 | |
| Use bulk batching exclusively for |
|
| 3.2.0 | |
| Permits batch to continue when an exception occurs. When executing a batch and an error occurs, must the batch continue and finish remaining batch before throwing exception. |
|
| 1.4.0 | 3.0.3 |
| Rewrites statements for |
|
| 1.1.8 | 3.0.3 |
|
|
|
| 1.5.0 | 3.0.3 |
| When using |
|
| 1.5.0 | 3.0.3 |
Code Example: Batch Queries
The following example program adds data to the test.contacts
table created in the example setup using a batched prepared statement.
import java.sql.*;
import java.util.Properties;
public class App {
public static void main(String[] argv) {
Properties connConfig = new Properties();
connConfig.setProperty("user", "db_user");
connConfig.setProperty("password", "db_user_password");
try (Connection conn = DriverManager.getConnection("jdbc:mariadb://HOST:PORT", connConfig)) {
try (PreparedStatement stmnt = conn.prepareStatement(
"INSERT INTO test.contacts (first_name, last_name, email) VALUES (?, ?, ?)")) {
stmnt.setString(1, "John");
stmnt.setString(2, "Smith");
stmnt.setString(3, "john.smith@example.com");
stmnt.addBatch();
stmnt.setString(1, "Jon");
stmnt.setString(2, "Smith");
stmnt.setString(3, "jon.smith@example.com");
stmnt.addBatch();
stmnt.setString(1, "Johnny");
stmnt.setString(2, "Smith");
stmnt.setString(3, "johnny.smith@example.com");
stmnt.addBatch();
stmnt.executeBatch();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Confirm that the test.contacts
table was properly updated using MariaDB Client to execute a statement.
Example output:
SELECT * FROM contacts;
+----+------------+-----------+------------------------+
| id | first_name | last_name | email |
+----+------------+-----------+------------------------+
| 1 | John | Smith | john.smith@example.com |
+----+------------+-----------+------------------------+
| 2 | Jon | Smith | jon.smith@example.com |
+----+------------+-----------+------------------------+
| 3 | Johnny | Smith |johnny.smith@example.com|
+----+------------+-----------+------------------------+