Debian Codegraph @

docs/programmer_reference/lock_twopl.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>Locking with transactions: two-phase locking</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_notxn.html" title="Locking without transactions" />
12 <link rel="next" href="lock_cam_conv.html" title="Berkeley DB Concurrent Data Store locking conventions" />
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">Locking with transactions: two-phase locking</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="lock_notxn.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_cam_conv.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_twopl"></a>Locking with transactions: two-phase locking</h2>
38 </div>
39 </div>
40 </div>
41 <p>Berkeley DB uses a locking protocol called <span class="emphasis"><em>two-phase locking (2PL)</em></span>. This
42 is the traditional protocol used in conjunction with lock-based transaction
43 systems.</p>
44 <p>In a two-phase locking system, transactions are divided into two
45 distinct phases. During the first phase, the transaction only acquires
46 locks; during the second phase, the transaction only releases locks.
47 More formally, once a transaction releases a lock, it may not acquire
48 any additional locks. Practically, this translates into a system in
49 which locks are acquired as they are needed throughout a transaction
50 and retained until the transaction ends, either by committing or
51 aborting. In Berkeley DB, locks are released during <a href="../api_reference/C/txnabort.html" class="olink">DB_TXN-&gt;abort()</a> or
52 <a href="../api_reference/C/txncommit.html" class="olink">DB_TXN-&gt;commit()</a>. The only exception to this protocol occurs when we
53 use lock-coupling to traverse a data structure. If the locks are held
54 only for traversal purposes, it is safe to release locks before
55 transactions commit or abort.</p>
56 <p>For applications, the implications of 2PL are that long-running
57 transactions will hold locks for a long time. When designing
58 applications, lock contention should be considered. In order to reduce
59 the probability of deadlock and achieve the best level of concurrency
60 possible, the following guidelines are helpful.</p>
61 <div class="orderedlist">
62 <ol type="1">
63 <li>When accessing multiple databases, design all transactions so that they
64 access the files in the same order.</li>
65 <li>If possible, access your most hotly contested resources last (so that
66 their locks are held for the shortest time possible).</li>
67 <li>If possible, use nested transactions to protect the parts of your
68 transaction most likely to deadlock.</li>
69 </ol>
70 </div>
71 </div>
72 <div class="navfooter">
73 <hr />
74 <table width="100%" summary="Navigation footer">
75 <tr>
76 <td width="40%" align="left"><a accesskey="p" href="lock_notxn.html">Prev</a> </td>
77 <td width="20%" align="center">
78 <a accesskey="u" href="lock.html">Up</a>
79 </td>
80 <td width="40%" align="right"> <a accesskey="n" href="lock_cam_conv.html">Next</a></td>
81 </tr>
82 <tr>
83 <td width="40%" align="left" valign="top">Locking without transactions </td>
84 <td width="20%" align="center">
85 <a accesskey="h" href="index.html">Home</a>
86 </td>
87 <td width="40%" align="right" valign="top"> Berkeley DB Concurrent Data Store locking conventions</td>
88 </tr>
89 </table>
90 </div>
91 </body>
92 </html>