| 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>Transactional cursors</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_read.html" title="Degrees of isolation" /> |
| 12 | <link rel="next" href="transapp_nested.html" title="Nested 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">Transactional cursors</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="transapp_read.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_nested.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_cursor"></a>Transactional cursors</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>Berkeley DB cursors may be used inside a transaction, exactly as any other |
| 42 | <a href="../api_reference/C/db.html" class="olink">DB</a> method. The enclosing transaction ID must be specified when |
| 43 | the cursor is created, but it does not then need to be further specified |
| 44 | on operations performed using the cursor. One important point to |
| 45 | remember is that a cursor <span class="bold"><strong>must be closed</strong></span> before the enclosing |
| 46 | transaction is committed or aborted.</p> |
| 47 | <p>The following code fragment uses a cursor to store a new key in the cats |
| 48 | database with four associated data items. The key is a name. The data |
| 49 | items are a company name and a list of the breeds of cat owned. Each |
| 50 | of the data entries is stored as a duplicate data item. In this |
| 51 | example, transactions are necessary to ensure that either all or none |
| 52 | of the data items appear in case of system or application failure.</p> |
| 53 | <pre class="programlisting">int |
| 54 | main(int argc, char *argv) |
| 55 | { |
| 56 | extern int optind; |
| 57 | DB *db_cats, *db_color, *db_fruit; |
| 58 | DB_ENV *dbenv; |
| 59 | int ch; |
| 60 | |
| 61 | while ((ch = getopt(argc, argv, "")) != EOF) |
| 62 | switch (ch) { |
| 63 | case '?': |
| 64 | default: |
| 65 | usage(); |
| 66 | } |
| 67 | argc -= optind; |
| 68 | argv += optind; |
| 69 | |
| 70 | env_dir_create(); |
| 71 | env_open(&dbenv); |
| 72 | |
| 73 | /* Open database: Key is fruit class; Data is specific type. */ |
| 74 | db_open(dbenv, &db_fruit, "fruit", 0); |
| 75 | |
| 76 | /* Open database: Key is a color; Data is an integer. */ |
| 77 | db_open(dbenv, &db_color, "color", 0); |
| 78 | |
| 79 | /* |
| 80 | * Open database: |
| 81 | * Key is a name; Data is: company name, cat breeds. |
| 82 | */ |
| 83 | db_open(dbenv, &db_cats, "cats", 1); |
| 84 | |
| 85 | add_fruit(dbenv, db_fruit, "apple", "yellow delicious"); |
| 86 | |
| 87 | add_color(dbenv, db_color, "blue", 0); |
| 88 | add_color(dbenv, db_color, "blue", 3); |
| 89 | |
| 90 | <span class="bold"><strong> add_cat(dbenv, db_cats, |
| 91 | "Amy Adams", |
| 92 | "Oracle", |
| 93 | "abyssinian", |
| 94 | "bengal", |
| 95 | "chartreaux", |
| 96 | NULL);</strong></span> |
| 97 | |
| 98 | return (0); |
| 99 | } |
| 100 | |
| 101 | <span class="bold"><strong>int |
| 102 | add_cat(DB_ENV *dbenv, DB *db, char *name, ...) |
| 103 | { |
| 104 | va_list ap; |
| 105 | DBC *dbc; |
| 106 | DBT key, data; |
| 107 | DB_TXN *tid; |
| 108 | int fail, ret, t_ret; |
| 109 | char *s; |
| 110 | |
| 111 | /* Initialization. */ |
| 112 | fail = 0; |
| 113 | |
| 114 | memset(&key, 0, sizeof(key)); |
| 115 | memset(&data, 0, sizeof(data)); |
| 116 | key.data = name; |
| 117 | key.size = strlen(name); |
| 118 | |
| 119 | retry: /* Begin the transaction. */ |
| 120 | if ((ret = dbenv->txn_begin(dbenv, NULL, &tid, 0)) != 0) { |
| 121 | dbenv->err(dbenv, ret, "DB_ENV->txn_begin"); |
| 122 | exit (1); |
| 123 | } |
| 124 | |
| 125 | /* Delete any previously existing item. */ |
| 126 | switch (ret = db->del(db, tid, &key, 0)) { |
| 127 | case 0: |
| 128 | case DB_NOTFOUND: |
| 129 | break; |
| 130 | case DB_LOCK_DEADLOCK: |
| 131 | default: |
| 132 | /* Retry the operation. */ |
| 133 | if ((t_ret = tid->abort(tid)) != 0) { |
| 134 | dbenv->err(dbenv, t_ret, "DB_TXN->abort"); |
| 135 | exit (1); |
| 136 | } |
| 137 | if (fail++ == MAXIMUM_RETRY) |
| 138 | return (ret); |
| 139 | goto retry; |
| 140 | } |
| 141 | |
| 142 | /* Create a cursor. */ |
| 143 | if ((ret = db->cursor(db, tid, &dbc, 0)) != 0) { |
| 144 | dbenv->err(dbenv, ret, "db->cursor"); |
| 145 | exit (1); |
| 146 | } |
| 147 | |
| 148 | /* Append the items, in order. */ |
| 149 | va_start(ap, name); |
| 150 | while ((s = va_arg(ap, char *)) != NULL) { |
| 151 | data.data = s; |
| 152 | data.size = strlen(s); |
| 153 | switch (ret = dbc->put(dbc, &key, &data, DB_KEYLAST)) { |
| 154 | case 0: |
| 155 | break; |
| 156 | case DB_LOCK_DEADLOCK: |
| 157 | default: |
| 158 | va_end(ap); |
| 159 | |
| 160 | /* Retry the operation. */ |
| 161 | if ((t_ret = dbc->close(dbc)) != 0) { |
| 162 | dbenv->err( |
| 163 | dbenv, t_ret, "dbc->close"); |
| 164 | exit (1); |
| 165 | } |
| 166 | if ((t_ret = tid->abort(tid)) != 0) { |
| 167 | dbenv->err(dbenv, t_ret, "DB_TXN->abort"); |
| 168 | exit (1); |
| 169 | } |
| 170 | if (fail++ == MAXIMUM_RETRY) |
| 171 | return (ret); |
| 172 | goto retry; |
| 173 | } |
| 174 | } |
| 175 | va_end(ap); |
| 176 | |
| 177 | /* Success: commit the change. */ |
| 178 | if ((ret = dbc->close(dbc)) != 0) { |
| 179 | dbenv->err(dbenv, ret, "dbc->close"); |
| 180 | exit (1); |
| 181 | } |
| 182 | if ((ret = tid->commit(tid, 0)) != 0) { |
| 183 | dbenv->err(dbenv, ret, "DB_TXN->commit"); |
| 184 | exit (1); |
| 185 | } |
| 186 | return (0); |
| 187 | }</strong></span></pre> |
| 188 | </div> |
| 189 | <div class="navfooter"> |
| 190 | <hr /> |
| 191 | <table width="100%" summary="Navigation footer"> |
| 192 | <tr> |
| 193 | <td width="40%" align="left"><a accesskey="p" href="transapp_read.html">Prev</a> </td> |
| 194 | <td width="20%" align="center"> |
| 195 | <a accesskey="u" href="transapp.html">Up</a> |
| 196 | </td> |
| 197 | <td width="40%" align="right"> <a accesskey="n" href="transapp_nested.html">Next</a></td> |
| 198 | </tr> |
| 199 | <tr> |
| 200 | <td width="40%" align="left" valign="top">Degrees of isolation </td> |
| 201 | <td width="20%" align="center"> |
| 202 | <a accesskey="h" href="index.html">Home</a> |
| 203 | </td> |
| 204 | <td width="40%" align="right" valign="top"> Nested transactions</td> |
| 205 | </tr> |
| 206 | </table> |
| 207 | </div> |
| 208 | </body> |
| 209 | </html> |