1<!--$Id: page.so,v 10.19 2002/06/01 23:42:12 bostic Exp $-->
2<!--Copyright (c) 1997,2008 Oracle.  All rights reserved.-->
3<!--See the file LICENSE for redistribution information.-->
4<html>
5<head>
6<title>Berkeley DB Reference Guide: Locking granularity</title>
7<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
8<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
9</head>
10<body bgcolor=white>
11<a name="2"><!--meow--></a><a name="3"><!--meow--></a>
12<table width="100%"><tr valign=top>
13<td><b><dl><dt>Berkeley DB Reference Guide:<dd>Locking Subsystem</dl></b></td>
14<td align=right><a href="../lock/deaddbg.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../lock/notxn.html"><img src="../../images/next.gif" alt="Next"></a>
15</td></tr></table>
16<p align=center><b>Locking granularity</b></p>
17<p>With the exception of the Queue access method, the Berkeley DB access methods
18do page-level locking.  The size of pages in a database may be set when
19the database is created by calling the <a href="../../api_c/db_set_pagesize.html">DB-&gt;set_pagesize</a> method.  If
20not specified by the application, Berkeley DB selects a page size that will
21provide the best I/O performance by setting the page size equal to the
22block size of the underlying file system.  Selecting a smaller page size
23can result in increased concurrency for some applications.</p>
24<p>In the Btree access method, Berkeley DB uses a technique called lock coupling
25to improve concurrency.  The traversal of a Btree requires reading a
26page, searching that page to determine which page to search next, and
27then repeating this process on the next page.  Once a page has been
28searched, it will never be accessed again for this operation, unless a
29page split is required.  To improve concurrency in the tree, once the
30next page to read/search has been determined, that page is locked and
31then the original page lock is released atomically (that is, without
32relinquishing control of the lock manager).  When page splits become
33necessary, write locks are reacquired.</p>
34<p>Because the Recno access method is built upon Btree, it also uses lock
35coupling for read operations.  However, because the Recno access method
36must maintain a count of records on its internal pages, it cannot
37lock-couple during write operations.  Instead, it retains write locks
38on all internal pages during every update operation.  For this reason,
39it is not possible to have high concurrency in the Recno access method
40in the presence of write operations.</p>
41<p>The Queue access method uses only short-term page locks.  That is, a page
42lock is released prior to requesting another page lock.  Record locks are
43used for transaction isolation.  The provides a high degree of concurrency
44for write operations.  A metadata page is used to keep track of the head
45and tail of the queue.  This page is never locked during other locking or
46I/O operations.</p>
47<p>The Hash access method does not have such traversal issues, but it must
48always refer to its metadata while computing a hash function because it
49implements dynamic hashing.  This metadata is stored on a special page
50in the hash database.  This page must therefore be read-locked on every
51operation.  Fortunately, it needs to be write-locked only when new pages
52are allocated to the file, which happens in three cases:</p>
53<p><ul type=disc>
54<li>a hash bucket becomes full and needs to split
55<li>a key or data item is too large to fit on a normal page
56<li>the number of duplicate items for a fixed key becomes so large that they
57are moved to an auxiliary page
58</ul>
59<p>In this case, the access method must obtain a write lock on the metadata
60page, thus requiring that all readers be blocked from entering the tree
61until the update completes.</p>
62<p>Finally, when traversing duplicate data items for a key, the lock on
63the key value also acts as a lock on all duplicates of that key.
64Therefore, two conflicting threads of control cannot access the same
65duplicate set simultaneously.</p>
66<table width="100%"><tr><td><br></td><td align=right><a href="../lock/deaddbg.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../lock/notxn.html"><img src="../../images/next.gif" alt="Next"></a>
67</td></tr></table>
68<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
69</body>
70</html>
71