| 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>Lock Handling</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="Getting Started with the Oracle Berkeley DB SQL APIs" /> |
| 10 | <link rel="up" href="lockingnotes.html" title="Chapter 2. Locking Notes" /> |
| 11 | <link rel="prev" href="lockingnotes.html" title="Chapter 2. Locking Notes" /> |
| 12 | <link rel="next" href="dbfeatures.html" title="Chapter 3. Berkeley DB Features" /> |
| 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">Lock Handling</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="lockingnotes.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 2. Locking Notes</th> |
| 26 | <td width="20%" align="right"> <a accesskey="n" href="dbfeatures.html">Next</a></td> |
| 27 | </tr> |
| 28 | </table> |
| 29 | <hr /> |
| 30 | </div> |
| 31 | <div class="sect1" lang="en" xml:lang="en"> |
| 32 | <div class="titlepage"> |
| 33 | <div> |
| 34 | <div> |
| 35 | <h2 class="title" style="clear: both"><a id="lockhandling"></a>Lock Handling</h2> |
| 36 | </div> |
| 37 | </div> |
| 38 | </div> |
| 39 | <div class="toc"> |
| 40 | <dl> |
| 41 | <dt> |
| 42 | <span class="sect2"> |
| 43 | <a href="lockhandling.html#sqllockmodel">SQLite Lock Usage</a> |
| 44 | </span> |
| 45 | </dt> |
| 46 | <dt> |
| 47 | <span class="sect2"> |
| 48 | <a href="lockhandling.html#bdblockusage">Lock Usage with the BDB SQL Interface</a> |
| 49 | </span> |
| 50 | </dt> |
| 51 | </dl> |
| 52 | </div> |
| 53 | <p> |
| 54 | There is a difference in how applications written for the |
| 55 | BDB SQL interface handle deadlocks as opposed to how deadlocks are handled |
| 56 | for SQLite applications. For the SQLite developer, the |
| 57 | following information is a necessary review in order to |
| 58 | understand how the BDB SQL interface behaves differently. |
| 59 | </p> |
| 60 | <p> |
| 61 | From a usage point of view, the BDB SQL interface behaves in the same way |
| 62 | as SQLite in shared cache mode. The implications of this |
| 63 | are explained below. |
| 64 | </p> |
| 65 | <div class="sect2" lang="en" xml:lang="en"> |
| 66 | <div class="titlepage"> |
| 67 | <div> |
| 68 | <div> |
| 69 | <h3 class="title"><a id="sqllockmodel"></a>SQLite Lock Usage</h3> |
| 70 | </div> |
| 71 | </div> |
| 72 | </div> |
| 73 | <p> |
| 74 | As mentioned previously in this chapter, SQLite locks the |
| 75 | entire database while performing a transaction. It also has a |
| 76 | locking model that is different from the BDB SQL interface, one that |
| 77 | supports multiple readers, but only a single writer. In SQLite, |
| 78 | transactions can start as follows: |
| 79 | </p> |
| 80 | <div class="itemizedlist"> |
| 81 | <ul type="disc"> |
| 82 | <li> |
| 83 | <p> |
| 84 | <code class="literal">BEGIN</code> |
| 85 | </p> |
| 86 | <p> |
| 87 | Begins the transaction, locking the entire database for |
| 88 | reading. Use this if you only want to read from the |
| 89 | database. |
| 90 | </p> |
| 91 | </li> |
| 92 | <li> |
| 93 | <p> |
| 94 | <code class="literal">BEGIN IMMEDIATE</code> |
| 95 | </p> |
| 96 | <p> |
| 97 | Begins the transaction, acquiring a "modify" lock. This |
| 98 | is also known as a RESERVED lock. Use this if you are |
| 99 | modifying the database (that is, performing |
| 100 | <code class="literal">INSERT</code>, <code class="literal">UPDATE</code>, |
| 101 | or <code class="literal">DELETE</code>). RESERVED locks and read |
| 102 | locks can co-exist. |
| 103 | </p> |
| 104 | </li> |
| 105 | <li> |
| 106 | <p> |
| 107 | <code class="literal">BEGIN EXCLUSIVE</code> |
| 108 | </p> |
| 109 | <p> |
| 110 | Begins the transaction, acquiring a write lock. |
| 111 | Transactions begun this way will be written to the disk |
| 112 | upon commit. No other lock can co-exist with an |
| 113 | exclusive lock. |
| 114 | </p> |
| 115 | </li> |
| 116 | </ul> |
| 117 | </div> |
| 118 | <p> |
| 119 | The last two statements are a kind of a contract. If you can |
| 120 | get them to complete (that is, not return |
| 121 | <code class="literal">SQLITE_LOCKED</code>), then you can start modifying |
| 122 | the database (that is, change data in the in-memory cache), and |
| 123 | you will eventually be able to commit (write) your |
| 124 | modifications to the database. |
| 125 | </p> |
| 126 | <p> |
| 127 | In order to avoid deadlocks in SQLite, programmers who want |
| 128 | to modify a SQLite database start the transaction with |
| 129 | <code class="literal">BEGIN IMMEDIATE</code>. If the transaction cannot |
| 130 | acquire the necessary locks, it will fail, returning |
| 131 | <code class="literal">SQLITE_BUSY</code>. At that point, the transaction |
| 132 | falls back to an unlocked state whereby it holds no locks |
| 133 | against the database. This means that any existing transactions in a |
| 134 | RESERVED state can safely wait for the necessary EXCLUSIVE lock |
| 135 | in order to finally write their modifications from the |
| 136 | in-memory cache to the on-disk database. |
| 137 | </p> |
| 138 | <p> |
| 139 | The important point here is that so long as the programmer uses |
| 140 | these locks correctly, he can assume that he can proceed with |
| 141 | his work without encountering a deadlock. (Assuming that all |
| 142 | database readers and writers are also using these locks |
| 143 | correctly.) |
| 144 | </p> |
| 145 | </div> |
| 146 | <div class="sect2" lang="en" xml:lang="en"> |
| 147 | <div class="titlepage"> |
| 148 | <div> |
| 149 | <div> |
| 150 | <h3 class="title"><a id="bdblockusage"></a>Lock Usage with the BDB SQL Interface</h3> |
| 151 | </div> |
| 152 | </div> |
| 153 | </div> |
| 154 | <p> |
| 155 | When you use the BDB SQL interface, you can |
| 156 | begin your transaction with <code class="literal">BEGIN</code> or <code class="literal">BEGIN EXCLUSIVE</code>. |
| 157 | </p> |
| 158 | <p> |
| 159 | Note that the <code class="literal">IMMEDIATE</code> keyword is ignored in the BDB SQL interface (<code class="literal">BEGIN IMMEDIATE</code> behaves like <code class="literal">BEGIN</code>). |
| 160 | </p> |
| 161 | <p> |
| 162 | When you begin your transaction with <code class="literal">BEGIN</code>, Berkeley DB decides what kind of a lock |
| 163 | you need based on what you are doing to the database. If |
| 164 | you perform an action that is read-only, it acquires a read |
| 165 | lock. If you perform a write action, it acquires a write |
| 166 | lock. |
| 167 | </p> |
| 168 | <p> |
| 169 | Also, the BDB SQL interface supports multiple readers |
| 170 | <span class="emphasis"><em>and</em></span> multiple writers. This means that |
| 171 | multiple transactions can acquire locks |
| 172 | as long as they are not trying to modify the |
| 173 | same page. For example: |
| 174 | </p> |
| 175 | <p> |
| 176 | <span class="bold"><strong>Session 1:</strong></span> |
| 177 | </p> |
| 178 | <pre class="programlisting">dbsql> create table a(x int); |
| 179 | dbsql> begin; |
| 180 | dbsql> insert into a values (1); |
| 181 | dbsql> commit; </pre> |
| 182 | <p> |
| 183 | <span class="bold"><strong>Session 2:</strong></span> |
| 184 | </p> |
| 185 | <pre class="programlisting">dbsql> create table b(x int); |
| 186 | dbsql> begin; |
| 187 | dbsql> insert into b values (1); |
| 188 | dbsql> commit; </pre> |
| 189 | <p> |
| 190 | Because these two sessions are operating on different pages |
| 191 | in the Berkeley DB cache, this example will work. If you tried |
| 192 | this with SQLite, you could not start the second |
| 193 | transaction until the first had completed. |
| 194 | </p> |
| 195 | <p> |
| 196 | However, if you do this using the BDB SQL interface: |
| 197 | </p> |
| 198 | <p> |
| 199 | <span class="bold"><strong>Session 1:</strong></span> |
| 200 | </p> |
| 201 | <pre class="programlisting">dbsql> begin; |
| 202 | dbsql> insert into a values (2);</pre> |
| 203 | <p> |
| 204 | <span class="bold"><strong>Session 2:</strong></span> |
| 205 | </p> |
| 206 | <pre class="programlisting">dbsql> begin; |
| 207 | dbsql> insert into a values (2); </pre> |
| 208 | <p> |
| 209 | The second session blocks until the first session commits |
| 210 | the transaction. Again, this is because both sessions are |
| 211 | operating on the same database page(s). However, if you |
| 212 | simultaneously attempt to write pages in reverse order, you |
| 213 | can deadlock. For example: |
| 214 | </p> |
| 215 | <p> |
| 216 | <span class="bold"><strong>Session 1:</strong></span> |
| 217 | </p> |
| 218 | <pre class="programlisting">dbsql> begin; |
| 219 | dbsql> insert into a values (3); |
| 220 | dbsql> insert into b values (3); |
| 221 | </pre> |
| 222 | <p> |
| 223 | <span class="bold"><strong>Session 2:</strong></span> |
| 224 | </p> |
| 225 | <pre class="programlisting">dbsql> begin; |
| 226 | dbsql> insert into b values (3); |
| 227 | dbsql> insert into a values (3); |
| 228 | Error: database table is locked |
| 229 | </pre> |
| 230 | <p> |
| 231 | What happens here is that Session 1 is blocked waiting |
| 232 | for a lock on table b, while Session 2 is blocked |
| 233 | waiting for a lock on table a. The application can make |
| 234 | no forward progress, and so it is deadlocked. |
| 235 | </p> |
| 236 | <p> |
| 237 | When such a deadlock is detected one session loses the lock it got |
| 238 | when executing its last statement, and that statement is automatically rolled back. |
| 239 | The rest of the statements in the session will still be valid, and you can |
| 240 | continue to execute statements in that session. |
| 241 | The session that does not lose its lock to deadlock detection |
| 242 | will continue to execute as if nothing happened. |
| 243 | </p> |
| 244 | <p> |
| 245 | Assume Session 2 was sacrificed to deadlock detection, no value would be inserted into a and an error will be returned. |
| 246 | But the insertion of value 3 into b would still be valid. |
| 247 | Session 1 would continue to wait while inserting into table b |
| 248 | until Session 2 either commits or aborts, thus freeing the lock it has on table b. |
| 249 | </p> |
| 250 | <p> |
| 251 | When you begin your transaction with <code class="literal">BEGIN EXCLUSIVE</code>, the session is |
| 252 | never aborted due to deadlock or lock contention with another transaction. |
| 253 | Non-exclusive transactions are allowed to execute concurrently with the exclusive |
| 254 | transaction, but the non-exclusive transactions will have their locks |
| 255 | released if deadlock with the exclusive transaction occurs. If two or |
| 256 | more exclusive transactions are running at the same time, they |
| 257 | will be forced to execute in serial. |
| 258 | </p> |
| 259 | <p>If Session 1 was using an exclusive transaction, |
| 260 | then Session 2 would lose its locks when deadlock is detected between the two. |
| 261 | If both Session 1 and Session 2 start an exclusive transaction, then the |
| 262 | last one to start the exclusive transaction would be blocked after |
| 263 | executing <code class="literal">BEGIN EXCLUSIVE</code> until the first one is committed or aborted. |
| 264 | </p> |
| 265 | </div> |
| 266 | </div> |
| 267 | <div class="navfooter"> |
| 268 | <hr /> |
| 269 | <table width="100%" summary="Navigation footer"> |
| 270 | <tr> |
| 271 | <td width="40%" align="left"><a accesskey="p" href="lockingnotes.html">Prev</a> </td> |
| 272 | <td width="20%" align="center"> |
| 273 | <a accesskey="u" href="lockingnotes.html">Up</a> |
| 274 | </td> |
| 275 | <td width="40%" align="right"> <a accesskey="n" href="dbfeatures.html">Next</a></td> |
| 276 | </tr> |
| 277 | <tr> |
| 278 | <td width="40%" align="left" valign="top">Chapter 2. Locking Notes </td> |
| 279 | <td width="20%" align="center"> |
| 280 | <a accesskey="h" href="index.html">Home</a> |
| 281 | </td> |
| 282 | <td width="40%" align="right" valign="top"> Chapter 3. Berkeley DB Features</td> |
| 283 | </tr> |
| 284 | </table> |
| 285 | </div> |
| 286 | </body> |
| 287 | </html> |