Debian Codegraph @

docs/gsg_txn/C/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.
52 </p>
53 <p>
54 Also, remember that when you are using the default isolation level,
55 every time your cursor reads a record it locks
56 that record until the encompassing transaction is resolved. This
57 means that walking your database with a transactional cursor
58 increases the chance of lock contention.
59 </p>
60 <p>
61 For this reason, if you must routinely walk your database with a
62 transactional cursor, consider using a reduced isolation level
63 such as read committed.
64 </p>
65 <div class="sect2" lang="en" xml:lang="en">
66 <div class="titlepage">
67 <div>
68 <div>
69 <h3 class="title"><a id="cursordirtyreads"></a>Using Cursors with Uncommitted Data</h3>
70 </div>
71 </div>
72 </div>
73 <p>
74
75 As described in <a class="xref" href="isolation.html#dirtyreads" title="Reading Uncommitted Data">Reading Uncommitted Data</a>
76 above, it is possible to relax your transaction's isolation
77 level such that it can read data modified but not yet committed
78 by another transaction. You can configure this when you create
79 your transaction handle, and when you do so then all cursors opened
80 inside that transaction will automatically use uncommitted reads.
81 </p>
82 <p>
83 You can also do this when you create a cursor handle from within
84 a serializable transaction. When you do this, only those
85 cursors configured for uncommitted reads uses uncommitted reads.
86 </p>
87 <p>
88 Either way, you must first configure your database
89 handle to support
90 uncommitted reads before you can configure your transactions or
91 your cursors to use them.
92 </p>
93 <p>
94 The following example shows how to configure an individual cursor handle
95 to read uncommitted data from within a serializable (full isolation) transaction.
96 For an example of
97 configuring a transaction to perform uncommitted reads in
98 general, see <a class="xref" href="isolation.html#dirtyreads" title="Reading Uncommitted Data">Reading Uncommitted Data</a>.
99 </p>
100 <pre class="programlisting">#include &lt;stdio.h&gt;
101 #include &lt;stdlib.h&gt;
102
103 #include "db.h"
104
105 int
106 main(void)
107 {
108 DB *dbp;
109 DBC *cursorp;
110 DB_ENV *envp;
111 DB_TXN *txn;
112 int ret, c_ret;
113 char *replacementString = "new string";
114
115 dbp = NULL;
116 envp = NULL;
117 txn = NULL;
118
119 /* Open the environment */
120 ret = db_env_create(&amp;envp, 0);
121 if (ret != 0) {
122 fprintf(stderr, "Error creating environment handle: %s\n",
123 db_strerror(ret));
124 return (EXIT_FAILURE);
125 }
126
127 env_flags = DB_CREATE | /* Create the environment if it does
128 * not already exist. */
129 DB_INIT_TXN | /* Initialize transactions */
130 DB_INIT_LOCK | /* Initialize locking. */
131 DB_INIT_LOG | /* Initialize logging */
132 DB_INIT_MPOOL; /* Initialize the in-memory cache. */
133
134 ret = envp-&gt;open(envp, db_home_dir, env_flags, 0);
135 if (ret != 0) {
136 fprintf(stderr, "Error opening environment: %s\n",
137 db_strerror(ret));
138 goto err;
139 }
140
141 /* Initialize the DB handle */
142 ret = db_create(&amp;dbp, envp, 0);
143 if (ret != 0) {
144 envp-&gt;err(envp, ret, "Database creation failed");
145 goto err;
146 }
147
148 db_flags = DB_CREATE | /* Create the db if it does not
149 * exist */
150 DB_AUTO_COMMIT | /* Enable auto commit */
151 DB_READ_UNCOMMITTED; /* Enable uncommitted reads */
152
153 ret = dbp-&gt;open(dbp, /* Pointer to the database */
154 NULL, /* Txn pointer */
155 file_name, /* File name */
156 NULL, /* Logical db name */
157 DB_BTREE, /* Database type (using btree) */
158 db_flags, /* Open flags */
159 0); /* File mode. Using defaults */
160 if (ret != 0) {
161 envp-&gt;err(envp, ret, "Database '%s' open failed",
162 file_name);
163 goto err;
164 }
165
166 /* Get the txn handle */
167 /* Note that this is a degree 3 transaction */
168 ret = envp-&gt;txn_begin(envp, NULL, &amp;txn, 0);
169 if (ret != 0) {
170 envp-&gt;err(envp, ret, "Transaction begin failed.");
171 goto err;
172 }
173
174 /* Get the cursor, supply the txn handle at that time */
175 /* Cause the cursor to perform uncommitted reads */
176 ret = dbp-&gt;cursor(dbp, txn, &amp;cursorp, DB_READ_UNCOMMITTED);
177 if (ret != 0) {
178 envp-&gt;err(envp, ret, "Cursor open failed.");
179 txn-&gt;abort(txn);
180 goto err;
181 }
182
183 /*
184 * From here, you perform your cursor reads and writes as normal,
185 * committing and aborting the transactions as is necessary, and
186 * testing for deadlock exceptions as normal (omitted for brevity).
187 */
188
189 ... </pre>
190 </div>
191 </div>
192 <div class="navfooter">
193 <hr />
194 <table width="100%" summary="Navigation footer">
195 <tr>
196 <td width="40%" align="left"><a accesskey="p" href="isolation.html">Prev</a> </td>
197 <td width="20%" align="center">
198 <a accesskey="u" href="txnconcurrency.html">Up</a>
199 </td>
200 <td width="40%" align="right"> <a accesskey="n" href="exclusivelock.html">Next</a></td>
201 </tr>
202 <tr>
203 <td width="40%" align="left" valign="top">Isolation </td>
204 <td width="20%" align="center">
205 <a accesskey="h" href="index.html">Home</a>
206 </td>
207 <td width="40%" align="right" valign="top"> Exclusive Database Handles</td>
208 </tr>
209 </table>
210 </div>
211 </body>
212 </html>