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