| 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>Deadlock detection</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="lock.html" title="Chapter 16. The Locking Subsystem" /> |
| 11 | <link rel="prev" href="lock_stdmode.html" title="Standard lock modes" /> |
| 12 | <link rel="next" href="lock_timeout.html" title="Deadlock detection using timers" /> |
| 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">Deadlock detection</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="lock_stdmode.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 16. |
| 26 | The Locking Subsystem |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="lock_timeout.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="lock_dead"></a>Deadlock detection</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>Practically any application that uses locking may deadlock. The |
| 42 | exceptions to this rule are when all the threads of control accessing |
| 43 | the database are read-only or when the Berkeley DB Concurrent Data Store product is used; the |
| 44 | Berkeley DB Concurrent Data Store product guarantees deadlock-free operation at the expense of |
| 45 | reduced concurrency. While there are data access patterns that are |
| 46 | deadlock free (for example, an application doing nothing but overwriting |
| 47 | fixed-length records in an already existing database), they are |
| 48 | extremely rare.</p> |
| 49 | <p>When a deadlock exists in the system, all the threads of control |
| 50 | involved in the deadlock are, by definition, waiting on a lock. The |
| 51 | deadlock detector examines the state of the lock manager and identifies |
| 52 | a deadlock, and selects one of the lock requests to reject. (See |
| 53 | <a class="xref" href="lock_config.html" title="Configuring locking">Configuring locking</a> for a |
| 54 | discussion of how a participant is selected). The <a href="../api_reference/C/lockget.html" class="olink">DB_ENV->lock_get()</a> or |
| 55 | <a href="../api_reference/C/lockvec.html" class="olink">DB_ENV->lock_vec()</a> call for which the selected participant is waiting then |
| 56 | returns a <a class="link" href="program_errorret.html#program_errorret.DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a> error. When using the Berkeley DB access |
| 57 | methods, this error return is propagated back through the Berkeley DB database |
| 58 | handle method to the calling application.</p> |
| 59 | <p>The deadlock detector identifies deadlocks by looking for a cycle in |
| 60 | what is commonly referred to as its "waits-for" graph. More precisely, |
| 61 | the deadlock detector reads through the lock table, and reviews each |
| 62 | lock object currently locked. Each object has lockers that currently |
| 63 | hold locks on the object and possibly a list of lockers waiting for a |
| 64 | lock on the object. Each object's list of waiting lockers defines a |
| 65 | partial ordering. That is, for a particular object, every waiting |
| 66 | locker comes after every holding locker because that holding locker must |
| 67 | release its lock before the waiting locker can make forward progress. |
| 68 | Conceptually, after each object has been examined, the partial orderings |
| 69 | are topologically sorted. If this topological sort reveals any cycles, |
| 70 | the lockers forming the cycle are involved in a deadlock. One of the |
| 71 | lockers is selected for rejection.</p> |
| 72 | <p>It is possible that rejecting a single lock request involved in a |
| 73 | deadlock is not enough to allow other lockers to make forward progress. |
| 74 | Unfortunately, at the time a lock request is selected for rejection, |
| 75 | there is not enough information available to determine whether rejecting |
| 76 | that single lock request will allow forward progress or not. Because |
| 77 | most applications have few deadlocks, Berkeley DB takes the conservative |
| 78 | approach, rejecting as few requests as may be necessary to resolve the |
| 79 | existing deadlocks. In particular, for each unique cycle found in the |
| 80 | waits-for graph described in the previous paragraph, only one lock |
| 81 | request is selected for rejection. However, if there are multiple |
| 82 | cycles, one lock request from each cycle is selected for rejection. |
| 83 | Only after the enclosing transactions have received the lock request |
| 84 | rejection return and aborted their transactions can it be determined |
| 85 | whether it is necessary to reject additional lock requests in order to |
| 86 | allow forward progress.</p> |
| 87 | <p>The <a href="../api_reference/C/db_deadlock.html" class="olink">db_deadlock</a> utility performs deadlock detection by calling |
| 88 | the underlying Berkeley DB <a href="../api_reference/C/lockdetect.html" class="olink">DB_ENV->lock_detect()</a> method at regular intervals |
| 89 | (<a href="../api_reference/C/lockdetect.html" class="olink">DB_ENV->lock_detect()</a> runs a single iteration of the Berkeley DB deadlock |
| 90 | detector). Alternatively, applications can create their own deadlock |
| 91 | utility or thread by calling the <a href="../api_reference/C/lockdetect.html" class="olink">DB_ENV->lock_detect()</a> method directly, or by |
| 92 | using the <a href="../api_reference/C/envset_lk_detect.html" class="olink">DB_ENV->set_lk_detect()</a> method to configure Berkeley DB to |
| 93 | automatically run the deadlock detector whenever there is a conflict |
| 94 | over a lock. The tradeoffs between using the <a href="../api_reference/C/lockdetect.html" class="olink">DB_ENV->lock_detect()</a> and |
| 95 | <a href="../api_reference/C/envset_lk_detect.html" class="olink">DB_ENV->set_lk_detect()</a> methods is that automatic deadlock detection will |
| 96 | resolve deadlocks more quickly (because the deadlock detector runs |
| 97 | as soon as the lock request blocks), however, automatic deadlock |
| 98 | detection often runs the deadlock detector when there is no need for |
| 99 | it, and for applications with large numbers of locks and/or where many |
| 100 | operations block temporarily on locks but are soon able to proceed, |
| 101 | automatic detection can decrease performance.</p> |
| 102 | </div> |
| 103 | <div class="navfooter"> |
| 104 | <hr /> |
| 105 | <table width="100%" summary="Navigation footer"> |
| 106 | <tr> |
| 107 | <td width="40%" align="left"><a accesskey="p" href="lock_stdmode.html">Prev</a> </td> |
| 108 | <td width="20%" align="center"> |
| 109 | <a accesskey="u" href="lock.html">Up</a> |
| 110 | </td> |
| 111 | <td width="40%" align="right"> <a accesskey="n" href="lock_timeout.html">Next</a></td> |
| 112 | </tr> |
| 113 | <tr> |
| 114 | <td width="40%" align="left" valign="top">Standard lock modes </td> |
| 115 | <td width="20%" align="center"> |
| 116 | <a accesskey="h" href="index.html">Home</a> |
| 117 | </td> |
| 118 | <td width="40%" align="right" valign="top"> Deadlock detection using timers</td> |
| 119 | </tr> |
| 120 | </table> |
| 121 | </div> |
| 122 | </body> |
| 123 | </html> |