| 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>DB->associate, DB->open, DB->remove, DB->rename</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 Upgrade Guide" /> |
| 10 | <link rel="up" href="upgrade_4_1_toc.html" title="Chapter 9. Upgrading Berkeley DB 4.0 applications to Berkeley DB 4.1" /> |
| 11 | <link rel="prev" href="upgrade_4_1_excl.html" title="DB_EXCL" /> |
| 12 | <link rel="next" href="upgrade_4_1_log_register.html" title="DB_ENV->log_register" /> |
| 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">DB->associate, DB->open, DB->remove, DB->rename</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="upgrade_4_1_excl.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 9. Upgrading Berkeley DB 4.0 applications to Berkeley DB 4.1</th> |
| 26 | <td width="20%" align="right"> <a accesskey="n" href="upgrade_4_1_log_register.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="upgrade_4_1_fop"></a>DB->associate, DB->open, DB->remove, DB->rename</h2> |
| 36 | </div> |
| 37 | </div> |
| 38 | </div> |
| 39 | <p>Historic releases of Berkeley DB transaction-protected the <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a>, |
| 40 | <a href="../api_reference/C/dbremove.html" class="olink">DB->remove()</a>, and <a href="../api_reference/C/dbrename.html" class="olink">DB->rename()</a> methods, but did it in an implicit |
| 41 | way, that is, applications did not specify the <a href="../api_reference/C/txn.html" class="olink">TXN</a> handles |
| 42 | associated with the operations. This approach had a number of problems, |
| 43 | the most significant of which was there was no way to group operations |
| 44 | that included database creation, removal or rename. For example, |
| 45 | applications wanting to maintain a list of the databases in an |
| 46 | environment in a well-known database had no way to update the well-known |
| 47 | database and create a database within a single transaction, and so there |
| 48 | was no way to guarantee the list of databases was correct for the |
| 49 | environment after system or application failure. Another example might |
| 50 | be the creation of both a primary database and a database intended to |
| 51 | serve as a secondary index, where again there was no way to group the |
| 52 | creation of both databases in a single atomic operation.</p> |
| 53 | <p>In the 4.1 release of Berkeley DB, this is no longer the case. The |
| 54 | <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a> and <a href="../api_reference/C/dbassociate.html" class="olink">DB->associate()</a> methods now take a <a href="../api_reference/C/txn.html" class="olink">TXN</a> |
| 55 | handle returned by <a href="../api_reference/C/txnbegin.html" class="olink">DB_ENV->txn_begin()</a> as an optional argument. New |
| 56 | <a href="../api_reference/C/envdbremove.html" class="olink">DB_ENV->dbremove()</a> and <a href="../api_reference/C/envdbrename.html" class="olink">DB_ENV->dbrename()</a> methods taking a |
| 57 | <a href="../api_reference/C/txn.html" class="olink">TXN</a> handle as an optional argument have been added.</p> |
| 58 | <p>To upgrade, applications must add a <a href="../api_reference/C/txn.html" class="olink">TXN</a> parameter in the |
| 59 | appropriate location for the <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a> method calls, and the |
| 60 | <a href="../api_reference/C/dbassociate.html" class="olink">DB->associate()</a> method calls (in both cases, the second argument for |
| 61 | the C API, the first for the C++ or Java APIs).</p> |
| 62 | <p>Applications wanting to transaction-protect their <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a> and |
| 63 | <a href="../api_reference/C/dbassociate.html" class="olink">DB->associate()</a> method calls can add a NULL <a href="../api_reference/C/txn.html" class="olink">TXN</a> |
| 64 | argument and specify the <a href="../api_reference/C/envset_flags.html#envset_flags_DB_AUTO_COMMIT" class="olink">DB_AUTO_COMMIT</a> flag to the two calls, |
| 65 | which wraps the operation in an internal Berkeley DB transaction. |
| 66 | Applications wanting to transaction-protect the remove and rename |
| 67 | operations must rewrite their calls to the <a href="../api_reference/C/dbremove.html" class="olink">DB->remove()</a> and |
| 68 | <a href="../api_reference/C/dbrename.html" class="olink">DB->rename()</a> methods to be, instead, calls to the new |
| 69 | <a href="../api_reference/C/envdbremove.html" class="olink">DB_ENV->dbremove()</a> and <a href="../api_reference/C/envdbrename.html" class="olink">DB_ENV->dbrename()</a> methods. Applications not |
| 70 | wanting to transaction-protect any of the operations can add a NULL |
| 71 | argument to their <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a> and <a href="../api_reference/C/dbassociate.html" class="olink">DB->associate()</a> method calls and |
| 72 | require no further changes.</p> |
| 73 | <p>For example, an application currently opening and closing a database as |
| 74 | follows:</p> |
| 75 | <pre class="programlisting">DB *dbp; |
| 76 | DB_ENV *dbenv; |
| 77 | int ret; |
| 78 | |
| 79 | if ((ret = db_create(&dbp, dbenv, 0)) != 0) |
| 80 | goto err_handler; |
| 81 | |
| 82 | if ((ret = dbp->open(dbp, "file", NULL, DB_BTREE, |
| 83 | DB_CREATE, 0664)) != 0) { |
| 84 | (void)dbp->close(dbp); |
| 85 | goto err_handler; |
| 86 | }</pre> |
| 87 | <p>could transaction-protect the <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a> call as follows:</p> |
| 88 | <pre class="programlisting">DB *dbp; |
| 89 | DB_ENV *dbenv; |
| 90 | int ret; |
| 91 | |
| 92 | if ((ret = db_create(&dbp, dbenv, 0)) != 0) |
| 93 | goto err_handler; |
| 94 | |
| 95 | if ((ret = dbp->open(dbp, |
| 96 | NULL, "file", NULL, DB_BTREE, DB_CREATE | |
| 97 | DB_AUTO_COMMIT, 0664)) != 0) { |
| 98 | (void)dbp->close(dbp); |
| 99 | goto err_handler; |
| 100 | }</pre> |
| 101 | <p>An application currently removing a database as follows:</p> |
| 102 | <pre class="programlisting">DB *dbp; |
| 103 | DB_ENV *dbenv; |
| 104 | int ret; |
| 105 | |
| 106 | if ((ret = db_create(&dbp, dbenv, 0)) != 0) |
| 107 | goto err_handler; |
| 108 | |
| 109 | if ((ret = dbp->remove(dbp, "file", NULL, 0)) != 0) |
| 110 | goto err_handler;</pre> |
| 111 | <p>could transaction-protect the database removal as follows:</p> |
| 112 | <pre class="programlisting">DB *dbp; |
| 113 | DB_ENV *dbenv; |
| 114 | int ret; |
| 115 | |
| 116 | if ((ret = |
| 117 | dbenv->dbremove(dbenv, NULL, "file", NULL, DB_AUTO_COMMIT)) != 0) |
| 118 | goto err_handler;</pre> |
| 119 | <p>An application currently renaming a database as follows:</p> |
| 120 | <pre class="programlisting">DB *dbp; |
| 121 | DB_ENV *dbenv; |
| 122 | int ret; |
| 123 | |
| 124 | if ((ret = db_create(&dbp, dbenv, 0)) != 0) |
| 125 | goto err_handler; |
| 126 | |
| 127 | if ((ret = dbp->rename(dbp, "file", NULL, "newname", 0)) != 0) |
| 128 | goto err_handler;</pre> |
| 129 | <p>could transaction-protect the database renaming as follows:</p> |
| 130 | <pre class="programlisting">DB *dbp; |
| 131 | DB_ENV *dbenv; |
| 132 | int ret; |
| 133 | |
| 134 | if ((ret = dbenv->dbrename( |
| 135 | dbenv, NULL, "file", NULL, "newname", DB_AUTO_COMMIT)) != 0) |
| 136 | goto err_handler;</pre> |
| 137 | <p>These examples are the simplest possible translation, and will result in |
| 138 | behavior matching that of previous releases. For further discussion on |
| 139 | how to transaction-protect <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a> method calls, see |
| 140 | <a href="../programmer_reference/transapp_data_open.html" class="olink">Opening the databases</a>.</p> |
| 141 | <p><a href="../api_reference/C/db.html" class="olink">DB</a> handles that will later be used for transaction-protected |
| 142 | operations must be opened within a transaction. Specifying a |
| 143 | transaction handle to operations using handles not opened within a |
| 144 | transaction will return an error. Similarly, not specifying a |
| 145 | transaction handle to operations using handles that were opened within |
| 146 | a transaction will also return an error.</p> |
| 147 | </div> |
| 148 | <div class="navfooter"> |
| 149 | <hr /> |
| 150 | <table width="100%" summary="Navigation footer"> |
| 151 | <tr> |
| 152 | <td width="40%" align="left"><a accesskey="p" href="upgrade_4_1_excl.html">Prev</a> </td> |
| 153 | <td width="20%" align="center"> |
| 154 | <a accesskey="u" href="upgrade_4_1_toc.html">Up</a> |
| 155 | </td> |
| 156 | <td width="40%" align="right"> <a accesskey="n" href="upgrade_4_1_log_register.html">Next</a></td> |
| 157 | </tr> |
| 158 | <tr> |
| 159 | <td width="40%" align="left" valign="top">DB_EXCL </td> |
| 160 | <td width="20%" align="center"> |
| 161 | <a accesskey="h" href="index.html">Home</a> |
| 162 | </td> |
| 163 | <td width="40%" align="right" valign="top"> DB_ENV->log_register</td> |
| 164 | </tr> |
| 165 | </table> |
| 166 | </div> |
| 167 | </body> |
| 168 | </html> |