Debian Codegraph @

docs/gsg_txn/CXX/lockingsubsystem.html

raw
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>The Locking Subsystem</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="blocking_deadlocks.html" title="Locks, Blocks, and Deadlocks" />
12 <link rel="next" href="isolation.html" title="Isolation" />
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">The Locking Subsystem</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="blocking_deadlocks.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 4. Concurrency</th>
26 <td width="20%" align="right"> <a accesskey="n" href="isolation.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="lockingsubsystem"></a>The Locking Subsystem</h2>
36 </div>
37 </div>
38 </div>
39 <div class="toc">
40 <dl>
41 <dt>
42 <span class="sect2">
43 <a href="lockingsubsystem.html#configuringlock">Configuring the Locking Subsystem</a>
44 </span>
45 </dt>
46 <dt>
47 <span class="sect2">
48 <a href="lockingsubsystem.html#configdeadlkdetect">Configuring Deadlock Detection</a>
49 </span>
50 </dt>
51 <dt>
52 <span class="sect2">
53 <a href="lockingsubsystem.html#deadlockresolve">Resolving Deadlocks</a>
54 </span>
55 </dt>
56 <dt>
57 <span class="sect2">
58 <a href="lockingsubsystem.html#setpriority">Setting Transaction Priorities</a>
59 </span>
60 </dt>
61 </dl>
62 </div>
63 <p>
64 In order to allow concurrent operations, DB provides the locking
65 subsystem. This subsystem provides inter- and intra- process
66 concurrency mechanisms. It is extensively used by DB concurrent
67 applications, but it can also be generally used for non-DB
68 resources.
69 </p>
70 <p>
71 This section describes the locking subsystem as it is used to
72 protect DB resources. In particular, issues on configuration are
73 examined here. For information on using the locking subsystem to
74 manage non-DB resources, see the
75 <em class="citetitle">Berkeley DB Programmer's Reference Guide</em>.
76 </p>
77 <div class="sect2" lang="en" xml:lang="en">
78 <div class="titlepage">
79 <div>
80 <div>
81 <h3 class="title"><a id="configuringlock"></a>Configuring the Locking Subsystem</h3>
82 </div>
83 </div>
84 </div>
85 <p>
86 You initialize the locking subsystem by specifying
87 <span>
88 <code class="literal">DB_INIT_LOCK</code> to the
89
90 <code class="methodname">DbEnv::open()</code>
91 method.
92 </span>
93
94 </p>
95 <p>
96 Before opening your environment, you can configure various
97 values for your locking subsystem. Note that these
98 limits can only be configured before the environment is
99 opened. Also, these methods configure the entire environment,
100 not just a specific environment handle.
101 </p>
102 <p>
103 Finally, each bullet below identifies the
104 <code class="filename">DB_CONFIG</code> file parameter that can be used
105 to specify the specific locking limit. If used, these
106 <code class="filename">DB_CONFIG</code> file parameters override any
107 value that you might specify using the environment handle.
108 </p>
109 <p>
110 The limits that you can configure are as follows:
111 </p>
112 <div class="itemizedlist">
113 <ul type="disc">
114 <li>
115 <p>
116 The number of lockers supported by the environment.
117 This value is used by the environment when it is
118 opened to estimate the amount of space that it
119 should allocate for various internal data
120 structures. By default, 1,000 lockers are
121 supported.
122 </p>
123 <p>
124 To configure this value, use the
125
126 <span>
127
128 <code class="methodname">DbEnv::set_memory_init()</code>
129 method to configure the <code class="literal">DB_MEM_LOCKER</code> structure.
130 </span>
131
132 </p>
133 <p>
134 As an alternative to this method, you can configure this
135 value using the <code class="filename">DB_CONFIG</code> file's
136 <code class="literal">set_lk_max_lockers</code> parameter.
137 </p>
138 </li>
139 <li>
140 <p>
141 The number of locks supported by the environment.
142 By default, 1,000 locks are supported.
143 </p>
144 <p>
145 To configure this value, use the
146 <span>
147
148 <code class="methodname">DbEnv::set_memory_init()</code>
149 method to configure the <code class="literal">DB_MEM_LOCK</code> structure.
150 </span>
151
152 </p>
153 <p>
154 As an alternative to this method, you can configure this
155 value using the <code class="filename">DB_CONFIG</code> file's
156 <code class="literal">set_lk_max_locks</code> parameter.
157 </p>
158 </li>
159 <li>
160 <p>
161 The number of locked objects supported by the
162 environment. By default, 1,000 objects can be
163 locked.
164 </p>
165 <p>
166 To configure this value, use the
167 <span>
168
169 <code class="methodname">DbEnv::set_memory_init()</code>
170 method to configure the <code class="literal">DB_MEM_LOCKOBJECT</code> structure.
171 </span>
172
173 </p>
174 <p>
175 As an alternative to this method, you can configure this
176 value using the <code class="filename">DB_CONFIG</code> file's
177 <code class="literal">set_lk_max_objects</code> parameter.
178 </p>
179 </li>
180 </ul>
181 </div>
182 <p>
183 For a definition of lockers, locks, and locked objects, see
184 <a class="xref" href="blocking_deadlocks.html#lockresources" title="Lock Resources">Lock Resources</a>.
185 </p>
186 <p>
187 For example, to configure the number of locks that your
188 environment can use:
189 </p>
190 <pre class="programlisting">#include "db_cxx.h"
191
192 ...
193
194 int main(void)
195 {
196 u_int32_t env_flags = DB_CREATE | // If the environment does not
197 // exist, create it.
198 DB_INIT_LOCK | // Initialize locking
199 DB_INIT_LOG | // Initialize logging
200 DB_INIT_MPOOL | // Initialize the cache
201 DB_THREAD | // Free-thread the env handle.
202 DB_INIT_TXN; // Initialize transactions
203
204 std::string envHome("/export1/testEnv");
205 DbEnv myEnv(0);
206
207 try {
208
209 // Configure max locks
210 myEnv.set_lk_max_locks(5000);
211 myEnv.set_memory_init(DB_MEM_LOCK, 5000);
212
213 myEnv.open(envHome.c_str(), env_flags, 0);
214
215 } catch(DbException &amp;e) {
216 std::cerr &lt;&lt; "Error opening database environment: "
217 &lt;&lt; envHome &lt;&lt; std::endl;
218 std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
219 return (EXIT_FAILURE);
220 }
221
222 try {
223 myEnv.close(0);
224 } catch(DbException &amp;e) {
225 std::cerr &lt;&lt; "Error closing database environment: "
226 &lt;&lt; envHome &lt;&lt; std::endl;
227 std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
228 return (EXIT_FAILURE);
229 }
230
231 return (EXIT_SUCCESS);
232 } </pre>
233 </div>
234 <div class="sect2" lang="en" xml:lang="en">
235 <div class="titlepage">
236 <div>
237 <div>
238 <h3 class="title"><a id="configdeadlkdetect"></a>Configuring Deadlock Detection</h3>
239 </div>
240 </div>
241 </div>
242 <p>
243 In order for DB to know that a deadlock has occurred,
244 some mechanism must be used to perform deadlock
245 detection. There are three ways that deadlock detection can
246 occur:
247 </p>
248 <div class="orderedlist">
249 <ol type="1">
250 <li>
251 <p>
252 Allow DB to internally detect deadlocks as they
253 occur.
254 </p>
255 <p>
256 To do this, you use
257
258 <span><code class="methodname">DbEnv::set_lk_detect()</code>.</span>
259
260 This method causes DB to walk its internal lock table
261 looking for a deadlock whenever a lock request
262 is blocked. This method also identifies how DB decides which lock
263 requests are rejected when deadlocks are detected. For example,
264 DB can decide to reject the lock request for the transaction
265 that has the most number of locks, the least number of locks,
266 holds the oldest lock, holds the most number of write locks, and
267 so forth (see the API reference documentation for a complete
268 list of the lock detection policies).
269 </p>
270 <p>
271 You can call this method at any time during your application's
272 lifetime, but typically it is used before you open your environment.
273 </p>
274 <p>
275 Note that how you want DB to decide which thread of control should break a deadlock is
276 extremely dependent on the nature of your application. It is not unusual for some performance
277 testing to be required in order to make this determination. That said, a transaction that is
278 holding the most number of locks is usually indicative of the transaction that has performed
279 the most amount of work. Frequently you will not want a transaction that has performed a lot of
280 work to abandon its efforts and start all over again. It is not therefore uncommon for
281 application developers to initially select the transaction with the <span class="emphasis"><em>minimum</em></span>
282 number of write locks to break the deadlock.
283 </p>
284 <p>
285 Using this mechanism for deadlock detection means
286 that your application will never have to wait on a
287 lock before discovering that a deadlock has
288 occurred. However, walking the lock table every
289 time a lock request is blocked can be expensive
290 from a performance perspective.
291 </p>
292 </li>
293 <li>
294 <p>
295 Use a dedicated thread or external process to perform
296 deadlock detection. Note that this thread must be
297 performing no other database operations beyond deadlock
298 detection.
299 </p>
300 <p>
301 To externally perform lock detection, you can use
302 either the
303
304 <code class="methodname">DbEnv::lock_detect()</code>
305
306 method, or use the
307 <span class="command"><strong>db_deadlock</strong></span> command line
308 utility. This method (or command) causes DB to walk the
309 lock table looking for deadlocks.
310 </p>
311 <p>
312 Note that like
313
314 <span><code class="methodname">DbEnv::set_lk_detect()</code>,</span>
315
316 you also use this method (or command line utility)
317 to identify which lock requests are rejected in the
318 event that a deadlock is detected.
319 </p>
320 <p>
321 Applications that perform deadlock detection in
322 this way typically run deadlock detection between every few
323 seconds and a minute. This means that your
324 application may have to wait to be notified of a
325 deadlock, but you also save the overhead of walking
326 the lock table every time a lock request is blocked.
327 </p>
328 </li>
329 <li>
330 <p>
331 Lock timeouts.
332 </p>
333 <p>
334 You can configure your locking subsystem such that
335 it times out any lock that is not released within a
336 specified amount of time. To do this, use the
337
338 <span><code class="methodname">DbEnv::set_timeout()</code></span>
339
340 method.
341 Note that lock timeouts are only checked when a
342 lock request is blocked or when deadlock
343 detection is otherwise performed. Therefore, a lock can have timed out and still be held for
344 some length of time until DB has a reason to examine its locking tables.
345 </p>
346 <p>
347 Be aware that extremely long-lived transactions, or
348 operations that hold locks for a long time, may be
349 inappropriately timed out before the transaction or
350 operation has a chance to complete. You should
351 therefore use this mechanism only if you know your
352 application will hold locks for very short periods
353 of time.
354 </p>
355 </li>
356 </ol>
357 </div>
358 <p>
359 For example, to configure your application such that DB
360 checks the lock table for deadlocks every time a lock
361 request is blocked:
362 </p>
363 <pre class="programlisting">#include "db_cxx.h"
364
365 ...
366
367 int main(void)
368 {
369 u_int32_t env_flags = DB_CREATE | // If the environment does not
370 // exist, create it.
371 DB_INIT_LOCK | // Initialize locking
372 DB_INIT_LOG | // Initialize logging
373 DB_INIT_MPOOL | // Initialize the cache
374 DB_THREAD | // Free-thread the env handle
375 DB_INIT_TXN; // Initialize transactions
376
377 std::string envHome("/export1/testEnv");
378 DbEnv myEnv(0);
379
380 try {
381
382 // Configure db to perform deadlock detection internally, and to
383 // choose the transaction that has performed the least amount
384 // of writing to break the deadlock in the event that one
385 // is detected.
386 myEnv.set_lk_detect(DB_LOCK_MINWRITE);
387 myEnv.open(envHome.c_str(), env_flags, 0);
388
389 // From here, you open your databases, proceed with your
390 // database operations, and respond to deadlocks as
391 // is normal (omitted for brevity).
392
393
394 ...</pre>
395 <p>
396 Finally, the following command line call causes
397 deadlock detection to be run against the
398 environment contained in <code class="literal">/export/dbenv</code>. The
399 transaction with the youngest lock is chosen to break the
400 deadlock:
401 </p>
402 <pre class="programlisting">&gt; /usr/local/db_install/bin/db_deadlock -h /export/dbenv -a y</pre>
403 <p>
404 For more information, see the
405 <a class="ulink" href="http://download.oracle.com/docs/cd/E17076_02/html/api_reference/C/db_deadlock.html" target="_top">
406 <code class="literal">db_deadlock</code> reference documentation.
407 </a>
408 </p>
409 </div>
410 <div class="sect2" lang="en" xml:lang="en">
411 <div class="titlepage">
412 <div>
413 <div>
414 <h3 class="title"><a id="deadlockresolve"></a>Resolving Deadlocks</h3>
415 </div>
416 </div>
417 </div>
418 <p>
419 When DB determines that a deadlock has occurred, it will
420 select a thread of control to resolve the deadlock and then
421
422 <span>
423 throws <code class="literal">DbDeadlockException</code> in that
424 thread.
425 </span>
426
427
428 If a deadlock is detected, the thread must:
429 </p>
430 <div class="orderedlist">
431 <ol type="1">
432 <li>
433 <p>
434 Cease all read and write operations.
435 </p>
436 </li>
437 <li>
438 <p>
439 Close all open cursors.
440 </p>
441 </li>
442 <li>
443 <p>
444 Abort the transaction.
445 </p>
446 </li>
447 <li>
448 <p>
449 Optionally retry the operation. If your application
450 retries deadlocked operations, the new attempt must
451 be made using a new transaction.
452 </p>
453 </li>
454 </ol>
455 </div>
456 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
457 <h3 class="title">Note</h3>
458 <p>
459 If a thread has deadlocked, it may not make any
460 additional database calls using the handle that has
461 deadlocked.
462 </p>
463 </div>
464 <p>
465 For example:
466 </p>
467 <pre class="programlisting">// retry_count is a counter used to identify how many times
468 // we've retried this operation. To avoid the potential for
469 // endless looping, we won't retry more than MAX_DEADLOCK_RETRIES
470 // times.
471
472 // txn is a transaction handle.
473 // key and data are DBT handles. Their usage is not shown here.
474 while (retry_count &lt; MAX_DEADLOCK_RETRIES) {
475 try {
476 envp-&gt;txn_begin(NULL, txn, 0);
477 dbp-&gt;put(txn, &amp;key, &amp;data, 0);
478 txn-&gt;commit(0);
479 return (EXIT_SUCCESS);
480 } catch (DbDeadlockException &amp;de) {
481 try {
482 // Abort the transaction and increment the
483 // retry counter
484 txn-&gt;abort();
485 retry_count++;
486 // If we've retried too many times, log it and exit
487 if (retry_count &gt;= MAX_DEADLOCK_RETRIES) {
488 envp-&gt;errx("Exceeded retry limit. Giving up.");
489 return (EXIT_FAILURE);
490 }
491 } catch (DbException &amp;ae) {
492 envp-&gt;err(ae.get_errno(), "txn abort failed.");
493 return (EXIT_FAILURE);
494 }
495 } catch (DbException &amp;e) {
496 try {
497 // For a generic error, log it and abort.
498 envp-&gt;err(e.get_errno(), "Error putting data.");
499 txn-&gt;abort();
500 } catch (DbException &amp;ae) {
501 envp-&gt;err(ae.get_errno(), "txn abort failed.");
502 return (EXIT_FAILURE);
503 }
504 }
505 } </pre>
506 </div>
507 <div class="sect2" lang="en" xml:lang="en">
508 <div class="titlepage">
509 <div>
510 <div>
511 <h3 class="title"><a id="setpriority"></a>Setting Transaction Priorities</h3>
512 </div>
513 </div>
514 </div>
515 <p>
516 Normally when a thread of control must be selected to
517 resolve a deadlock, DB decides which thread will
518 perform the resolution; you have no way of knowing in
519 advance which thread will be selected to resolve the
520 deadlock.
521 </p>
522 <p>
523 However, there may be situations where you know it is
524 better for one thread to resolve a deadlock over another
525 thread. As an example, if you have a background thread
526 running data management activities, and another thread
527 responding to user requests, you might want deadlock
528 resolution to occur in the background thread because you
529 can better afford the throughput costs there. Under these
530 circumstances, you can identify which thread of control
531 will be selected for resolved deadlocks by setting a
532 transaction priorities.
533 </p>
534 <p>
535 When two transactions are deadlocked, DB will abort the
536 transaction with the lowest priority. By default, every
537 transaction is given a priority of 100. However, you can
538 set a different priority on a transaction-by-transaction
539 basis by using the
540
541 <code class="methodname">DbTxn::set_priority()</code>
542
543 method.
544 </p>
545 <p>
546 When two or more transactions are tied for the lowest
547 priority, the tie is broken based on the policy provided to
548 <span>
549 the
550
551 <code class="methodname">DbEnv::lock_detect()</code>
552 method's <code class="literal">atype</code> parameter.
553 </span>
554
555 </p>
556 <p>
557 A transaction's priority can be changed at any time after
558 the transaction handle has been created and before the
559 transaction has been resolved (committed or aborted).
560 For example:
561 </p>
562 <pre class="programlisting">#include "db_cxx.h"
563
564 ...
565
566 int main(void)
567 {
568 ...
569
570 try {
571
572 ...
573 // Database and environment open omitted for brevity.
574 ...
575 DbTxn *txn = NULL;
576 myEnv.txn_begin(NULL, &amp;txn, 0);
577 <strong class="userinput"><code>txn-&gt;set_priority(200);</code></strong>
578
579 try {
580 db-&gt;put(txn, &amp;key, &amp;data, 0);
581 txn-&gt;commit(0);
582 } catch (DbException &amp;e) {
583 std::cerr &lt;&lt; "Error in transaction: "
584 &lt;&lt; e.what() &lt;&lt; std::endl;
585 txn-&gt;abort();
586 }
587
588 } catch(DbException &amp;e) {
589 std::cerr &lt;&lt; "Error opening database and environment: "
590 &lt;&lt; file_name &lt;&lt; ", "
591 &lt;&lt; envHome &lt;&lt; std::endl;
592 std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
593 }
594
595 ...
596
597 } </pre>
598 </div>
599 </div>
600 <div class="navfooter">
601 <hr />
602 <table width="100%" summary="Navigation footer">
603 <tr>
604 <td width="40%" align="left"><a accesskey="p" href="blocking_deadlocks.html">Prev</a> </td>
605 <td width="20%" align="center">
606 <a accesskey="u" href="txnconcurrency.html">Up</a>
607 </td>
608 <td width="40%" align="right"> <a accesskey="n" href="isolation.html">Next</a></td>
609 </tr>
610 <tr>
611 <td width="40%" align="left" valign="top">Locks, Blocks, and Deadlocks </td>
612 <td width="20%" align="center">
613 <a accesskey="h" href="index.html">Home</a>
614 </td>
615 <td width="40%" align="right" valign="top"> Isolation</td>
616 </tr>
617 </table>
618 </div>
619 </body>
620 </html>