| 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 4. Concurrency</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="index.html" title="Getting Started with Berkeley DB Transaction Processing" /> |
| 11 |
<link rel="prev" href="maxtxns.html" title="Configuring the Transaction Subsystem" /> |
| 12 |
<link rel="next" href="blocking_deadlocks.html" title="Locks, Blocks, and Deadlocks" /> |
| 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 4. Concurrency</th> |
| 22 |
</tr> |
| 23 |
<tr> |
| 24 |
<td width="20%" align="left"><a accesskey="p" href="maxtxns.html">Prev</a> </td> |
| 25 |
<th width="60%" align="center"> </th> |
| 26 |
<td width="20%" align="right"> <a accesskey="n" href="blocking_deadlocks.html">Next</a></td> |
| 27 |
</tr> |
| 28 |
</table> |
| 29 |
<hr /> |
| 30 |
</div> |
| 31 |
<div class="chapter" lang="en" xml:lang="en"> |
| 32 |
<div class="titlepage"> |
| 33 |
<div> |
| 34 |
<div> |
| 35 |
<h2 class="title"><a id="txnconcurrency"></a>Chapter 4. Concurrency</h2> |
| 36 |
</div> |
| 37 |
</div> |
| 38 |
</div> |
| 39 |
<div class="toc"> |
| 40 |
<p> |
| 41 |
<b>Table of Contents</b> |
| 42 |
</p> |
| 43 |
<dl> |
| 44 |
<dt> |
| 45 |
<span class="sect1"> |
| 46 |
<a href="txnconcurrency.html#concurrenthandles">Which DB Handles are Free-Threaded</a> |
| 47 |
</span> |
| 48 |
</dt> |
| 49 |
<dt> |
| 50 |
<span class="sect1"> |
| 51 |
<a href="blocking_deadlocks.html">Locks, Blocks, and Deadlocks</a> |
| 52 |
</span> |
| 53 |
</dt> |
| 54 |
<dd> |
| 55 |
<dl> |
| 56 |
<dt> |
| 57 |
<span class="sect2"> |
| 58 |
<a href="blocking_deadlocks.html#locks">Locks</a> |
| 59 |
</span> |
| 60 |
</dt> |
| 61 |
<dt> |
| 62 |
<span class="sect2"> |
| 63 |
<a href="blocking_deadlocks.html#blocks">Blocks</a> |
| 64 |
</span> |
| 65 |
</dt> |
| 66 |
<dt> |
| 67 |
<span class="sect2"> |
| 68 |
<a href="blocking_deadlocks.html#deadlocks">Deadlocks</a> |
| 69 |
</span> |
| 70 |
</dt> |
| 71 |
</dl> |
| 72 |
</dd> |
| 73 |
<dt> |
| 74 |
<span class="sect1"> |
| 75 |
<a href="lockingsubsystem.html">The Locking Subsystem</a> |
| 76 |
</span> |
| 77 |
</dt> |
| 78 |
<dd> |
| 79 |
<dl> |
| 80 |
<dt> |
| 81 |
<span class="sect2"> |
| 82 |
<a href="lockingsubsystem.html#configuringlock">Configuring the Locking Subsystem</a> |
| 83 |
</span> |
| 84 |
</dt> |
| 85 |
<dt> |
| 86 |
<span class="sect2"> |
| 87 |
<a href="lockingsubsystem.html#configdeadlkdetect">Configuring Deadlock Detection</a> |
| 88 |
</span> |
| 89 |
</dt> |
| 90 |
<dt> |
| 91 |
<span class="sect2"> |
| 92 |
<a href="lockingsubsystem.html#deadlockresolve">Resolving Deadlocks</a> |
| 93 |
</span> |
| 94 |
</dt> |
| 95 |
<dt> |
| 96 |
<span class="sect2"> |
| 97 |
<a href="lockingsubsystem.html#setpriority">Setting Transaction Priorities</a> |
| 98 |
</span> |
| 99 |
</dt> |
| 100 |
</dl> |
| 101 |
</dd> |
| 102 |
<dt> |
| 103 |
<span class="sect1"> |
| 104 |
<a href="isolation.html">Isolation</a> |
| 105 |
</span> |
| 106 |
</dt> |
| 107 |
<dd> |
| 108 |
<dl> |
| 109 |
<dt> |
| 110 |
<span class="sect2"> |
| 111 |
<a href="isolation.html#degreesofisolation">Supported Degrees of Isolation</a> |
| 112 |
</span> |
| 113 |
</dt> |
| 114 |
<dt> |
| 115 |
<span class="sect2"> |
| 116 |
<a href="isolation.html#dirtyreads">Reading Uncommitted Data</a> |
| 117 |
</span> |
| 118 |
</dt> |
| 119 |
<dt> |
| 120 |
<span class="sect2"> |
| 121 |
<a href="isolation.html#readcommitted">Committed Reads</a> |
| 122 |
</span> |
| 123 |
</dt> |
| 124 |
<dt> |
| 125 |
<span class="sect2"> |
| 126 |
<a href="isolation.html#snapshot_isolation">Using Snapshot Isolation</a> |
| 127 |
</span> |
| 128 |
</dt> |
| 129 |
</dl> |
| 130 |
</dd> |
| 131 |
<dt> |
| 132 |
<span class="sect1"> |
| 133 |
<a href="txn_ccursor.html">Transactional Cursors and Concurrent Applications</a> |
| 134 |
</span> |
| 135 |
</dt> |
| 136 |
<dd> |
| 137 |
<dl> |
| 138 |
<dt> |
| 139 |
<span class="sect2"> |
| 140 |
<a href="txn_ccursor.html#cursordirtyreads">Using Cursors with Uncommitted Data</a> |
| 141 |
</span> |
| 142 |
</dt> |
| 143 |
</dl> |
| 144 |
</dd> |
| 145 |
<dt> |
| 146 |
<span class="sect1"> |
| 147 |
<a href="exclusivelock.html">Exclusive Database Handles</a> |
| 148 |
</span> |
| 149 |
</dt> |
| 150 |
<dt> |
| 151 |
<span class="sect1"> |
| 152 |
<a href="readmodifywrite.html">Read/Modify/Write</a> |
| 153 |
</span> |
| 154 |
</dt> |
| 155 |
<dt> |
| 156 |
<span class="sect1"> |
| 157 |
<a href="txnnowait.html">No Wait on Blocks</a> |
| 158 |
</span> |
| 159 |
</dt> |
| 160 |
<dt> |
| 161 |
<span class="sect1"> |
| 162 |
<a href="reversesplit.html">Reverse BTree Splits</a> |
| 163 |
</span> |
| 164 |
</dt> |
| 165 |
</dl> |
| 166 |
</div> |
| 167 |
<p> |
| 168 |
|
| 169 |
DB offers a great deal of support for multi-threaded |
| 170 |
<span> |
| 171 |
and multi-process |
| 172 |
</span> |
| 173 |
applications even when transactions are not in use. Many of DB's |
| 174 |
handles are |
| 175 |
thread-safe<span>, or |
| 176 |
can be made thread-safe by providing the appropriate flag at handle creation time,</span> |
| 177 |
and DB provides a |
| 178 |
flexible locking subsystem for managing databases in a concurrent |
| 179 |
application. Further, DB provides a robust mechanism for |
| 180 |
detecting and responding to |
| 181 |
<span>deadlocks</span> |
| 182 |
. |
| 183 |
All of these concepts are |
| 184 |
explored in this chapter. |
| 185 |
</p> |
| 186 |
<p> |
| 187 |
Before continuing, it is useful to define a few terms that will appear |
| 188 |
throughout this chapter: |
| 189 |
</p> |
| 190 |
<div class="itemizedlist"> |
| 191 |
<ul type="disc"> |
| 192 |
<li> |
| 193 |
<p> |
| 194 |
<span class="emphasis"><em>Thread of control</em></span> |
| 195 |
</p> |
| 196 |
<p> |
| 197 |
Refers to a thread that is performing work in your application. |
| 198 |
Typically, in this book that thread will be performing DB |
| 199 |
operations. |
| 200 |
</p> |
| 201 |
<p> |
| 202 |
Note that this term can also be taken to mean a separate process |
| 203 |
that is performing work — DB supports multi-process |
| 204 |
operations on your databases. |
| 205 |
</p> |
| 206 |
<p> |
| 207 |
Also, DB is agnostic with regard to the type or style of |
| 208 |
threads in use in your application. So if you are using |
| 209 |
multiple threads |
| 210 |
(as opposed to multiple processes) to perform concurrent |
| 211 |
database access, you are free to use whatever thread package is |
| 212 |
best for your platform and application. That said, this manual |
| 213 |
will use pthreads for its threading examples because those have |
| 214 |
the best chance of being supported across a large range of |
| 215 |
platforms. |
| 216 |
</p> |
| 217 |
</li> |
| 218 |
<li> |
| 219 |
<p> |
| 220 |
<span class="emphasis"><em>Locking</em></span> |
| 221 |
</p> |
| 222 |
<p> |
| 223 |
When a thread of control obtains |
| 224 |
access to a shared resource, it is said to be |
| 225 |
<span class="emphasis"><em>locking</em></span> that resource. Note that |
| 226 |
DB supports both exclusive and non-exclusive locks. See |
| 227 |
<a class="xref" href="blocking_deadlocks.html#locks" title="Locks">Locks</a> for more information. |
| 228 |
</p> |
| 229 |
</li> |
| 230 |
<li> |
| 231 |
<p> |
| 232 |
<span class="emphasis"><em>Free-threaded</em></span> |
| 233 |
</p> |
| 234 |
<p> |
| 235 |
Data structures and objects are free-threaded if they can be |
| 236 |
shared across threads of control without any explicit locking on |
| 237 |
the part of the application. Some books, libraries, and |
| 238 |
programming languages may use the term |
| 239 |
<span class="emphasis"><em>thread-safe</em></span> for data structures or objects |
| 240 |
that have this characteristic. The two terms mean the |
| 241 |
same thing. |
| 242 |
</p> |
| 243 |
<p> |
| 244 |
For a description of free-threaded DB objects, see |
| 245 |
<a class="xref" href="txnconcurrency.html#concurrenthandles" title="Which DB Handles are Free-Threaded">Which DB Handles are Free-Threaded</a>. |
| 246 |
</p> |
| 247 |
</li> |
| 248 |
<li> |
| 249 |
<p> |
| 250 |
<span class="emphasis"><em>Blocked</em></span> |
| 251 |
</p> |
| 252 |
<p> |
| 253 |
When a thread cannot obtain a lock because some other |
| 254 |
thread already holds a lock on that object, the lock |
| 255 |
attempt is said to be <span class="emphasis"><em>blocked</em></span>. See |
| 256 |
<a class="xref" href="blocking_deadlocks.html#blocks" title="Blocks">Blocks</a> for more information. |
| 257 |
</p> |
| 258 |
</li> |
| 259 |
<li> |
| 260 |
<p> |
| 261 |
<span class="emphasis"><em>Deadlock</em></span> |
| 262 |
</p> |
| 263 |
<p> |
| 264 |
Occurs when two or more threads of control attempt to access conflicting resource in such a way as none |
| 265 |
of the threads can any longer make further progress. |
| 266 |
</p> |
| 267 |
<p> |
| 268 |
For example, if Thread A is blocked waiting for a resource held by Thread |
| 269 |
B, while at the same time Thread B is blocked waiting for a |
| 270 |
resource held by Thread A, then neither thread can make any |
| 271 |
forward progress. In this situation, Thread A and Thread B |
| 272 |
are said to be <span class="emphasis"><em>deadlocked.</em></span> |
| 273 |
</p> |
| 274 |
<p> |
| 275 |
For more information, see <a class="xref" href="blocking_deadlocks.html#deadlocks" title="Deadlocks">Deadlocks</a>. |
| 276 |
</p> |
| 277 |
</li> |
| 278 |
</ul> |
| 279 |
</div> |
| 280 |
<div class="sect1" lang="en" xml:lang="en"> |
| 281 |
<div class="titlepage"> |
| 282 |
<div> |
| 283 |
<div> |
| 284 |
<h2 class="title" style="clear: both"><a id="concurrenthandles"></a>Which DB Handles are Free-Threaded</h2> |
| 285 |
</div> |
| 286 |
</div> |
| 287 |
</div> |
| 288 |
<p> |
| 289 |
The following describes to what extent and under what conditions |
| 290 |
individual handles are free-threaded. |
| 291 |
</p> |
| 292 |
<div class="itemizedlist"> |
| 293 |
<ul type="disc"> |
| 294 |
<li> |
| 295 |
<p> |
| 296 |
<code class="classname">DB_ENV</code> |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
</p> |
| 301 |
<p> |
| 302 |
Free-threaded so long as |
| 303 |
<span> |
| 304 |
the <code class="literal">DB_THREAD</code> flag is provided to |
| 305 |
the environment <code class="methodname">open()</code> |
| 306 |
method. |
| 307 |
</span> |
| 308 |
|
| 309 |
</p> |
| 310 |
</li> |
| 311 |
<li> |
| 312 |
<p> |
| 313 |
<code class="classname">DB</code> |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
</p> |
| 318 |
<p> |
| 319 |
Free-threaded so long as |
| 320 |
<span> |
| 321 |
the <code class="literal">DB_THREAD</code> flag is provided to |
| 322 |
the database <code class="methodname">open()</code> |
| 323 |
method, or if the database is opened using a |
| 324 |
free-threaded environment handle. |
| 325 |
</span> |
| 326 |
|
| 327 |
</p> |
| 328 |
</li> |
| 329 |
<li> |
| 330 |
<p> |
| 331 |
<code class="classname">DBC</code> |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
</p> |
| 336 |
<p> |
| 337 |
Cursors are not free-threaded. However, they |
| 338 |
can be used by multiple threads of control so |
| 339 |
long as the application serializes access to the handle. |
| 340 |
</p> |
| 341 |
</li> |
| 342 |
<li> |
| 343 |
<p> |
| 344 |
<code class="classname">DB_TXN</code> |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
</p> |
| 349 |
<p> |
| 350 |
Access must be serialized by the application across threads of control. |
| 351 |
</p> |
| 352 |
</li> |
| 353 |
</ul> |
| 354 |
</div> |
| 355 |
</div> |
| 356 |
</div> |
| 357 |
<div class="navfooter"> |
| 358 |
<hr /> |
| 359 |
<table width="100%" summary="Navigation footer"> |
| 360 |
<tr> |
| 361 |
<td width="40%" align="left"><a accesskey="p" href="maxtxns.html">Prev</a> </td> |
| 362 |
<td width="20%" align="center"> </td> |
| 363 |
<td width="40%" align="right"> <a accesskey="n" href="blocking_deadlocks.html">Next</a></td> |
| 364 |
</tr> |
| 365 |
<tr> |
| 366 |
<td width="40%" align="left" valign="top">Configuring the Transaction Subsystem </td> |
| 367 |
<td width="20%" align="center"> |
| 368 |
<a accesskey="h" href="index.html">Home</a> |
| 369 |
</td> |
| 370 |
<td width="40%" align="right" valign="top"> Locks, Blocks, and Deadlocks</td> |
| 371 |
</tr> |
| 372 |
</table> |
| 373 |
</div> |
| 374 |
</body> |
| 375 |
</html> |