| 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>Isolation</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="transapp.html" title="Chapter 11. Berkeley DB Transactional Data Store Applications" /> |
| 11 | <link rel="prev" href="transapp_atomicity.html" title="Atomicity" /> |
| 12 | <link rel="next" href="transapp_read.html" title="Degrees of isolation" /> |
| 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">Isolation</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="transapp_atomicity.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 11. |
| 26 | Berkeley DB Transactional Data Store Applications |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="transapp_read.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="transapp_inc"></a>Isolation</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>The third reason listed for using transactions was <span class="emphasis"><em>isolation</em></span>. |
| 42 | Consider an application suite in which multiple threads of control |
| 43 | (multiple processes or threads in one or more processes) are changing |
| 44 | the values associated with a key in one or more databases. Specifically, |
| 45 | they are taking the current value, incrementing it, and then storing it |
| 46 | back into the database.</p> |
| 47 | <p>Such an application requires isolation. Because we want to change a value |
| 48 | in the database, we must make sure that after we read it, no other thread |
| 49 | of control modifies it. For example, assume that both thread #1 and |
| 50 | thread #2 are doing similar operations in the database, where thread #1 |
| 51 | is incrementing records by 3, and thread #2 is incrementing records by |
| 52 | 5. We want to increment the record by a total of 8. If the operations |
| 53 | interleave in the right (well, wrong) order, that is not what will |
| 54 | happen:</p> |
| 55 | <pre class="programlisting">thread #1 <span class="bold"><strong>read</strong></span> record: the value is 2 |
| 56 | thread #2 <span class="bold"><strong>read</strong></span> record: the value is 2 |
| 57 | thread #2 <span class="bold"><strong>write</strong></span> record + 5 back into the database (new value 7) |
| 58 | thread #1 <span class="bold"><strong>write</strong></span> record + 3 back into the database (new value 5)</pre> |
| 59 | <p>As you can see, instead of incrementing the record by a total of 8, |
| 60 | we've incremented it only by 3 because thread #1 overwrote thread #2's |
| 61 | change. By wrapping the operations in transactions, we ensure that this |
| 62 | cannot happen. In a transaction, when the first thread reads the |
| 63 | record, locks are acquired that will not be released until the |
| 64 | transaction finishes, guaranteeing that all writers |
| 65 | will block, waiting for the first thread's transaction to complete (or |
| 66 | to be aborted).</p> |
| 67 | <p>Here is an example function that does transaction-protected increments |
| 68 | on database records to ensure isolation:</p> |
| 69 | <pre class="programlisting">int |
| 70 | main(int argc, char *argv) |
| 71 | { |
| 72 | extern int optind; |
| 73 | DB *db_cats, *db_color, *db_fruit; |
| 74 | DB_ENV *dbenv; |
| 75 | int ch; |
| 76 | |
| 77 | while ((ch = getopt(argc, argv, "")) != EOF) |
| 78 | switch (ch) { |
| 79 | case '?': |
| 80 | default: |
| 81 | usage(); |
| 82 | } |
| 83 | argc -= optind; |
| 84 | argv += optind; |
| 85 | |
| 86 | env_dir_create(); |
| 87 | env_open(&dbenv); |
| 88 | |
| 89 | /* Open database: Key is fruit class; Data is specific type. */ |
| 90 | db_open(dbenv, &db_fruit, "fruit", 0); |
| 91 | |
| 92 | /* Open database: Key is a color; Data is an integer. */ |
| 93 | db_open(dbenv, &db_color, "color", 0); |
| 94 | |
| 95 | /* |
| 96 | * Open database: |
| 97 | * Key is a name; Data is: company name, cat breeds. |
| 98 | */ |
| 99 | db_open(dbenv, &db_cats, "cats", 1); |
| 100 | |
| 101 | add_fruit(dbenv, db_fruit, "apple", "yellow delicious"); |
| 102 | |
| 103 | <span class="bold"><strong> add_color(dbenv, db_color, "blue", 0); |
| 104 | add_color(dbenv, db_color, "blue", 3);</strong></span> |
| 105 | |
| 106 | return (0); |
| 107 | } |
| 108 | |
| 109 | <span class="bold"><strong>int |
| 110 | add_color(DB_ENV *dbenv, DB *dbp, char *color, int increment) |
| 111 | { |
| 112 | DBT key, data; |
| 113 | DB_TXN *tid; |
| 114 | int fail, original, ret, t_ret; |
| 115 | char buf64; |
| 116 | |
| 117 | /* Initialization. */ |
| 118 | memset(&key, 0, sizeof(key)); |
| 119 | key.data = color; |
| 120 | key.size = strlen(color); |
| 121 | memset(&data, 0, sizeof(data)); |
| 122 | data.flags = DB_DBT_MALLOC; |
| 123 | |
| 124 | for (fail = 0;;) { |
| 125 | /* Begin the transaction. */ |
| 126 | if ((ret = dbenv->txn_begin(dbenv, NULL, &tid, 0)) != 0) { |
| 127 | dbenv->err(dbenv, ret, "DB_ENV->txn_begin"); |
| 128 | exit (1); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Get the key. If it exists, we increment the value. If it |
| 133 | * doesn't exist, we create it. |
| 134 | */ |
| 135 | switch (ret = dbp->get(dbp, tid, &key, &data, DB_RMW)) { |
| 136 | case 0: |
| 137 | original = atoi(data.data); |
| 138 | break; |
| 139 | case DB_LOCK_DEADLOCK: |
| 140 | default: |
| 141 | /* Retry the operation. */ |
| 142 | if ((t_ret = tid->abort(tid)) != 0) { |
| 143 | dbenv->err(dbenv, t_ret, "DB_TXN->abort"); |
| 144 | exit (1); |
| 145 | } |
| 146 | if (fail++ == MAXIMUM_RETRY) |
| 147 | return (ret); |
| 148 | continue; |
| 149 | case DB_NOTFOUND: |
| 150 | original = 0; |
| 151 | break; |
| 152 | } |
| 153 | if (data.data != NULL) |
| 154 | free(data.data); |
| 155 | |
| 156 | /* Create the new data item. */ |
| 157 | (void)snprintf(buf, sizeof(buf), "%d", original + increment); |
| 158 | data.data = buf; |
| 159 | data.size = strlen(buf) + 1; |
| 160 | |
| 161 | /* Store the new value. */ |
| 162 | switch (ret = dbp->put(dbp, tid, &key, &data, 0)) { |
| 163 | case 0: |
| 164 | /* Success: commit the change. */ |
| 165 | if ((ret = tid->commit(tid, 0)) != 0) { |
| 166 | dbenv->err(dbenv, ret, "DB_TXN->commit"); |
| 167 | exit (1); |
| 168 | } |
| 169 | return (0); |
| 170 | case DB_LOCK_DEADLOCK: |
| 171 | default: |
| 172 | /* Retry the operation. */ |
| 173 | if ((t_ret = tid->abort(tid)) != 0) { |
| 174 | dbenv->err(dbenv, t_ret, "DB_TXN->abort"); |
| 175 | exit (1); |
| 176 | } |
| 177 | if (fail++ == MAXIMUM_RETRY) |
| 178 | return (ret); |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | }</strong></span></pre> |
| 183 | <p>The <a href="../api_reference/C/dbcget.html#dbcget_DB_RMW" class="olink">DB_RMW</a> flag in the <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a> call specifies a write lock |
| 184 | should be acquired on the key/data pair, instead of the more obvious read |
| 185 | lock. We do this because the application expects to write the key/data |
| 186 | pair in a subsequent operation, and the transaction is much more likely to |
| 187 | deadlock if we first obtain a read lock and subsequently a write lock, than |
| 188 | if we obtain the write lock initially.</p> |
| 189 | </div> |
| 190 | <div class="navfooter"> |
| 191 | <hr /> |
| 192 | <table width="100%" summary="Navigation footer"> |
| 193 | <tr> |
| 194 | <td width="40%" align="left"><a accesskey="p" href="transapp_atomicity.html">Prev</a> </td> |
| 195 | <td width="20%" align="center"> |
| 196 | <a accesskey="u" href="transapp.html">Up</a> |
| 197 | </td> |
| 198 | <td width="40%" align="right"> <a accesskey="n" href="transapp_read.html">Next</a></td> |
| 199 | </tr> |
| 200 | <tr> |
| 201 | <td width="40%" align="left" valign="top">Atomicity </td> |
| 202 | <td width="20%" align="center"> |
| 203 | <a accesskey="h" href="index.html">Home</a> |
| 204 | </td> |
| 205 | <td width="40%" align="right" valign="top"> Degrees of isolation</td> |
| 206 | </tr> |
| 207 | </table> |
| 208 | </div> |
| 209 | </body> |
| 210 | </html> |