• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt/router/db-4.8.30/docs/gsg_txn/JAVA/
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml">
4  <head>
5    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6    <title>Transactional Cursors</title>
7    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9    <link rel="start" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
10    <link rel="up" href="usingtxns.html" title="Chapter��3.��Transaction Basics" />
11    <link rel="prev" href="nestedtxn.html" title="Nested Transactions" />
12    <link rel="next" href="txnindices.html" title="Secondary Indices with Transaction Applications" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Transactional Cursors</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="nestedtxn.html">Prev</a>��</td>
22          <th width="60%" align="center">Chapter��3.��Transaction Basics</th>
23          <td width="20%" align="right">��<a accesskey="n" href="txnindices.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="sect1" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title" style="clear: both"><a id="txncursor"></a>Transactional Cursors</h2>
33          </div>
34        </div>
35      </div>
36      <div class="toc">
37        <dl>
38          <dt>
39            <span class="sect2">
40              <a href="txncursor.html#dplcursors">Using Transactional DPL Cursors</a>
41            </span>
42          </dt>
43        </dl>
44      </div>
45      <p>
46            You can transaction-protect your cursor operations by
47            specifying a transaction handle at the time that you create
48            your cursor.  Beyond that, you do not ever
49            provide a transaction handle directly to a cursor method.
50        </p>
51      <p>
52            Note that if you transaction-protect a cursor, then you must
53            make sure that the cursor is closed before you either commit or
54            abort the transaction. For example:
55        </p>
56      <pre class="programlisting">package db.txn;
57
58import com.sleepycat.db.Cursor;
59import com.sleepycat.db.Database;
60import com.sleepycat.db.DatabaseConfig;
61import com.sleepycat.db.DatabaseEntry;
62import com.sleepycat.db.DatabaseException;
63import com.sleepycat.db.Environment;
64import com.sleepycat.db.EnvironmentConfig;
65import com.sleepycat.db.LockMode;
66import com.sleepycat.db.OperationStatus;
67import com.sleepycat.db.Transaction;
68
69import java.io.File;
70import java.io.FileNotFoundException;
71
72...
73
74Database myDatabase = null;
75Environment myEnv = null;
76try {
77
78    // Database and environment opens omitted
79
80    String replacementData = "new data";
81
82    Transaction txn = myEnv.beginTransaction(null, null);
83    Cursor cursor = null;
84    try {
85        // Use the transaction handle here
86        cursor = db.openCursor(txn, null);
87        DatabaseEntry key, data;
88        
89        DatabaseEntry key, data;
90        while(cursor.getNext(key, data, LockMode.DEFAULT) ==
91           OperationStatus.SUCCESS) {
92            
93            data.setData(replacementData.getBytes("UTF-8"));
94            // No transaction handle is used on the cursor read or write
95            // methods.
96            cursor.putCurrent(data);
97        }
98        
99        cursor.close();
100        cursor = null;
101        txn.commit();
102        txn = null;
103    } catch (Exception e) {
104        if (cursor != null) {
105            cursor.close();
106        }
107        if (txn != null) {
108            txn.abort();
109            txn = null;
110        }
111    }
112
113} catch (DatabaseException de) {
114    // Exception handling goes here
115} </pre>
116      <div class="sect2" lang="en" xml:lang="en">
117        <div class="titlepage">
118          <div>
119            <div>
120              <h3 class="title"><a id="dplcursors"></a>Using Transactional DPL Cursors</h3>
121            </div>
122          </div>
123        </div>
124        <p>
125            When using the DPL, you create the cursor using the entity
126            class's primary or secondary index (see the
127            
128            <em class="citetitle">Getting Started with Berkeley DB for Java</em>
129            guide for details). At the time that you create the cursor, you
130            pass a transaction handle to the <code class="methodname">entities()</code>
131            method, and this causes all subsequent operations performed
132            using that cursor to be performed within the scope of the
133            transaction.
134        </p>
135        <p>
136                Note that if you are using a transaction-enabled store,
137                then you must provide a transaction handle when you open
138                your cursor.
139        </p>
140        <p>
141                For example:
142        </p>
143        <pre class="programlisting">package persist.txn;
144
145import com.sleepycat.db.DatabaseException;
146import com.sleepycat.db.Environment;
147import com.sleepycat.db.EnvironmentConfig;
148import com.sleepycat.db.Transaction;
149
150import com.sleepycat.persist.EntityCursor;
151import com.sleepycat.persist.EntityStore;
152import com.sleepycat.persist.PrimaryIndex;
153
154import java.io.File;
155import java.io.FileNotFoundException;
156
157...
158
159Environment myEnv = null;
160EntityStore store = null;
161
162...
163
164
165    // Store and environment open omitted, as is the DataAccessor
166    // instantiation.
167
168...
169
170    Transaction txn = myEnv.beginTransaction(null, null);
171    PrimaryIndex&lt;String,Inventory&gt; pi =
172        store.getPrimaryIndex(String.class, Inventory.class);
173    EntityCursor&lt;Inventory&gt; pi_cursor = pi.entities(txn, null);
174
175    try {
176        for (Inventory ii : pi_cursor) {
177            // do something with each object "ii"
178            // A transactional handle is not required for any write operations.
179            // All operations performed using this cursor will be done within
180            // the scope of the transaction, txn.
181        }
182        pi_cursor.close();
183        pi_cursor = null;
184        txn.commit();
185        txn = null;
186    // Always make sure the cursor is closed when we are done with it.
187    } catch (Exception e) {
188        if (pi_cursor != null) {
189            pi_cursor.close();
190        }
191        if (txn != null) {
192            txn.abort();
193            txn = null;
194        }
195    } </pre>
196      </div>
197    </div>
198    <div class="navfooter">
199      <hr />
200      <table width="100%" summary="Navigation footer">
201        <tr>
202          <td width="40%" align="left"><a accesskey="p" href="nestedtxn.html">Prev</a>��</td>
203          <td width="20%" align="center">
204            <a accesskey="u" href="usingtxns.html">Up</a>
205          </td>
206          <td width="40%" align="right">��<a accesskey="n" href="txnindices.html">Next</a></td>
207        </tr>
208        <tr>
209          <td width="40%" align="left" valign="top">Nested Transactions��</td>
210          <td width="20%" align="center">
211            <a accesskey="h" href="index.html">Home</a>
212          </td>
213          <td width="40%" align="right" valign="top">��Secondary Indices with Transaction Applications</td>
214        </tr>
215      </table>
216    </div>
217  </body>
218</html>
219