Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird
Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird
Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Batch Updates<br />
Batch updates are intended to group multiple update operations to be submitted to<br />
a database server to be processed at once. <strong>Firebird</strong> does not provide support for<br />
such functionality, but <strong>Jaybird</strong> emulates it by issuing separate update commands.<br />
Batch Updates with java.sql.Statement interface<br />
The Statement interface defines three methods for batch updates: addBatch,<br />
executeBatch and clearBatch. It is allowed to add arbitrary<br />
INSERT/UPDATE/DELETE or DDL statement to the batch group. Adding a<br />
statement that returns result set is an error.<br />
Statement stmt = connection.createStatement();<br />
stmt.addBatch("UPDATE products " +<br />
"SET amount = amount – 1 WHERE id = 1");<br />
stmt.addBatch("INSERT INTO orders(id, amount) VALUES(1,<br />
1)");<br />
int[]updateCounts = stmt.executeBatch();<br />
Illustration 4.15.: Example of batch updates using Statement object<br />
The <strong>JDBC</strong> specification recommends to turn the auto-commit mode off to<br />
guarantee standard behavior for all databases. The specification explicitly states<br />
that behavior in auto-commit case is implementation defined. <strong>Jaybird</strong> executes a<br />
batch in a single transaction, i.e. the “all-or-nothing” principle. A new transaction<br />
is started before the batch execution and is committed if there were no exception<br />
during batch execution, or is rolled back if at least one batch command generated<br />
an error.<br />
The Statement.executeBatch method submits the job to the database server. In<br />
case of successful execution of the complete batch, it returns an array of integers<br />
containing update counts for each of the commands. Possible values are:<br />
• 0 or positive value – an update count for the corresponding update/DDL<br />
statement.<br />
• Statement.SUCCESS_NO_INFO – <strong>driver</strong> does not have any information about<br />
the update count, but it knows that statement was executed successfully.<br />
The Statement.executeBatch method closes the current result set if one is open.<br />
After successful execution the batch is cleared. Calling execute, executeUpdate<br />
and executeQuery before the batch is executed does not have any effect on the<br />
currently added batch statements.<br />
If at least one statement from the batch fails, a<br />
java.sql.BatchUpdateException is thrown. <strong>Jaybird</strong> will stop executing<br />
statements from batch after the first error. In auto-commit mode it will also<br />
rollback the transaction. An application can obtain update counts for the already<br />
executed statements using getUpdateCounts method of the<br />
BatchUpdateException class. The returned array will always contain fewer<br />
entries than there were statements in the batch.<br />
Batch Updates with java.sql.PreparedStatement and java.sql.CallableStatement<br />
Chapter 4. Executing statements 50