Whole document tree
6 Batch UpdatesThe batch update facility allows multiple update operations to be submitted to a data source for processing at once. Submitting multiple updates together, instead of individually, can greatly improve performance.Statement , PreparedStatement , and CallableStatement
objects can be used to submit batch updates.
6.1 Description of batch updates6.1.1 StatementsThe batch update facility allows aStatement object to submit a set of heterogeneous
update commands together as a single unit, or batch, to the underlying DBMS. In the
example below all of the update operations required to insert a new employee into a fictitious
company database are submitted as a single batch.
In the example, autocommit mode is disabled to prevent the driver from committing the transaction when// turn off autocommit con.setAutoCommit(false); Statement stmt = con.createStatement(); stmt.addBatch("INSERT INTO employees VALUES (1000, 'Joe Jones')"); stmt.addBatch("INSERT INTO departments VALUES (260, 'Shoe')"); stmt.addBatch("INSERT INTO emp_dept VALUES (1000, 260)"); // submit a batch of update commands for execution int[] updateCounts = stmt.executeBatch(); Statement.executeBatch() is called. Disabling autocommit allows
an application to decide whether or not to commit the transaction in the event that an
error occurs and some of the commands in a batch cannot be processed successfully.
For this reason, autocommit should always be turned off when batch updates are done.
The commit behavior of executeBatch is always implementation defined when an error
occurs and autocommit is true.
To keep our discussion of batch updates general, we define the term element to refer to
an individual member of a batch. As we have seen, an element in a batch is just a simple
command when a
In the new JDBC API, a
The
executeBatch() closes the calling Statement object's current result set if one
is open. The statement's internal list of batch elements is reset to empty once executeBatch()
returns. The behavior of the executeQuery , executeUpdate , or execute
methods is implementation defined when a statement's batch is non-empty.
Handling failures during execution A JDBC driver may or may not continue processing the remaining elements in a batch once execution of an element in a batch fails. However, a JDBC driver must always provide the same behavior when used with a particular DBMS. For example, a driver cannot continue processing after a failure for one batch, and not continue processing for another batch.
If a driver stops processing after the first failure, the array returned by
When a driver continues processing in the presence of failures, the number of elements,
N, in the array returned by
A JDBC technology based application can distinguish a JDBC driver that continues
processing after a failure from one that does not by examining the size of the array returned
by 6.1.2 PreparedStatementsAn element in a batch consists of a parameterized command and an associated set of parameters when aPreparedStatement is used. The batch update facility is used with
a PreparedStatement to associate multiple sets of input parameter values with a single
PreparedStatement object. The sets of parameter values together with their associated
parameterized update command can then be sent to the underlying DBMS engine for
execution as a single unit.
The example below inserts two new employee records into a database as a single batch.
The
Finally,// turn off autocommit con.setAutoCommit(false); 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(); PreparedStatement.executeBatch() is called to submit the updates to the
DBMS. Calling PreparedStatement.executeBatch() clears the statement's associated
list of batch elements. The array returned by PreparedStatement.executeBatch()
contains an element for each set of parameters in the batch, similar to the case
for Statement . Each element either contains an update count or the generic `success'
indicator (-2).
Error handling in the case of 6.1.3 Callable StatementsThe batch update facility works the same withCallableStatement objects as it does
with PreparedStatement objects. Multiple sets of input parameter values may be associated
with a callable statement and sent to the DBMS together. Stored procedures
invoked using the batch update facility with a callable statement must return an update
count, and may not have out or inout parameters. The CallableStatement.executeBatch()
method should throw an exception if this restriction is violated. Error handling
is analogous to PreparedStatement .
6.2 What's requiredSupport for batch updates is optional. If a JDBC driver supports batch updates, then theDatabaseMetaData.supportsBatchUpdates() method must return true, else it must
return false. In addition, to preserve backward compatibility, JDBC drivers that do not
continue processing after a failure are not required to return a value of -2 as described
in Section 6.1, however, this is encouraged. JDBC drivers that continue processing are
required to support both of the negative return values.
Note: In the future, the JDBC API shall define symbolic constants for the negative array entry values described in Section 6.1. These values have been added as an addendum to the original JDBC 2.0 API specification.
Contents | Prev | Next jdbc@eng.sun.com or jdbc-business@eng.sun.com Copyright © 1996-1999 Sun Microsystems, Inc. All rights reserved. |