ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzaha_5.4.0.1/batchblo.htm

119 lines
8.1 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="concept" />
<meta name="DC.Title" content="Blocked insert support" />
<meta name="abstract" content="You can use a blocked insert is an iSeries operation to insert several rows into a database table at a time." />
<meta name="description" content="You can use a blocked insert is an iSeries operation to insert several rows into a database table at a time." />
<meta name="DC.Relation" scheme="URI" content="batchupd.htm" />
<meta name="DC.Relation" scheme="URI" content="batchstm.htm" />
<meta name="DC.Relation" scheme="URI" content="batchpre.htm" />
<meta name="DC.Relation" scheme="URI" content="batchexc.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="batchblo" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Blocked insert support</title>
</head>
<body id="batchblo"><a name="batchblo"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Blocked insert support</h1>
<div><p>You can use a blocked insert is an iSeries™ operation to insert several rows
into a database table at a time.</p>
<p>A blocked insert is a special type of operation on an iSeries server
that provides a highly optimized way to insert several rows into a database
table at a time. Blocked inserts can be thought of as a subset of batched
updates. Batched updates can be any form of an update request, but blocked
inserts are specific. However, blocked insert types of batched updates are
common; the native JDBC driver has been changed to take advantage of this
feature.</p>
<p>Because of system restrictions when using blocked insert support, the default
setting for the native JDBC driver is to have blocked insert disabled. It
can be enabled through a Connection property or a DataSource property. Most
of the restrictions when using a blocked insert can be checked and handled
on your behalf, but a few restrictions cannot; thus, this is the reason for
turning off blocked insert support by default. The list of restrictions is
as follows:</p>
<ul><li>The SQL statement used must be an INSERT statement with a VALUES clause,
meaning that it is not an INSERT statement with SUBSELECT. The JDBC driver
recognizes this restriction and takes the appropriate course of action.</li>
<li>A PreparedStatement must be used, meaning that there is no optimized support
for Statement objects. The JDBC driver recognizes this restriction and takes
the appropriate course of action.</li>
<li>The SQL statement must specify parameter markers for all the columns in
the table. This means that you cannot either use constant values for a column
or allow the database to insert default values for any of the columns. The
JDBC driver does not have a mechanism to handle testing for specific parameter
markers in your SQL statement. If you set the property to perform optimized
blocked insertions and you do not avoid defaults or constants in your SQL
statements, the values that end up in the database table are not correct.</li>
<li>The connection must be to the local system. This means that a connection
using DRDA<sup>®</sup> to
access a remote system cannot be used because DRDA does not support a blocked insert
operation. The JDBC driver does not have a mechanism to handle testing for
a connection to a local system. If you set the property to perform an optimized
blocked insertion and you attempt to connect to a remote system, the processing
of the batch update fails.</li>
</ul>
<p>This code example shows how to enable support for blocked insert processing.
The only difference between this code and a version that does not use blocked
insert support is <samp class="codeph">use block insert=true</samp> that is added to
the Connection URL.</p>
<p><strong>Example:</strong> Blocked insert processing</p>
<div class="note"><span class="notetitle">Note:</span> By using the
code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
<pre>// Create a database connection
Connection c = DriverManager.getConnection("jdbc:db2:*local;use block insert=true");
BigDecimal bd = new BigDecimal("123456");
// Create a PreparedStatement to insert into a table with 4 columns
PreparedStatement ps =
c.prepareStatement("insert into cujosql.xxx values(?, ?, ?, ?)");
// Start timing...
for (int i = 1; i &lt;= 10000; i++) {
ps.setInt(1, i); // Set all the parameters for a row
ps.setBigDecimal(2, bd);
ps.setBigDecimal(3, bd);
ps.setBigDecimal(4, bd);
ps.addBatch(); //Add the parameters to the batch
}
// Process the batch
int[] counts = ps.executeBatch();
// End timing...</pre>
<p>In similar test cases, a blocked insert is several times faster than performing
the same operations when a blocked insert is not used. For example, the test
performed on the previous code was nine time faster using blocked inserts.
Cases that only use primitive types instead of objects can be up to sixteen
times faster. In applications where there is a significant amount of work
going on, change your expectations appropriately.</p>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="batchupd.htm" title="Batch update support allows any updates to the database to be passed as a single transaction between the user program and the database. This procedure can significantly improve performance when many updates must be performed at once.">Batch updates</a></div>
</div>
<div class="relconcepts"><strong>Related concepts</strong><br />
<div><a href="batchexc.htm" title="An important consideration of batch updates is what action to take when a call to the executeBatch method fails. In this case, a new type of exception, called BatchUpdateException, is thrown. The BatchUpdateException is a subclass of SQLException and it allows you to call all the same methods you have always called to receive the message, the SQLState, and vendor code.">BatchUpdateException</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="batchstm.htm" title="To perform a Statement batch update, you must turn off auto-commit. In Java Database Connectivity (JDBC), auto-commit is on by default. Auto-commit means any updates to the database are committed after each SQL statement is processed. If you want to treat a group of statements being handed to the database as one functional group, you do not want the database committing each statement individually. If you do not turn off auto-commit and a statement in the middle of the batch fails, you cannot roll back the entire batch and try it again because half of the statements have been made final. Further, the additional work of committing each statement in a batch creates a lot of overhead.">Statement batch update</a></div>
<div><a href="batchpre.htm" title="A preparedStatement batch is similar to the Statement batch; however, a preparedStatement batch always works off the same prepared statement, and you only change the parameters to that statement.">PreparedStatement batch update</a></div>
</div>
</div>
</body>
</html>