| 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>Locks, Blocks, and Deadlocks</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="txnconcurrency.html" title="Chapter 4. Concurrency" /> |
| 12 |
<link rel="next" href="lockingsubsystem.html" title="The Locking Subsystem" /> |
| 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">Locks, Blocks, and Deadlocks</th> |
| 22 |
</tr> |
| 23 |
<tr> |
| 24 |
<td width="20%" align="left"><a accesskey="p" href="txnconcurrency.html">Prev</a> </td> |
| 25 |
<th width="60%" align="center">Chapter 4. Concurrency</th> |
| 26 |
<td width="20%" align="right"> <a accesskey="n" href="lockingsubsystem.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="blocking_deadlocks"></a>Locks, Blocks, and Deadlocks</h2> |
| 36 |
</div> |
| 37 |
</div> |
| 38 |
</div> |
| 39 |
<div class="toc"> |
| 40 |
<dl> |
| 41 |
<dt> |
| 42 |
<span class="sect2"> |
| 43 |
<a href="blocking_deadlocks.html#locks">Locks</a> |
| 44 |
</span> |
| 45 |
</dt> |
| 46 |
<dt> |
| 47 |
<span class="sect2"> |
| 48 |
<a href="blocking_deadlocks.html#blocks">Blocks</a> |
| 49 |
</span> |
| 50 |
</dt> |
| 51 |
<dt> |
| 52 |
<span class="sect2"> |
| 53 |
<a href="blocking_deadlocks.html#deadlocks">Deadlocks</a> |
| 54 |
</span> |
| 55 |
</dt> |
| 56 |
</dl> |
| 57 |
</div> |
| 58 |
<p> |
| 59 |
It is important to understand how locking works in a |
| 60 |
concurrent application before continuing with a description of |
| 61 |
the concurrency mechanisms DB makes available to you. |
| 62 |
Blocking and deadlocking have important performance implications |
| 63 |
for your application. Consequently, this section provides a |
| 64 |
fundamental description of these concepts, and how they affect |
| 65 |
DB operations. |
| 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="locks"></a>Locks</h3> |
| 72 |
</div> |
| 73 |
</div> |
| 74 |
</div> |
| 75 |
<p> |
| 76 |
When one thread of control wants to obtain access to an |
| 77 |
object, it requests a <span class="emphasis"><em>lock</em></span> for that |
| 78 |
object. This lock is what allows DB to provide your |
| 79 |
application with its transactional isolation guarantees by |
| 80 |
ensuring that: |
| 81 |
</p> |
| 82 |
<div class="itemizedlist"> |
| 83 |
<ul type="disc"> |
| 84 |
<li> |
| 85 |
<p> |
| 86 |
no other thread of control can read that object (in |
| 87 |
the case of an exclusive lock), and |
| 88 |
</p> |
| 89 |
</li> |
| 90 |
<li> |
| 91 |
<p> |
| 92 |
no other thread of control can modify that object |
| 93 |
(in the case of an exclusive or non-exclusive lock). |
| 94 |
</p> |
| 95 |
</li> |
| 96 |
</ul> |
| 97 |
</div> |
| 98 |
<div class="sect3" lang="en" xml:lang="en"> |
| 99 |
<div class="titlepage"> |
| 100 |
<div> |
| 101 |
<div> |
| 102 |
<h4 class="title"><a id="lockresources"></a>Lock Resources</h4> |
| 103 |
</div> |
| 104 |
</div> |
| 105 |
</div> |
| 106 |
<p> |
| 107 |
When locking occurs, there are conceptually three resources |
| 108 |
in use: |
| 109 |
</p> |
| 110 |
<div class="orderedlist"> |
| 111 |
<ol type="1"> |
| 112 |
<li> |
| 113 |
<p> |
| 114 |
The locker. |
| 115 |
</p> |
| 116 |
<p> |
| 117 |
This is the thing that holds the lock. In a |
| 118 |
transactional application, the locker is a |
| 119 |
transaction handle. |
| 120 |
<span> |
| 121 |
For non-transactional operations, the locker is a cursor or a |
| 122 |
<span>DB</span> |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
handle. |
| 127 |
</span> |
| 128 |
|
| 129 |
</p> |
| 130 |
</li> |
| 131 |
<li> |
| 132 |
<p> |
| 133 |
The lock. |
| 134 |
</p> |
| 135 |
<p> |
| 136 |
This is the actual data structure that locks |
| 137 |
the object. In DB, a locked |
| 138 |
object structure in the lock manager |
| 139 |
is representative of the object that |
| 140 |
is locked. |
| 141 |
</p> |
| 142 |
</li> |
| 143 |
<li> |
| 144 |
<p> |
| 145 |
The locked object. |
| 146 |
</p> |
| 147 |
<p> |
| 148 |
The thing that your application |
| 149 |
actually wants to lock. |
| 150 |
In a DB |
| 151 |
application, the locked object is usually a |
| 152 |
<span> |
| 153 |
database page, which in turn contains |
| 154 |
multiple database entries (key and data). |
| 155 |
<span> |
| 156 |
However, for Queue databases, |
| 157 |
individual database records are locked. |
| 158 |
</span> |
| 159 |
|
| 160 |
</span> |
| 161 |
|
| 162 |
</p> |
| 163 |
</li> |
| 164 |
</ol> |
| 165 |
</div> |
| 166 |
<p> |
| 167 |
You can configure how many total lockers, locks, |
| 168 |
and locked objects your |
| 169 |
application is allowed to support. See |
| 170 |
<a class="xref" href="lockingsubsystem.html#configuringlock" title="Configuring the Locking Subsystem">Configuring the Locking Subsystem</a> |
| 171 |
for details. |
| 172 |
</p> |
| 173 |
<p> |
| 174 |
The following figure shows a transaction handle, |
| 175 |
<code class="literal">Txn A</code>, that is holding a lock on |
| 176 |
database |
| 177 |
<span>page</span> |
| 178 |
|
| 179 |
<code class="literal">002</code>. In this graphic, <code class="literal">Txn |
| 180 |
A</code> is the locker, and the locked object is |
| 181 |
<span>page</span> |
| 182 |
|
| 183 |
<code class="literal">002</code>. Only a single lock is in use |
| 184 |
in this operation. |
| 185 |
</p> |
| 186 |
<div class="mediaobject"> |
| 187 |
<img src="simplelock.jpg" /> |
| 188 |
</div> |
| 189 |
</div> |
| 190 |
<div class="sect3" lang="en" xml:lang="en"> |
| 191 |
<div class="titlepage"> |
| 192 |
<div> |
| 193 |
<div> |
| 194 |
<h4 class="title"><a id="locktypes"></a>Types of Locks</h4> |
| 195 |
</div> |
| 196 |
</div> |
| 197 |
</div> |
| 198 |
<p> |
| 199 |
DB applications support both exclusive and |
| 200 |
non-exclusive locks. <span class="emphasis"><em>Exclusive |
| 201 |
locks</em></span> are granted when a |
| 202 |
locker wants to write to an object. For this reason, |
| 203 |
exclusive locks are also sometimes called |
| 204 |
<span class="emphasis"><em>write locks</em></span>. |
| 205 |
</p> |
| 206 |
<p> |
| 207 |
An exclusive lock prevents any other locker from |
| 208 |
obtaining any sort of a lock on the object. This |
| 209 |
provides isolation by ensuring that no other locker can |
| 210 |
observe or modify an exclusively locked object until the locker is done |
| 211 |
writing to that object. |
| 212 |
</p> |
| 213 |
<p> |
| 214 |
<span class="emphasis"><em>Non-exclusive locks</em></span> are granted |
| 215 |
for read-only access. For this reason, non-exclusive |
| 216 |
locks are also sometimes called <span class="emphasis"><em>read |
| 217 |
locks</em></span>. Since multiple lockers can |
| 218 |
simultaneously hold read locks on the same |
| 219 |
object, read locks are also |
| 220 |
sometimes called <span class="emphasis"><em>shared locks</em></span>. |
| 221 |
</p> |
| 222 |
<p> |
| 223 |
A non-exclusive lock prevents any other locker from |
| 224 |
modifying the locked object while the locker is still |
| 225 |
reading the object. This is how transactional cursors are able to |
| 226 |
achieve repeatable reads; by default, the |
| 227 |
cursor's transaction holds |
| 228 |
a read lock on any object that the cursor has examined until |
| 229 |
such a time as the transaction is committed |
| 230 |
or aborted. |
| 231 |
<span> |
| 232 |
You can avoid these read locks by using |
| 233 |
snapshot isolation. See <a class="xref" href="isolation.html#snapshot_isolation" title="Using Snapshot Isolation">Using Snapshot Isolation</a> |
| 234 |
for details. |
| 235 |
</span> |
| 236 |
</p> |
| 237 |
<p> |
| 238 |
In the following figure, <code class="literal">Txn A</code> and |
| 239 |
<code class="literal">Txn B</code> are both holding read locks on |
| 240 |
<span>page</span> |
| 241 |
|
| 242 |
<code class="literal">002</code>, while <code class="literal">Txn C</code> |
| 243 |
is holding a write lock on |
| 244 |
<span>page</span> |
| 245 |
|
| 246 |
<code class="literal">003</code>: |
| 247 |
</p> |
| 248 |
<div class="mediaobject"> |
| 249 |
<img src="rwlocks1.jpg" /> |
| 250 |
</div> |
| 251 |
</div> |
| 252 |
<div class="sect3" lang="en" xml:lang="en"> |
| 253 |
<div class="titlepage"> |
| 254 |
<div> |
| 255 |
<div> |
| 256 |
<h4 class="title"><a id="locklifetime"></a>Lock Lifetime</h4> |
| 257 |
</div> |
| 258 |
</div> |
| 259 |
</div> |
| 260 |
<p> |
| 261 |
A locker holds its locks until such a time as it does |
| 262 |
not need the lock any more. What this means is: |
| 263 |
</p> |
| 264 |
<div class="orderedlist"> |
| 265 |
<ol type="1"> |
| 266 |
<li> |
| 267 |
<p> |
| 268 |
A transaction holds any locks that it obtains |
| 269 |
until the transaction is committed or aborted. |
| 270 |
</p> |
| 271 |
</li> |
| 272 |
<li> |
| 273 |
<p> |
| 274 |
All non-transaction operations hold locks |
| 275 |
until such a time as the operation is completed. |
| 276 |
For cursor operations, the lock is held until the cursor is moved to a new position or |
| 277 |
closed. |
| 278 |
</p> |
| 279 |
</li> |
| 280 |
</ol> |
| 281 |
</div> |
| 282 |
</div> |
| 283 |
</div> |
| 284 |
<div class="sect2" lang="en" xml:lang="en"> |
| 285 |
<div class="titlepage"> |
| 286 |
<div> |
| 287 |
<div> |
| 288 |
<h3 class="title"><a id="blocks"></a>Blocks</h3> |
| 289 |
</div> |
| 290 |
</div> |
| 291 |
</div> |
| 292 |
<p> |
| 293 |
Simply put, a thread of control is blocked when it attempts |
| 294 |
to obtain a lock, but that attempt is denied because some |
| 295 |
other thread of control holds a conflicting lock. |
| 296 |
Once blocked, the thread of control is temporarily unable |
| 297 |
to make any forward progress until the requested lock is |
| 298 |
obtained or the operation requesting the lock is |
| 299 |
abandoned. |
| 300 |
</p> |
| 301 |
<p> |
| 302 |
Be aware that when we talk about blocking, strictly |
| 303 |
speaking the thread is not what is attempting to obtain the |
| 304 |
lock. Rather, some object within the thread (such as a |
| 305 |
cursor) is attempting to obtain the |
| 306 |
lock. However, once a locker attempts to |
| 307 |
obtain a lock, the entire thread of control must pause until the lock |
| 308 |
request is in some way resolved. |
| 309 |
</p> |
| 310 |
<p> |
| 311 |
For example, if <code class="literal">Txn A</code> holds a write lock (an exclusive |
| 312 |
lock) on |
| 313 |
<span>object</span> |
| 314 |
|
| 315 |
002, then if <code class="literal">Txn B</code> tries to obtain a read <span class="emphasis"><em>or</em></span> write lock on |
| 316 |
that |
| 317 |
<span>object,</span> |
| 318 |
|
| 319 |
the thread of control in which <code class="literal">Txn |
| 320 |
B</code> is running |
| 321 |
is blocked: |
| 322 |
</p> |
| 323 |
<div class="mediaobject"> |
| 324 |
<img src="writeblock.jpg" /> |
| 325 |
</div> |
| 326 |
<p> |
| 327 |
However, if <code class="literal">Txn A</code> only holds a read |
| 328 |
lock (a shared lock) on |
| 329 |
<span>object</span> |
| 330 |
|
| 331 |
<code class="literal">002</code>, then only those handles that attempt to obtain a |
| 332 |
write lock on that |
| 333 |
<span>object</span> |
| 334 |
|
| 335 |
will block. |
| 336 |
</p> |
| 337 |
<div class="mediaobject"> |
| 338 |
<img src="readblock.jpg" /> |
| 339 |
</div> |
| 340 |
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 341 |
<h3 class="title">Note</h3> |
| 342 |
<p> |
| 343 |
The previous description describes DB's default |
| 344 |
behavior when it cannot obtain a lock. It is |
| 345 |
possible to configure DB transactions so that |
| 346 |
they will not block. Instead, if a lock is |
| 347 |
unavailable, the application is immediately notified of a |
| 348 |
deadlock situation. See <a class="xref" href="txnnowait.html" title="No Wait on Blocks">No Wait on Blocks</a> |
| 349 |
for more information. |
| 350 |
</p> |
| 351 |
</div> |
| 352 |
<div class="sect3" lang="en" xml:lang="en"> |
| 353 |
<div class="titlepage"> |
| 354 |
<div> |
| 355 |
<div> |
| 356 |
<h4 class="title"><a id="blockperformance"></a>Blocking and Application Performance</h4> |
| 357 |
</div> |
| 358 |
</div> |
| 359 |
</div> |
| 360 |
<p> |
| 361 |
Multi-threaded |
| 362 |
<span> |
| 363 |
and multi-process |
| 364 |
</span> |
| 365 |
applications typically perform better than simple |
| 366 |
single-threaded applications because the |
| 367 |
application can perform one part of its workload |
| 368 |
(updating |
| 369 |
<span>a database record, </span> |
| 370 |
|
| 371 |
for example) while it is waiting for some other |
| 372 |
lengthy operation to complete (performing disk or |
| 373 |
network I/O, for example). This performance |
| 374 |
improvement is particularly noticeable if you use |
| 375 |
hardware that offers multiple CPUs, because the threads |
| 376 |
<span> |
| 377 |
and processes |
| 378 |
</span> |
| 379 |
can run simultaneously. |
| 380 |
</p> |
| 381 |
<p> |
| 382 |
That said, concurrent applications can see reduced |
| 383 |
workload throughput if their threads of control are |
| 384 |
seeing a large amount of lock contention. That is, |
| 385 |
if threads are blocking on lock requests, then that |
| 386 |
represents a performance penalty for your |
| 387 |
application. |
| 388 |
</p> |
| 389 |
<p> |
| 390 |
Consider once again the previous diagram of a blocked write lock request. |
| 391 |
In that diagram, <code class="literal">Txn C</code> cannot |
| 392 |
obtain its requested write lock because |
| 393 |
<code class="literal">Txn A</code> and <code class="literal">Txn |
| 394 |
B</code> are both already holding read locks on |
| 395 |
the requested |
| 396 |
<span>object.</span> |
| 397 |
|
| 398 |
In this case, the thread in which |
| 399 |
<code class="literal">Txn C</code> is running will pause until |
| 400 |
such a time as <code class="literal">Txn C</code> either |
| 401 |
obtains its write lock, or the operation |
| 402 |
that is requesting the lock is abandoned. |
| 403 |
The fact that <code class="literal">Txn |
| 404 |
C</code>'s thread has temporarily halted all |
| 405 |
forward progress represents a performance penalty |
| 406 |
for your application. |
| 407 |
</p> |
| 408 |
<p> |
| 409 |
Moreover, any read locks that are requested while |
| 410 |
<code class="literal">Txn C</code> is waiting for its write |
| 411 |
lock will also block until such a time as |
| 412 |
<code class="literal">Txn C</code> has obtained and |
| 413 |
subsequently released its write lock. |
| 414 |
</p> |
| 415 |
</div> |
| 416 |
<div class="sect3" lang="en" xml:lang="en"> |
| 417 |
<div class="titlepage"> |
| 418 |
<div> |
| 419 |
<div> |
| 420 |
<h4 class="title"><a id="blockavoidance"></a>Avoiding Blocks</h4> |
| 421 |
</div> |
| 422 |
</div> |
| 423 |
</div> |
| 424 |
<p> |
| 425 |
Reducing lock contention is an important part of |
| 426 |
performance tuning your concurrent DB |
| 427 |
application. Applications that have multiple |
| 428 |
threads of control obtaining exclusive (write) |
| 429 |
locks are prone to contention issues. Moreover, as |
| 430 |
you increase the numbers of lockers and as you |
| 431 |
increase the time that a lock is held, you increase |
| 432 |
the chances of your application seeing lock contention. |
| 433 |
</p> |
| 434 |
<p> |
| 435 |
As you are designing your application, try to do |
| 436 |
the following in order to reduce lock contention: |
| 437 |
</p> |
| 438 |
<div class="itemizedlist"> |
| 439 |
<ul type="disc"> |
| 440 |
<li> |
| 441 |
<p> |
| 442 |
Reduce the length of time your application |
| 443 |
holds locks. |
| 444 |
</p> |
| 445 |
<p> |
| 446 |
Shorter lived transactions will result in |
| 447 |
shorter lock lifetimes, which will in turn |
| 448 |
help to reduce lock contention. |
| 449 |
</p> |
| 450 |
<p> |
| 451 |
In addition, by default transactional cursors hold read |
| 452 |
locks until such a time as the transaction is completed. |
| 453 |
For this reason, try to minimize the time you keep |
| 454 |
transactional cursors opened, or reduce your isolation |
| 455 |
levels – see below. |
| 456 |
</p> |
| 457 |
</li> |
| 458 |
<li> |
| 459 |
<p> |
| 460 |
If possible, access heavily accessed (read |
| 461 |
or write) items toward the end of the |
| 462 |
transaction. This reduces the amount of |
| 463 |
time that a heavily used |
| 464 |
<span> |
| 465 |
page |
| 466 |
</span> |
| 467 |
|
| 468 |
is locked by the transaction. |
| 469 |
</p> |
| 470 |
</li> |
| 471 |
<li> |
| 472 |
<p> |
| 473 |
Reduce your application's isolation guarantees. |
| 474 |
</p> |
| 475 |
<p> |
| 476 |
By reducing your isolation guarantees, you |
| 477 |
reduce the situations in which a lock can |
| 478 |
block another lock. Try using uncommitted reads |
| 479 |
for your read operations in order to |
| 480 |
prevent a read lock being blocked by a |
| 481 |
write lock. |
| 482 |
</p> |
| 483 |
<p> |
| 484 |
In addition, for cursors you can use degree |
| 485 |
2 (read committed) isolation, which causes |
| 486 |
the cursor to release its read locks as |
| 487 |
soon as it is done reading the record (as |
| 488 |
opposed to holding its read locks until the |
| 489 |
transaction ends). |
| 490 |
</p> |
| 491 |
<p> |
| 492 |
Be aware that reducing your |
| 493 |
isolation guarantees can have |
| 494 |
adverse consequences for your |
| 495 |
application. Before deciding |
| 496 |
to reduce your isolation, take |
| 497 |
care to examine your |
| 498 |
application's isolation |
| 499 |
requirements. |
| 500 |
For information on isolation |
| 501 |
levels, see |
| 502 |
<a class="xref" href="isolation.html" title="Isolation">Isolation</a>. |
| 503 |
</p> |
| 504 |
</li> |
| 505 |
<li> |
| 506 |
<p> |
| 507 |
Use snapshot isolation for |
| 508 |
read-only threads. |
| 509 |
</p> |
| 510 |
<p> |
| 511 |
Snapshot isolation causes the |
| 512 |
transaction to make a copy of the |
| 513 |
page on which it is holding a lock. |
| 514 |
When a reader makes a copy of a |
| 515 |
page, write locks can still be |
| 516 |
obtained for the original page. |
| 517 |
This eliminates entirely read-write |
| 518 |
contention. |
| 519 |
</p> |
| 520 |
<p> |
| 521 |
Snapshot isolation is described in |
| 522 |
<a class="xref" href="isolation.html#snapshot_isolation" title="Using Snapshot Isolation">Using Snapshot Isolation</a>. |
| 523 |
</p> |
| 524 |
</li> |
| 525 |
<li> |
| 526 |
<p> |
| 527 |
Consider your data access patterns. |
| 528 |
</p> |
| 529 |
<p> |
| 530 |
Depending on the nature of your application, |
| 531 |
this may be something that you can not |
| 532 |
do anything about. However, if it is |
| 533 |
possible to create your threads such that |
| 534 |
they operate only on non-overlapping |
| 535 |
portions of your database, then you can |
| 536 |
reduce lock contention because your |
| 537 |
threads will rarely (if ever) block on one another's |
| 538 |
locks. |
| 539 |
</p> |
| 540 |
</li> |
| 541 |
</ul> |
| 542 |
</div> |
| 543 |
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 544 |
<h3 class="title">Note</h3> |
| 545 |
<p> |
| 546 |
It is possible to configure DB's transactions |
| 547 |
so that they never wait on blocked lock requests. |
| 548 |
Instead, if they are blocked on a lock request, |
| 549 |
they will notify the application of a deadlock (see |
| 550 |
the next section). |
| 551 |
</p> |
| 552 |
<p> |
| 553 |
You configure this behavior on a transaction by |
| 554 |
transaction basis. See <a class="xref" href="txnnowait.html" title="No Wait on Blocks">No Wait on Blocks</a> for more information. |
| 555 |
</p> |
| 556 |
</div> |
| 557 |
</div> |
| 558 |
</div> |
| 559 |
<div class="sect2" lang="en" xml:lang="en"> |
| 560 |
<div class="titlepage"> |
| 561 |
<div> |
| 562 |
<div> |
| 563 |
<h3 class="title"><a id="deadlocks"></a>Deadlocks</h3> |
| 564 |
</div> |
| 565 |
</div> |
| 566 |
</div> |
| 567 |
<p> |
| 568 |
A deadlock occurs when two or more threads of control are |
| 569 |
blocked, each waiting on a resource held by the other |
| 570 |
thread. When this happens, there is no |
| 571 |
possibility of the threads ever making forward progress |
| 572 |
unless some outside agent takes action to break the |
| 573 |
deadlock. |
| 574 |
</p> |
| 575 |
<p> |
| 576 |
For example, if |
| 577 |
<code class="literal">Txn A</code> is |
| 578 |
blocked by <code class="literal">Txn B</code> at the same time |
| 579 |
<code class="literal">Txn B</code> is blocked by <code class="literal">Txn |
| 580 |
A</code> then the threads of control containing |
| 581 |
<code class="literal">Txn A</code> and <code class="literal">Txn B</code> are |
| 582 |
deadlocked; neither thread can make |
| 583 |
any forward progress because neither thread will ever release the lock |
| 584 |
that is blocking the other thread. |
| 585 |
</p> |
| 586 |
<div class="mediaobject"> |
| 587 |
<img src="deadlock.jpg" /> |
| 588 |
</div> |
| 589 |
<p> |
| 590 |
When two threads of control deadlock, the only |
| 591 |
solution is to have a mechanism external to the two threads |
| 592 |
capable of recognizing the deadlock and notifying at least |
| 593 |
one thread that it is in a deadlock situation. |
| 594 |
Once notified, a thread of |
| 595 |
control must abandon the attempted operation in order to |
| 596 |
resolve the deadlock. |
| 597 |
|
| 598 |
<span> |
| 599 |
DB's locking subsystem offers a deadlock notification |
| 600 |
mechanism. See |
| 601 |
<a class="xref" href="lockingsubsystem.html#configdeadlkdetect" title="Configuring Deadlock Detection">Configuring Deadlock Detection</a> |
| 602 |
for more information. |
| 603 |
</span> |
| 604 |
|
| 605 |
|
| 606 |
</p> |
| 607 |
<p> |
| 608 |
Note that when one locker in a thread of control is blocked |
| 609 |
waiting on a lock held by another locker in that same |
| 610 |
thread of the control, the thread is said to be |
| 611 |
<span class="emphasis"><em>self-deadlocked</em></span>. |
| 612 |
</p> |
| 613 |
<div class="sect3" lang="en" xml:lang="en"> |
| 614 |
<div class="titlepage"> |
| 615 |
<div> |
| 616 |
<div> |
| 617 |
<h4 class="title"><a id="deadlockavoidance"></a>Deadlock Avoidance</h4> |
| 618 |
</div> |
| 619 |
</div> |
| 620 |
</div> |
| 621 |
<p> |
| 622 |
The things that you do to avoid lock contention also |
| 623 |
help to reduce deadlocks (see <a class="xref" href="blocking_deadlocks.html#blockavoidance" title="Avoiding Blocks">Avoiding Blocks</a>). |
| 624 |
|
| 625 |
<span> |
| 626 |
Beyond that, you can also do the following in order to |
| 627 |
avoid deadlocks: |
| 628 |
</span> |
| 629 |
|
| 630 |
|
| 631 |
</p> |
| 632 |
<div class="itemizedlist"> |
| 633 |
<ul type="disc"> |
| 634 |
<li> |
| 635 |
<p> |
| 636 |
Never have more than one active transaction at |
| 637 |
a time in a thread. A common cause of this is |
| 638 |
for a thread to be using auto-commit for one |
| 639 |
operation while an explicit transaction is in |
| 640 |
use in that thread at the same time. |
| 641 |
</p> |
| 642 |
</li> |
| 643 |
<li> |
| 644 |
<p> |
| 645 |
Make sure all threads access data in the same |
| 646 |
order as all other threads. So long as threads |
| 647 |
lock database pages |
| 648 |
in the same basic order, there is no |
| 649 |
possibility of a deadlock (threads can still |
| 650 |
block, however). |
| 651 |
</p> |
| 652 |
<p> |
| 653 |
Be aware that if you are using secondary |
| 654 |
databases (indexes), it is not possible to |
| 655 |
obtain locks in a consistent order because you |
| 656 |
cannot predict the order in which locks are |
| 657 |
obtained in secondary databases. If you are |
| 658 |
writing a concurrent application and you are |
| 659 |
using secondary databases, you must be prepared |
| 660 |
to handle deadlocks. |
| 661 |
</p> |
| 662 |
</li> |
| 663 |
<li> |
| 664 |
<p> |
| 665 |
If you are using BTrees in which you are |
| 666 |
constantly adding and then deleting data, turn |
| 667 |
Btree reverse split off. See |
| 668 |
<a class="xref" href="reversesplit.html" title="Reverse BTree Splits">Reverse BTree Splits</a> |
| 669 |
for more information. |
| 670 |
</p> |
| 671 |
</li> |
| 672 |
<li> |
| 673 |
<p> |
| 674 |
Declare a read/modify/write lock for those |
| 675 |
situations where you are reading a record in |
| 676 |
preparation of modifying and then writing the |
| 677 |
record. Doing this causes DB to give your |
| 678 |
read operation a write lock. This means that no |
| 679 |
other thread of control can share a read lock |
| 680 |
(which might cause contention), but it also |
| 681 |
means that the writer thread will not have to |
| 682 |
wait to obtain a write lock when it is ready to |
| 683 |
write the modified data back to the database. |
| 684 |
</p> |
| 685 |
<p> |
| 686 |
For information on declaring |
| 687 |
read/modify/write locks, see |
| 688 |
<a class="xref" href="readmodifywrite.html" title="Read/Modify/Write">Read/Modify/Write</a>. |
| 689 |
</p> |
| 690 |
</li> |
| 691 |
</ul> |
| 692 |
</div> |
| 693 |
</div> |
| 694 |
</div> |
| 695 |
</div> |
| 696 |
<div class="navfooter"> |
| 697 |
<hr /> |
| 698 |
<table width="100%" summary="Navigation footer"> |
| 699 |
<tr> |
| 700 |
<td width="40%" align="left"><a accesskey="p" href="txnconcurrency.html">Prev</a> </td> |
| 701 |
<td width="20%" align="center"> |
| 702 |
<a accesskey="u" href="txnconcurrency.html">Up</a> |
| 703 |
</td> |
| 704 |
<td width="40%" align="right"> <a accesskey="n" href="lockingsubsystem.html">Next</a></td> |
| 705 |
</tr> |
| 706 |
<tr> |
| 707 |
<td width="40%" align="left" valign="top">Chapter 4. Concurrency </td> |
| 708 |
<td width="20%" align="center"> |
| 709 |
<a accesskey="h" href="index.html">Home</a> |
| 710 |
</td> |
| 711 |
<td width="40%" align="right" valign="top"> The Locking Subsystem</td> |
| 712 |
</tr> |
| 713 |
</table> |
| 714 |
</div> |
| 715 |
</body> |
| 716 |
</html> |