Debian Codegraph @

docs/gsg_txn/JAVA/txn_ccursor.html

raw
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 and Concurrent Applications</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="txnconcurrency.html" title="Chapter 4. Concurrency" />
11 <link rel="prev" href="isolation.html" title="Isolation" />
12 <link rel="next" href="exclusivelock.html" title="Exclusive Database Handles" />
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 and Concurrent Applications</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="isolation.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 4. Concurrency</th>
26 <td width="20%" align="right"> <a accesskey="n" href="exclusivelock.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="txn_ccursor"></a>Transactional Cursors and Concurrent Applications</h2>
36 </div>
37 </div>
38 </div>
39 <div class="toc">
40 <dl>
41 <dt>
42 <span class="sect2">
43 <a href="txn_ccursor.html#cursordirtyreads">Using Cursors with Uncommitted Data</a>
44 </span>
45 </dt>
46 </dl>
47 </div>
48 <p>
49 When you use transactional cursors with a concurrent application, remember that
50 in the event of a deadlock you must make sure that you close your cursor before you abort and retry your
51 transaction. <span>This is true of both
52 base API and DPL cursors.</span>
53 </p>
54 <p>
55 Also, remember that when you are using the default isolation level,
56 every time your cursor reads a record it locks
57 that record until the encompassing transaction is resolved. This
58 means that walking your database with a transactional cursor
59 increases the chance of lock contention.
60 </p>
61 <p>
62 For this reason, if you must routinely walk your database with a
63 transactional cursor, consider using a reduced isolation level
64 such as read committed. <span>This is
65 true of both base API and DPL cursors.</span>
66 </p>
67 <div class="sect2" lang="en" xml:lang="en">
68 <div class="titlepage">
69 <div>
70 <div>
71 <h3 class="title"><a id="cursordirtyreads"></a>Using Cursors with Uncommitted Data</h3>
72 </div>
73 </div>
74 </div>
75 <p>
76
77 As described in <a class="xref" href="isolation.html#dirtyreads" title="Reading Uncommitted Data">Reading Uncommitted Data</a>
78 above, it is possible to relax your transaction's isolation
79 level such that it can read data modified but not yet committed
80 by another transaction. You can configure this when you create
81 your transaction handle, and when you do so then all cursors opened
82 inside that transaction will automatically use uncommitted reads.
83 </p>
84 <p>
85 You can also do this when you create a cursor handle from within
86 a serializable transaction. When you do this, only those
87 cursors configured for uncommitted reads uses uncommitted reads.
88 </p>
89 <p>
90 Either way, you must first configure your database
91 <span>or store</span> handle to support
92 uncommitted reads before you can configure your transactions or
93 your cursors to use them.
94 </p>
95 <p>
96 The following example shows how to configure an individual cursor handle
97 to read uncommitted data from within a serializable (full isolation) transaction.
98 For an example of
99 configuring a transaction to perform uncommitted reads in
100 general, see <a class="xref" href="isolation.html#dirtyreads" title="Reading Uncommitted Data">Reading Uncommitted Data</a>.
101 </p>
102 <pre class="programlisting">package db.txn;
103
104 import com.sleepycat.db.Cursor;
105 import com.sleepycat.db.CursorConfig;
106 import com.sleepycat.db.Database;
107 import com.sleepycat.db.DatabaseConfig;
108 import com.sleepycat.db.Environment;
109 import com.sleepycat.db.EnvironmentConfig;
110
111 import java.io.File;
112
113 ...
114
115 Database myDatabase = null;
116 Environment myEnv = null;
117 try {
118
119 EnvironmentConfig myEnvConfig = new EnvironmentConfig();
120 myEnvConfig.setTransactional(true);
121 myEnvConfig.setInitializeCache(true);
122 myEnvConfig.setInitializeLocking(true);
123 myEnvConfig.setInitializeLogging(true);
124
125 myEnv = new Environment(new File("/my/env/home"),
126 myEnvConfig);
127
128 // Open the database.
129 DatabaseConfig dbConfig = new DatabaseConfig();
130 dbConfig.setTransactional(true);
131 dbConfig.setType(DatabaseType.BTREE);
132 dbConfig.setReadUncommitted(true); // Enable uncommitted reads.
133 myDatabase = myEnv.openDatabase(null, // txn handle
134 "sampleDatabase", // db file name
135 null, // db name
136 dbConfig);
137
138 // Open the transaction. Note that this is a degree 3
139 // transaction.
140 Transaction txn = myEnv.beginTransaction(null, null);
141 Cursor cursor = null;
142 try {
143 // Use the transaction handle here
144 // Get our cursor. Note that we pass the transaction
145 // handle here. Note also that we cause the cursor
146 // to perform uncommitted reads.
147 CursorConfig cconfig = new CursorConfig();
148 cconfig.setReadUncommitted(true);
149 cursor = db.openCursor(txn, cconfig);
150
151 // From here, you perform your cursor reads and writes
152 // as normal, committing and aborting the transactions as
153 // is necessary, and testing for deadlock exceptions as
154 // normal (omitted for brevity).
155
156 ... </pre>
157 <p>
158 If you are using the DPL:
159 </p>
160 <pre class="programlisting">package persist.txn;
161
162 import com.sleepycat.db.CursorConfig;
163 import com.sleepycat.db.DatabaseConfig;
164 import com.sleepycat.db.Environment;
165 import com.sleepycat.db.EnvironmentConfig;
166
167 import com.sleepycat.persist.EntityCursor;
168 import com.sleepycat.persist.EntityStore;
169 import com.sleepycat.persist.PrimaryIndex;
170 import com.sleepycat.persist.StoreConfig;
171
172 import java.util.Iterator;
173
174 import java.io.File;
175
176 ...
177
178 EntityStore myStore = null;
179 Environment myEnv = null;
180 PrimaryIndex&lt;String,AnEntityClass&gt; pKey;
181
182 try {
183
184 EnvironmentConfig myEnvConfig = new EnvironmentConfig();
185 myEnvConfig.setTransactional(true);
186 myEnvConfig.setInitializeCache(true);
187 myEnvConfig.setInitializeLocking(true);
188 myEnvConfig.setInitializeLogging(true);
189
190 myEnv = new Environment(new File("/my/env/home"),
191 myEnvConfig);
192
193 // Set up the entity store
194 StoreConfig myStoreConfig = new StoreConfig();
195 myStoreConfig.setAllowCreate(true);
196 myStoreConfig.setTransactional(true);
197
198 // Configure uncommitted reads
199 DatabaseConfig dbConfig = new DatabaseConfig();
200 dbConfig.setTransactional(true);
201 dbConfig.setType(DatabaseType.BTREE);
202 dbConfig.setAllowCreate(true);
203 dbConfig.setReadUncommitted(true); // Enable uncommitted reads.
204
205 // Instantiate the store
206 myStore = new EntityStore(myEnv, storeName, myStoreConfig);
207
208 // Set the DatabaseConfig object, so that the underlying
209 // database is configured for uncommitted reads.
210 myStore.setPrimaryConfig(AnEntityClass.class, myDbConfig);
211
212 // Open the transaction. Note that this is a degree 3
213 // transaction.
214 Transaction txn = myEnv.beginTransaction(null, null);
215
216 //Configure our cursor for uncommitted reads.
217 CursorConfig cconfig = new CursorConfig();
218 cconfig.setReadUncommitted(true);
219
220 // Get our cursor. Note that we pass the transaction
221 // handle here. Note also that we cause the cursor
222 // to perform uncommitted reads.
223 EntityCursor&lt;AnEntityClass&gt; cursor = pKey.entities(txn, cconfig);
224
225 try {
226 // From here, you perform your cursor reads and writes
227 // as normal, committing and aborting the transactions as
228 // is necessary, and testing for deadlock exceptions as
229 // normal (omitted for brevity).
230
231 ... </pre>
232 </div>
233 </div>
234 <div class="navfooter">
235 <hr />
236 <table width="100%" summary="Navigation footer">
237 <tr>
238 <td width="40%" align="left"><a accesskey="p" href="isolation.html">Prev</a> </td>
239 <td width="20%" align="center">
240 <a accesskey="u" href="txnconcurrency.html">Up</a>
241 </td>
242 <td width="40%" align="right"> <a accesskey="n" href="exclusivelock.html">Next</a></td>
243 </tr>
244 <tr>
245 <td width="40%" align="left" valign="top">Isolation </td>
246 <td width="20%" align="center">
247 <a accesskey="h" href="index.html">Home</a>
248 </td>
249 <td width="40%" align="right" valign="top"> Exclusive Database Handles</td>
250 </tr>
251 </table>
252 </div>
253 </body>
254 </html>