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