| 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 granularity</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_deaddbg.html" title="Deadlock debugging" /> |
| 12 | <link rel="next" href="lock_notxn.html" title="Locking without transactions" /> |
| 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 granularity</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="lock_deaddbg.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_notxn.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_page"></a>Locking granularity</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>With the exception of the Queue access method, the Berkeley DB access methods |
| 42 | do page-level locking. The size of pages in a database may be set when |
| 43 | the database is created by calling the <a href="../api_reference/C/dbset_pagesize.html" class="olink">DB->set_pagesize()</a> method. If |
| 44 | not specified by the application, Berkeley DB selects a page size that will |
| 45 | provide the best I/O performance by setting the page size equal to the |
| 46 | block size of the underlying file system. Selecting a smaller page size |
| 47 | can result in increased concurrency for some applications.</p> |
| 48 | <p>In the Btree access method, Berkeley DB uses a technique called lock coupling |
| 49 | to improve concurrency. The traversal of a Btree requires reading a |
| 50 | page, searching that page to determine which page to search next, and |
| 51 | then repeating this process on the next page. Once a page has been |
| 52 | searched, it will never be accessed again for this operation, unless a |
| 53 | page split is required. To improve concurrency in the tree, once the |
| 54 | next page to read/search has been determined, that page is locked and |
| 55 | then the original page lock is released atomically (that is, without |
| 56 | relinquishing control of the lock manager). When page splits become |
| 57 | necessary, write locks are reacquired.</p> |
| 58 | <p>Because the Recno access method is built upon Btree, it also uses lock |
| 59 | coupling for read operations. However, because the Recno access method |
| 60 | must maintain a count of records on its internal pages, it cannot |
| 61 | lock-couple during write operations. Instead, it retains write locks |
| 62 | on all internal pages during every update operation. For this reason, |
| 63 | it is not possible to have high concurrency in the Recno access method |
| 64 | in the presence of write operations.</p> |
| 65 | <p>The Queue access method uses only short-term page locks. That is, a page |
| 66 | lock is released prior to requesting another page lock. Record locks are |
| 67 | used for transaction isolation. The provides a high degree of concurrency |
| 68 | for write operations. A metadata page is used to keep track of the head |
| 69 | and tail of the queue. This page is never locked during other locking or |
| 70 | I/O operations.</p> |
| 71 | <p>The Hash access method does not have such traversal issues, but it must |
| 72 | always refer to its metadata while computing a hash function because it |
| 73 | implements dynamic hashing. This metadata is stored on a special page |
| 74 | in the hash database. This page must therefore be read-locked on every |
| 75 | operation. Fortunately, it needs to be write-locked only when new pages |
| 76 | are allocated to the file, which happens in three cases:</p> |
| 77 | <div class="itemizedlist"> |
| 78 | <ul type="disc"> |
| 79 | <li>a hash bucket becomes full and needs to split</li> |
| 80 | <li>a key or data item is too large to fit on a normal page</li> |
| 81 | <li>the number of duplicate items for a fixed key becomes so large that they |
| 82 | are moved to an auxiliary page</li> |
| 83 | </ul> |
| 84 | </div> |
| 85 | <p>In this case, the access method must obtain a write lock on the metadata |
| 86 | page, thus requiring that all readers be blocked from entering the tree |
| 87 | until the update completes.</p> |
| 88 | <p>Finally, when traversing duplicate data items for a key, the lock on |
| 89 | the key value also acts as a lock on all duplicates of that key. |
| 90 | Therefore, two conflicting threads of control cannot access the same |
| 91 | duplicate set simultaneously.</p> |
| 92 | </div> |
| 93 | <div class="navfooter"> |
| 94 | <hr /> |
| 95 | <table width="100%" summary="Navigation footer"> |
| 96 | <tr> |
| 97 | <td width="40%" align="left"><a accesskey="p" href="lock_deaddbg.html">Prev</a> </td> |
| 98 | <td width="20%" align="center"> |
| 99 | <a accesskey="u" href="lock.html">Up</a> |
| 100 | </td> |
| 101 | <td width="40%" align="right"> <a accesskey="n" href="lock_notxn.html">Next</a></td> |
| 102 | </tr> |
| 103 | <tr> |
| 104 | <td width="40%" align="left" valign="top">Deadlock debugging </td> |
| 105 | <td width="20%" align="center"> |
| 106 | <a accesskey="h" href="index.html">Home</a> |
| 107 | </td> |
| 108 | <td width="40%" align="right" valign="top"> Locking without transactions</td> |
| 109 | </tr> |
| 110 | </table> |
| 111 | </div> |
| 112 | </body> |
| 113 | </html> |