Everybody says batch updates reduce the number of JDBC calls. Could someone explain what "JDBC call" means and how increased number of such calls are costlier compared to carrying entire load in one JDBC call.
A JDBC batch update is a batch of updates grouped together, and sent to the database in one batch, rather than sending the updates one by one. Sending a batch of updates to the database in one go, is faster than sending them one by one, waiting for each one to finish.
Batch Processing allows you to group related SQL statements into a batch and submit them with one call to the database. When you send several SQL statements to the database at once, you reduce the amount of communication overhead, thereby improving performance. JDBC drivers are not required to support this feature.
The addBatch(String query) method of the CallableStatement, PreparedStatement, and Statement is used to single statements to a batch. The executeBatch() method begins the execution of all the grouped together statements.
Java™ database connectivity (JDBC) is the JavaSoft specification of a standard application programming interface (API) that allows Java programs to access database management systems. The JDBC API consists of a set of interfaces and classes written in the Java programming language.
Colloquially, when developer's use the phrase JDBC call they're talking about sending information over the wire to the database. The batch feature of the JDBC API allows you to submit multiple discrete operations in a single network call, as opposed to a call for each SQL statement. From the JDBC spec:
The batch update facility allows a Statement object to submit a set of heterogeneous SQL statements together as a single unit, or batch, to the underlying data source.
In the case of a PreparedStatement, the added benefit of only having to create a single statement exists. This allows you to execute the same query with multiple sets of bind parameters without adding the statement itself to the batch multiple times. The end result is less network traffic and its associated overhead.
An example from the spec:
PreparedStatement stmt = con.prepareStatement(
"INSERT INTO employees VALUES (?, ?)");
stmt.setInt(1, 2000);
stmt.setString(2, "Kelly Kaufmann");
stmt.addBatch();
stmt.setInt(1, 3000);
stmt.setString(2, "Bill Barnes");
stmt.addBatch();
// submit the batch for execution
int[] updateCounts = stmt.executeBatch();
As described in IBM's documentation:
The JDBC drivers that support JDBC 2.0 and above support batch updates. With batch updates, instead of updating rows of a DB2(R) table one at a time, you can direct JDBC to execute a group of updates at the same time. Statements that can be included in the same batch of updates are known as batchable statements.
If a statement has input parameters or host expressions, you can include that statement only in a batch that has other instances of the same statement. This type of batch is known as a homogeneous batch. If a statement has no input parameters, you can include that statement in a batch only if the other statements in the batch have no input parameters or host expressions. This type of batch is known as a heterogeneous batch. Two statements that can be included in the same batch are known as batch compatible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With