Debian Codegraph @

docs/programmer_reference/transapp_deadlock.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>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="transapp.html" title="Chapter 11.  Berkeley DB Transactional Data Store Applications" />
11 <link rel="prev" href="transapp_admin.html" title="Environment infrastructure" />
12 <link rel="next" href="transapp_checkpoint.html" title="Checkpoints" />
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="transapp_admin.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 11. 
26 Berkeley DB Transactional Data Store Applications
27 </th>
28 <td width="20%" align="right"> <a accesskey="n" href="transapp_checkpoint.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="transapp_deadlock"></a>Deadlock detection</h2>
38 </div>
39 </div>
40 </div>
41 <p>
42 The first component of the infrastructure,
43 <span class="emphasis"><em>deadlock detection</em></span>, is not so much a
44 requirement specific to transaction-protected applications, but instead
45 is necessary for almost all applications in which more than a single
46 thread of control will be accessing the database at one time. Even
47 when Berkeley DB automatically handles database locking, it is normally
48 possible for deadlock to occur. Because the underlying database access
49 methods may update multiple pages during a single Berkeley DB API call,
50 deadlock is possible even when threads of control are making only
51 single update calls into the database. The exception to this rule is
52 when all the threads of control accessing the database are read-only or
53 when the Berkeley DB Concurrent Data Store product is used; the
54 Berkeley DB Concurrent Data Store product guarantees deadlock-free
55 operation at the expense of reduced concurrency.
56 </p>
57 <p>
58 When the deadlock occurs, two (or more) threads of control each request
59 additional locks that can never be granted because one of the threads
60 of control waiting holds the requested resource. For example, consider
61 two processes: A and B. Let's say that A obtains a write lock on item
62 X, and B obtains a write lock on item Y. Then, A requests a lock on Y,
63 and B requests a lock on X. A will wait until resource Y becomes
64 available and B will wait until resource X becomes available.
65 Unfortunately, because both A and B are waiting, neither will release
66 the locks they hold and neither will ever obtain the resource on which
67 it is waiting. For another example, consider two transactions, A and
68 B, each of which may want to modify item X. Assume that transaction A
69 obtains a read lock on X and confirms that a modification is needed.
70 Then it is descheduled and the thread containing transaction B runs.
71 At that time, transaction B obtains a read lock on X and confirms that
72 it also wants to make a modification. Both transactions A and B will
73 block when they attempt to upgrade their read locks to write locks
74 because the other already holds a read lock. This is a deadlock.
75 Transaction A cannot make forward progress until Transaction B releases
76 its read lock on X, but Transaction B cannot make forward progress
77 until Transaction A releases its read lock on X.
78 </p>
79 <p>
80 In order to detect that deadlock has happened, a separate process or
81 thread must review the locks currently held in the database. If
82 deadlock has occurred, a victim must be selected, and that victim will
83 then return the error
84 <a class="link" href="program_errorret.html#program_errorret.DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a>
85 from whatever Berkeley DB call it was making. Berkeley DB provides the
86 <a href="../api_reference/C/db_deadlock.html" class="olink">db_deadlock</a> utility that can be used to perform this deadlock detection.
87 Alternatively, applications can create their own deadlock utility or
88 thread using the underlying <a href="../api_reference/C/lockdetect.html" class="olink">DB_ENV-&gt;lock_detect()</a> function, or specify that
89 Berkeley DB run the deadlock detector internally whenever there is a
90 conflict over a lock (see <a href="../api_reference/C/envset_lk_detect.html" class="olink">DB_ENV-&gt;set_lk_detect()</a> for more information).
91 The following code fragment does the latter:
92 </p>
93 <pre class="programlisting">void
94 env_open(DB_ENV **dbenvp)
95 {
96 DB_ENV *dbenv;
97 int ret;
98
99 /* Create the environment handle. */
100 if ((ret = db_env_create(&amp;dbenv, 0)) != 0) {
101 fprintf(stderr,
102 "txnapp: db_env_create: %s\n", db_strerror(ret));
103 exit (1);
104 }
105
106 /* Set up error handling. */
107 dbenv-&gt;set_errpfx(dbenv, "txnapp");
108 dbenv-&gt;set_errfile(dbenv, stderr);
109
110 <span class="bold"><strong> /* Do deadlock detection internally. */
111 if ((ret = dbenv-&gt;set_lk_detect(dbenv, DB_LOCK_DEFAULT)) != 0) {
112 dbenv-&gt;err(dbenv, ret, "set_lk_detect: DB_LOCK_DEFAULT");
113 exit (1);
114 }</strong></span>
115
116 /*
117 * Open a transactional environment:
118 * create if it doesn't exist
119 * free-threaded handle
120 * run recovery
121 * read/write owner only
122 */
123 if ((ret = dbenv-&gt;open(dbenv, ENV_DIRECTORY,
124 DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG |
125 DB_INIT_MPOOL | DB_INIT_TXN | DB_RECOVER | DB_THREAD,
126 S_IRUSR | S_IWUSR)) != 0) {
127 dbenv-&gt;err(dbenv, ret, "dbenv-&gt;open: %s", ENV_DIRECTORY);
128 exit (1);
129 }
130
131 *dbenvp = dbenv;
132 }</pre>
133 <p>
134 Deciding how often to run the deadlock detector and which of the
135 deadlocked transactions will be forced to abort when the deadlock is
136 detected is a common tuning parameter for Berkeley DB applications.
137 </p>
138 </div>
139 <div class="navfooter">
140 <hr />
141 <table width="100%" summary="Navigation footer">
142 <tr>
143 <td width="40%" align="left"><a accesskey="p" href="transapp_admin.html">Prev</a> </td>
144 <td width="20%" align="center">
145 <a accesskey="u" href="transapp.html">Up</a>
146 </td>
147 <td width="40%" align="right"> <a accesskey="n" href="transapp_checkpoint.html">Next</a></td>
148 </tr>
149 <tr>
150 <td width="40%" align="left" valign="top">Environment infrastructure </td>
151 <td width="20%" align="center">
152 <a accesskey="h" href="index.html">Home</a>
153 </td>
154 <td width="40%" align="right" valign="top"> Checkpoints</td>
155 </tr>
156 </table>
157 </div>
158 </body>
159 </html>