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