libdb/docs/programmer_reference/lock_twopl.html
2012-11-14 16:35:20 -05:00

92 lines
4.5 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Locking with transactions: two-phase locking</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
<link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" />
<link rel="up" href="lock.html" title="Chapter 16.  The Locking Subsystem" />
<link rel="prev" href="lock_notxn.html" title="Locking without transactions" />
<link rel="next" href="lock_cam_conv.html" title="Berkeley DB Concurrent Data Store locking conventions" />
</head>
<body>
<div xmlns="" class="navheader">
<div class="libver">
<p>Library Version 11.2.5.3</p>
</div>
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Locking with transactions: two-phase locking</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="lock_notxn.html">Prev</a> </td>
<th width="60%" align="center">Chapter 16. 
The Locking Subsystem
</th>
<td width="20%" align="right"> <a accesskey="n" href="lock_cam_conv.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="lock_twopl"></a>Locking with transactions: two-phase locking</h2>
</div>
</div>
</div>
<p>Berkeley DB uses a locking protocol called <span class="emphasis"><em>two-phase locking (2PL)</em></span>. This
is the traditional protocol used in conjunction with lock-based transaction
systems.</p>
<p>In a two-phase locking system, transactions are divided into two
distinct phases. During the first phase, the transaction only acquires
locks; during the second phase, the transaction only releases locks.
More formally, once a transaction releases a lock, it may not acquire
any additional locks. Practically, this translates into a system in
which locks are acquired as they are needed throughout a transaction
and retained until the transaction ends, either by committing or
aborting. In Berkeley DB, locks are released during <a href="../api_reference/C/txnabort.html" class="olink">DB_TXN-&gt;abort()</a> or
<a href="../api_reference/C/txncommit.html" class="olink">DB_TXN-&gt;commit()</a>. The only exception to this protocol occurs when we
use lock-coupling to traverse a data structure. If the locks are held
only for traversal purposes, it is safe to release locks before
transactions commit or abort.</p>
<p>For applications, the implications of 2PL are that long-running
transactions will hold locks for a long time. When designing
applications, lock contention should be considered. In order to reduce
the probability of deadlock and achieve the best level of concurrency
possible, the following guidelines are helpful.</p>
<div class="orderedlist">
<ol type="1">
<li>When accessing multiple databases, design all transactions so that they
access the files in the same order.</li>
<li>If possible, access your most hotly contested resources last (so that
their locks are held for the shortest time possible).</li>
<li>If possible, use nested transactions to protect the parts of your
transaction most likely to deadlock.</li>
</ol>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="lock_notxn.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="lock.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="lock_cam_conv.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Locking without transactions </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Berkeley DB Concurrent Data Store locking conventions</td>
</tr>
</table>
</div>
</body>
</html>