| 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>Berkeley DB Transactional Data Store locking conventions</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_cam_conv.html" title="Berkeley DB Concurrent Data Store locking conventions" /> |
| 12 | <link rel="next" href="lock_nondb.html" title="Locking and non-Berkeley DB applications" /> |
| 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">Berkeley DB Transactional Data Store locking conventions</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="lock_cam_conv.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_nondb.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_am_conv"></a>Berkeley DB Transactional Data Store locking conventions</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>All Berkeley DB access methods follow the same conventions for locking |
| 42 | database objects. Applications that do their own locking and also do |
| 43 | locking via the access methods must be careful to adhere to these |
| 44 | conventions.</p> |
| 45 | <p>Whenever a Berkeley DB database is opened, the <a href="../api_reference/C/db.html" class="olink">DB</a> handle is assigned |
| 46 | a unique locker ID. Unless transactions are specified, that ID is used |
| 47 | as the locker for all calls that the Berkeley DB methods make to the lock |
| 48 | subsystem. In order to lock a file, pages in the file, or records in |
| 49 | the file, we must create a unique ID that can be used as the object to |
| 50 | be locked in calls to the lock manager. Under normal operation, that |
| 51 | object is a 28-byte value created by the concatenation of a unique file |
| 52 | identifier, a page or record number, and an object type (page or record).</p> |
| 53 | <p>In a transaction-protected environment, database create and delete |
| 54 | operations are recoverable and single-threaded. This single-threading |
| 55 | is achieved using a single lock for the entire environment that must be |
| 56 | acquired before beginning a create or delete operation. In this case, |
| 57 | the object on which Berkeley DB will lock is a 4-byte unsigned integer with |
| 58 | a value of 0.</p> |
| 59 | <p>If applications are using the lock subsystem directly while they are |
| 60 | also using locking via the access methods, they must take care not to |
| 61 | inadvertently lock objects that happen to be equal to the unique file |
| 62 | IDs used to lock files. This is most easily accomplished by using a |
| 63 | lock object with a length different from the values used by Berkeley DB.</p> |
| 64 | <p>All the access methods other than Queue use standard read/write locks |
| 65 | in a simple multiple-reader/single writer page-locking scheme. An |
| 66 | operation that returns data (for example, <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a> or |
| 67 | <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a>) obtains a read lock on all the pages accessed while |
| 68 | locating the requested record. When an update operation is requested |
| 69 | (for example, <a href="../api_reference/C/dbput.html" class="olink">DB->put()</a> or <a href="../api_reference/C/dbcdel.html" class="olink">DBC->del()</a>), the page containing |
| 70 | the updated (or new) data is write-locked. As read-modify-write cycles |
| 71 | are quite common and are deadlock-prone under normal circumstances, the |
| 72 | Berkeley DB interfaces allow the application to specify the <a href="../api_reference/C/dbcget.html#dbcget_DB_RMW" class="olink">DB_RMW</a> |
| 73 | flag, which causes operations to immediately obtain a write lock, even |
| 74 | though they are only reading the data. Although this may reduce |
| 75 | concurrency somewhat, it reduces the probability of deadlock. In the |
| 76 | presence of transactions, page locks are held until transaction commit.</p> |
| 77 | <p>The Queue access method does not hold long-term page locks. Instead, |
| 78 | page locks are held only long enough to locate records or to change |
| 79 | metadata on a page, and record locks are held for the appropriate |
| 80 | duration. In the presence of transactions, record locks are held until |
| 81 | transaction commit. For <a href="../api_reference/C/db.html" class="olink">DB</a> operations, record locks are held until |
| 82 | operation completion; for <a href="../api_reference/C/dbc.html" class="olink">DBC</a> operations, record locks are held |
| 83 | until subsequent records are returned or the cursor is closed.</p> |
| 84 | <p>Under non-transaction operations, the access methods do not normally |
| 85 | hold locks across calls to the Berkeley DB interfaces. The one exception to |
| 86 | this rule is when cursors are used. Because cursors maintain a position |
| 87 | in a file, they must hold locks across calls; in fact, they will hold |
| 88 | a lock until the cursor is closed.</p> |
| 89 | <p>In this mode, the assignment of locker IDs to <a href="../api_reference/C/db.html" class="olink">DB</a> and cursor |
| 90 | handles is complicated. If the <a href="../api_reference/C/dbopen.html#open_DB_THREAD" class="olink">DB_THREAD</a> option was specified |
| 91 | when the <a href="../api_reference/C/db.html" class="olink">DB</a> handle was opened, each use of <a href="../api_reference/C/db.html" class="olink">DB</a> has its |
| 92 | own unique locker ID, and each cursor is assigned its own unique locker |
| 93 | ID when it is created, so <a href="../api_reference/C/db.html" class="olink">DB</a> handle and cursor operations can |
| 94 | all conflict with one another. (This is because when Berkeley DB handles |
| 95 | may be shared by multiple threads of control the Berkeley DB library cannot |
| 96 | identify which operations are performed by which threads of control, |
| 97 | and it must ensure that two different threads of control are not |
| 98 | simultaneously modifying the same data structure. By assigning each |
| 99 | <a href="../api_reference/C/db.html" class="olink">DB</a> handle and cursor its own locker, two threads of control |
| 100 | sharing a handle cannot inadvertently interfere with each other.)</p> |
| 101 | <p>This has important implications. If a single thread of control opens |
| 102 | two cursors, uses a combination of cursor and non-cursor operations, or |
| 103 | begins two separate transactions, the operations are performed on behalf |
| 104 | of different lockers. Conflicts that arise between these different |
| 105 | lockers may not cause actual deadlocks, but can, in fact, permanently |
| 106 | block the thread of control. For example, assume that an application |
| 107 | creates a cursor and uses it to read record A. Now, assume a second |
| 108 | cursor is opened, and the application attempts to write record A using |
| 109 | the second cursor. Unfortunately, the first cursor has a read lock, so |
| 110 | the second cursor cannot obtain its write lock. However, that read lock |
| 111 | is held by the same thread of control, so the read lock can never be |
| 112 | released if we block waiting for the write lock. This might appear to |
| 113 | be a deadlock from the application's perspective, but Berkeley DB cannot |
| 114 | identify it as such because it has no knowledge of which lockers belong |
| 115 | to which threads of control. For this reason, application designers |
| 116 | are encouraged to close cursors as soon as they are done with them.</p> |
| 117 | <p>If the <a href="../api_reference/C/dbopen.html#open_DB_THREAD" class="olink">DB_THREAD</a> option was not specified when the <a href="../api_reference/C/db.html" class="olink">DB</a> |
| 118 | handle was opened, all uses of the <a href="../api_reference/C/db.html" class="olink">DB</a> handle and all cursors |
| 119 | created using that handle will use the same locker ID for all |
| 120 | operations. In this case, if a single thread of control opens two |
| 121 | cursors or uses a combination of cursor and non-cursor operations, these |
| 122 | operations are performed on behalf of the same locker, and so cannot |
| 123 | deadlock or block the thread of control.</p> |
| 124 | <p>Complicated operations that require multiple cursors (or combinations |
| 125 | of cursor and non-cursor operations) can be performed in two ways. |
| 126 | First, they may be performed within a transaction, in which case all |
| 127 | operations lock on behalf of the designated transaction. Second, they |
| 128 | may be performed using a local <a href="../api_reference/C/db.html" class="olink">DB</a> handle, although, as |
| 129 | <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a> operations are relatively slow, this may not be a good |
| 130 | idea. Finally, the <a href="../api_reference/C/dbcdup.html" class="olink">DBC->dup()</a> function duplicates a cursor, using |
| 131 | the same locker ID as the originating cursor. There is no way to |
| 132 | achieve this duplication functionality through the <a href="../api_reference/C/db.html" class="olink">DB</a> handle |
| 133 | calls, but any <a href="../api_reference/C/db.html" class="olink">DB</a> call can be implemented by one or more calls |
| 134 | through a cursor.</p> |
| 135 | <p>When the access methods use transactions, many of these problems disappear. |
| 136 | The transaction ID is used as the locker ID for all operations performed |
| 137 | on behalf of the transaction. This means that the application may open |
| 138 | multiple cursors on behalf of the same transaction and these cursors will |
| 139 | all share a common locker ID. This is safe because transactions cannot |
| 140 | span threads of control, so the library knows that two cursors in the same |
| 141 | transaction cannot modify the database concurrently.</p> |
| 142 | </div> |
| 143 | <div class="navfooter"> |
| 144 | <hr /> |
| 145 | <table width="100%" summary="Navigation footer"> |
| 146 | <tr> |
| 147 | <td width="40%" align="left"><a accesskey="p" href="lock_cam_conv.html">Prev</a> </td> |
| 148 | <td width="20%" align="center"> |
| 149 | <a accesskey="u" href="lock.html">Up</a> |
| 150 | </td> |
| 151 | <td width="40%" align="right"> <a accesskey="n" href="lock_nondb.html">Next</a></td> |
| 152 | </tr> |
| 153 | <tr> |
| 154 | <td width="40%" align="left" valign="top">Berkeley DB Concurrent Data Store locking conventions </td> |
| 155 | <td width="20%" align="center"> |
| 156 | <a accesskey="h" href="index.html">Home</a> |
| 157 | </td> |
| 158 | <td width="40%" align="right" valign="top"> Locking and non-Berkeley DB applications</td> |
| 159 | </tr> |
| 160 | </table> |
| 161 | </div> |
| 162 | </body> |
| 163 | </html> |