Debian Codegraph @

docs/programmer_reference/cam.html

raw symbols SCIP
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>Chapter 10.  Berkeley DB Concurrent Data Store 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="Berkeley DB Programmer's Reference Guide" />
10 <link rel="up" href="index.html" title="Berkeley DB Programmer's Reference Guide" />
11 <link rel="prev" href="env_faq.html" title="Environment FAQ" />
12 <link rel="next" href="cam_fail.html" title="Handling failure in Data Store and Concurrent Data Store 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">Chapter 10. 
22 Berkeley DB Concurrent Data Store Applications
23 </th>
24 </tr>
25 <tr>
26 <td width="20%" align="left"><a accesskey="p" href="env_faq.html">Prev</a> </td>
27 <th width="60%" align="center"> </th>
28 <td width="20%" align="right"> <a accesskey="n" href="cam_fail.html">Next</a></td>
29 </tr>
30 </table>
31 <hr />
32 </div>
33 <div class="chapter" lang="en" xml:lang="en">
34 <div class="titlepage">
35 <div>
36 <div>
37 <h2 class="title"><a id="cam"></a>Chapter 10. 
38 Berkeley DB Concurrent Data Store Applications
39 </h2>
40 </div>
41 </div>
42 </div>
43 <div class="toc">
44 <p>
45 <b>Table of Contents</b>
46 </p>
47 <dl>
48 <dt>
49 <span class="sect1">
50 <a href="cam.html#cam_intro">Concurrent Data Store introduction</a>
51 </span>
52 </dt>
53 <dt>
54 <span class="sect1">
55 <a href="cam_fail.html">Handling failure in Data Store and Concurrent Data Store applications</a>
56 </span>
57 </dt>
58 <dt>
59 <span class="sect1">
60 <a href="cam_app.html">Architecting Data Store and Concurrent Data Store applications</a>
61 </span>
62 </dt>
63 </dl>
64 </div>
65 <div class="sect1" lang="en" xml:lang="en">
66 <div class="titlepage">
67 <div>
68 <div>
69 <h2 class="title" style="clear: both"><a id="cam_intro"></a>Concurrent Data Store introduction</h2>
70 </div>
71 </div>
72 </div>
73 <p>It is often desirable to have concurrent read-write access to a database
74 when there is no need for full recoverability or transaction semantics.
75 For this class of applications, Berkeley DB provides interfaces supporting
76 deadlock-free, multiple-reader/single writer access to the database.
77 This means that at any instant in time, there may be either multiple
78 readers accessing data or a single writer modifying data. The
79 application is entirely unaware of which is happening, and Berkeley DB
80 implements the necessary locking and blocking to ensure this behavior.</p>
81 <p>To create Berkeley DB Concurrent Data Store applications, you must first initialize an environment
82 by calling <a href="../api_reference/C/envopen.html" class="olink">DB_ENV-&gt;open()</a>. You must specify the <a href="../api_reference/C/envopen.html#envopen_DB_INIT_CDB" class="olink">DB_INIT_CDB</a> and <a href="../api_reference/C/envopen.html#envopen_DB_INIT_MPOOL" class="olink">DB_INIT_MPOOL</a>
83 flags to that method. It is an error to
84 specify any of the other <a href="../api_reference/C/envopen.html" class="olink">DB_ENV-&gt;open()</a> subsystem or recovery
85 configuration flags, for example, <a href="../api_reference/C/envopen.html#envopen_DB_INIT_LOCK" class="olink">DB_INIT_LOCK</a>, <a href="../api_reference/C/envopen.html#envopen_DB_INIT_TXN" class="olink">DB_INIT_TXN</a> or <a href="../api_reference/C/envopen.html#envopen_DB_RECOVER" class="olink">DB_RECOVER</a>
86 All databases must, of
87 course, be created in this environment by using the <a href="../api_reference/C/dbcreate.html" class="olink">db_create()</a>
88 function or <a href="../api_reference/CXX/db.html" class="olink">Db</a> constructor, and specifying the environment
89 as an argument.</p>
90 <p>Berkeley DB performs appropriate locking so that safe enforcement of the
91 deadlock-free, multiple-reader/single-writer semantic is transparent to
92 the application. However, a basic understanding of Berkeley DB Concurrent Data Store locking
93 behavior is helpful when writing Berkeley DB Concurrent Data Store applications.</p>
94 <p>Berkeley DB Concurrent Data Store
95 avoids deadlocks without the need for a deadlock detector by performing
96 all locking on an entire database at once (or on an entire environment
97 in the case of the <a href="../api_reference/C/envset_flags.html#set_flags_DB_CDB_ALLDB" class="olink">DB_CDB_ALLDB</a> flag), and by ensuring that at
98 any given time only one thread of control is allowed to simultaneously
99 hold a read (shared) lock and attempt to acquire a write (exclusive)
100 lock.</p>
101 <p>All open Berkeley DB cursors hold a read lock, which serves as a guarantee
102 that the database will not change beneath them; likewise, all
103 non-cursor <a href="../api_reference/C/dbget.html" class="olink">DB-&gt;get()</a> operations temporarily acquire and release
104 a read lock that is held during the actual traversal of the database.
105 Because read locks will not conflict with each other, any number of
106 cursors in any number of threads of control may be open simultaneously,
107 and any number of <a href="../api_reference/C/dbget.html" class="olink">DB-&gt;get()</a> operations may be concurrently in
108 progress.</p>
109 <p>To enforce the rule that only one thread of control at a time can
110 attempt to upgrade a read lock to a write lock, however, Berkeley DB must
111 forbid multiple cursors from attempting to write concurrently. This is
112 done using the <a href="../api_reference/C/dbcursor.html#cursor_DB_WRITECURSOR" class="olink">DB_WRITECURSOR</a> flag to the <a href="../api_reference/C/dbcursor.html" class="olink">DB-&gt;cursor()</a> method.
113 This is the only difference between access method calls in Berkeley DB Concurrent Data Store and
114 in the other Berkeley DB products. The <a href="../api_reference/C/dbcursor.html#cursor_DB_WRITECURSOR" class="olink">DB_WRITECURSOR</a> flag causes the
115 newly created cursor to be a "write" cursor; that is, a cursor capable
116 of performing writes as well as reads. Only cursors thus created are
117 permitted to perform write operations (either deletes or puts), and only
118 one such cursor can exist at any given time.</p>
119 <p>Any attempt to create a second write cursor or to perform a non-cursor
120 write operation while a write cursor is open will block until that write
121 cursor is closed. Read cursors may open and perform reads without blocking
122 while a write cursor is extant. However, any attempts to actually perform
123 a write, either using the write cursor or directly using the
124 <a href="../api_reference/C/dbput.html" class="olink">DB-&gt;put()</a> or <a href="../api_reference/C/dbdel.html" class="olink">DB-&gt;del()</a> methods, will block until all read cursors
125 are closed. This is how the multiple-reader/single-writer semantic is
126 enforced, and prevents reads from seeing an inconsistent database state
127 that may be an intermediate stage of a write operation.</p>
128 <p>By default, Berkeley DB Concurrent Data Store does locking on a per-database basis. For this
129 reason, using cursors to access multiple databases in different orders
130 in different threads or processes, or leaving cursors open on one
131 database while accessing another database, can cause an application to
132 hang. If this behavior is a requirement for the application, Berkeley DB
133 should be configured to do locking on an environment-wide basis. See
134 the <a href="../api_reference/C/envset_flags.html#set_flags_DB_CDB_ALLDB" class="olink">DB_CDB_ALLDB</a> flag of the <a href="../api_reference/C/envset_flags.html" class="olink">DB_ENV-&gt;set_flags()</a> method
135 for more information.</p>
136 <p>With these behaviors, Berkeley DB can guarantee deadlock-free concurrent
137 database access, so that multiple threads of control are free to perform
138 reads and writes without needing to handle synchronization themselves
139 or having to run a deadlock detector. Berkeley DB has no direct knowledge of
140 which cursors belong to which threads, so some care must be taken to
141 ensure that applications do not inadvertently block themselves, causing
142 the application to hang and be unable to proceed.</p>
143 <p>As a consequence of the Berkeley DB Concurrent Data Store locking model, the following sequences
144 of operations will cause a thread to block itself indefinitely:</p>
145 <div class="orderedlist">
146 <ol type="1">
147 <li>Keeping a cursor open while issuing a <a href="../api_reference/C/dbput.html" class="olink">DB-&gt;put()</a> or <a href="../api_reference/C/dbdel.html" class="olink">DB-&gt;del()</a>
148 access method call.</li>
149 <li>Attempting to open a write cursor while another cursor is already being
150 held open by the same thread of control. Note that it is correct
151 operation for one thread of control to attempt to open a write cursor
152 or to perform a non-cursor write (<a href="../api_reference/C/dbput.html" class="olink">DB-&gt;put()</a> or <a href="../api_reference/C/dbdel.html" class="olink">DB-&gt;del()</a>)
153 while a write cursor is already active in another thread. It is only a
154 problem if these things are done within a single thread of control --
155 in which case that thread will block and never be able to release the
156 lock that is blocking it.</li>
157 <li>Not testing Berkeley DB error return codes (if any cursor operation returns
158 an unexpected error, that cursor must still be closed).</li>
159 </ol>
160 </div>
161 <p>If the application needs to open multiple cursors in a single thread to
162 perform an operation, it can indicate to Berkeley DB that the cursor locks
163 should not block each other by creating a Berkeley DB Concurrent Data Store <span class="bold"><strong>group</strong></span>, using
164 <a href="../api_reference/C/envcdsgroup_begin.html" class="olink">DB_ENV-&gt;cdsgroup_begin()</a>. This creates a locker ID that is shared by all
165 cursors opened in the group.</p>
166 <p>Berkeley DB Concurrent Data Store groups use a <a href="../api_reference/C/txn.html" class="olink">TXN</a> handle to indicate the shared locker
167 ID to Berkeley DB calls, and call <a href="../api_reference/C/txncommit.html" class="olink">DB_TXN-&gt;commit()</a> to end the group. This
168 is a convenient way to pass the locked ID to the calls where it is
169 needed, but should not be confused with the real transactional semantics
170 provided by Berkeley DB Transactional Data Store. In particular, Berkeley DB Concurrent Data Store groups do not provide any
171 abort or recovery facilities, and have no impact on durability of
172 operations.</p>
173 </div>
174 </div>
175 <div class="navfooter">
176 <hr />
177 <table width="100%" summary="Navigation footer">
178 <tr>
179 <td width="40%" align="left"><a accesskey="p" href="env_faq.html">Prev</a> </td>
180 <td width="20%" align="center"> </td>
181 <td width="40%" align="right"> <a accesskey="n" href="cam_fail.html">Next</a></td>
182 </tr>
183 <tr>
184 <td width="40%" align="left" valign="top">Environment FAQ </td>
185 <td width="20%" align="center">
186 <a accesskey="h" href="index.html">Home</a>
187 </td>
188 <td width="40%" align="right" valign="top"> Handling failure in Data Store and Concurrent Data Store applications</td>
189 </tr>
190 </table>
191 </div>
192 </body>
193 </html>