125 lines
6.9 KiB
HTML
125 lines
6.9 KiB
HTML
<?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="reference" />
|
|
<meta name="DC.Title" content="AS400JDBCSavepoint class" />
|
|
<meta name="abstract" content="" />
|
|
<meta name="description" content="" />
|
|
<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="jdbcsavepoint" />
|
|
<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>AS400JDBCSavepoint class</title>
|
|
</head>
|
|
<body id="jdbcsavepoint"><a name="jdbcsavepoint"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">AS400JDBCSavepoint class</h1>
|
|
<div><p></p>
|
|
<div class="section"><p>The <a href="javadoc/com/ibm/as400/access/AS400JDBCParameterMetaData.html#NAVBAR_TOP"> AS400JDBCSavepoint</a> class represents a logical breaking
|
|
point in a transaction. Using savepoints gives you more granular control
|
|
over which changes are affected when you roll back a transaction.</p>
|
|
</div>
|
|
<div class="section" id="jdbcsavepoint__jdbcsavepointfig1"><a name="jdbcsavepoint__jdbcsavepointfig1"><!-- --></a><p><strong>Figure 1: Using savepoints to control
|
|
rollbacks in a transaction</strong></p>
|
|
<p><img src="rzahh586.gif" longdesc="rzahh586.htm" alt="Using savepoints to control roll backs in a transaction" /></p>
|
|
<p>For
|
|
example, Figure 1 shows a transaction that includes two savepoints, A and
|
|
B. Rolling back the transaction to either savepoint undoes (or reverses)
|
|
only those changes from the point a rollback is called to the savepoint.
|
|
This prevents having to undo all the changes in the entire transaction. Note
|
|
that once you rollback to savepoint A, you cannot later rollback to savepoint
|
|
B. You cannot access savepoint B after work is rolled back past it.</p>
|
|
</div>
|
|
<div class="section"><h4 class="sectiontitle">Example: Using savepoints</h4><p>In this scenario, assume
|
|
that your application updates student records. At the end of updating a certain
|
|
field in every student record, you perform a commit. Your code detects a particular
|
|
error associated with updating this field and rolls back the work done when
|
|
this error occurs. You know that this particular error affects only the work
|
|
performed on the current record.</p>
|
|
<p>So, you set a savepoint between each
|
|
update of student records. Now, when this error occurs, you rollback only
|
|
the last update in the student table. Instead of having to roll back a large
|
|
amount of work, you can now roll back only a small amount of work.</p>
|
|
<p>The
|
|
following example code helps illustrate how you can use savepoints. The example
|
|
assumes that the student ID for John is 123456 and the student ID for Jane
|
|
is 987654.</p>
|
|
<pre> // Get a connection from the driver
|
|
Class.forName("com.ibm.as400.access.AS400JDBCDriver");
|
|
|
|
// Get a statement object
|
|
Statement statement = connection.createStatement();
|
|
|
|
// Update John's record with his 'B' grade in gym.
|
|
int rows = statement.executeUpdate(
|
|
"UPDATE STUDENTTABLE SET GRADE_SECOND_PERIOD = 'B' WHERE STUDENT_ID= '123456'");
|
|
|
|
// Set a savepoint marking an intermediate point in the transaction
|
|
Savepoint savepoint1 = connection.setSavepoint("SAVEPOINT_1");
|
|
|
|
// Update Jane's record with her 'C' grade in biochemistry.
|
|
int rows = statement.executeUpdate(
|
|
"UPDATE STUDENTTABLE SET GRADE_SECOND_PERIOD = 'C' WHERE STUDENT_ID= '987654'");
|
|
|
|
// An error is detected, so we need to roll back Jane's record, but not John's.
|
|
// Rollback the transaction to savepoint 1. The change to Jane's record is
|
|
// removed while the change to John's record remains.
|
|
connection.rollback(savepoint1);
|
|
|
|
// Commit the transaction; only John's 'B' grade is committed to the database.
|
|
connection.commit();</pre>
|
|
</div>
|
|
<div class="section"><h4 class="sectiontitle">Considerations and restrictions</h4><p>Using savepoints
|
|
requires that you be aware of the following considerations and restrictions:</p>
|
|
<p><strong>Considerations</strong></p>
|
|
<p>IBM<sup>®</sup> Toolbox
|
|
for Java™ follows
|
|
database rules regarding how rollbacks affect cursors and retained locks.
|
|
For example, when you set the connection option to keep cursors open after
|
|
a traditional rollback, cursors also remain open after a rollback to a savepoint.
|
|
In other words, when a rollback request happens involving savepoints, IBM Toolbox
|
|
for Java does
|
|
not move or close the cursor when the underlying database does not support
|
|
this.</p>
|
|
<p>Using a savepoint to roll back a transaction undoes only the actions
|
|
performed from the point where you start the roll back to the savepoint.
|
|
Actions performed before that savepoint remain. As in the previous example,
|
|
be aware that you can commit a transaction that includes work performed before
|
|
a particular savepoint but does not include work performed after the savepoint.</p>
|
|
<p>All
|
|
savepoints are released and become invalid when the transaction is committed
|
|
or when the entire transaction is rolled back. You can also release savepoints
|
|
by calling <a href="javadoc/com/ibm/as400/access/AS400JDBCConnection.html#RELEASESAVEPOINT(JAVA.SQL.SAVEPOINT)">Connection.releaseSavepoint()</a>. </p>
|
|
<p><strong>Restrictions</strong></p>
|
|
<p>The
|
|
following restrictions apply when using savepoints:</p>
|
|
<ul><li>Named savepoints must be unique.</li>
|
|
<li>You cannot reuse a savepoint name until the savepoint is released, committed,
|
|
or rolled back.</li>
|
|
<li>Auto-commit must be set to 'OFF' for savepoints to be valid. You can set
|
|
auto-commit 'OFF' by using <tt>Connection.setAutoCommit(false)</tt>. Enabling
|
|
auto-commit when using savepoints throws an exception.</li>
|
|
<li>Savepoints are not valid across XA connections. Using an XA connection
|
|
with savepoints throws an exception.</li>
|
|
<li>Your server must be running i5/OS™ Version 5 Release 2 or later. Using
|
|
savepoints when connecting (or already connected) to a server running V5R1
|
|
or earlier version of i5/OS throws an exception. </li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
|
|
</body>
|
|
</html> |