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>Auto Commit</title> 7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> 9 <link rel="home" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" /> 10 <link rel="up" href="usingtxns.html" title="Chapter��3.��Transaction Basics" /> 11 <link rel="previous" href="abortresults.html" title="Aborting a Transaction" /> 12 <link rel="next" href="nestedtxn.html" title="Nested Transactions" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Auto Commit</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="abortresults.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��3.��Transaction Basics</th> 23 <td width="20%" align="right">��<a accesskey="n" href="nestedtxn.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="sect1" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title" style="clear: both"><a id="autocommit"></a>Auto Commit</h2> 33 </div> 34 </div> 35 <div></div> 36 </div> 37 <p> 38 While transactions are frequently used to provide atomicity to 39 multiple database 40 41 operations, it is sometimes necessary to perform 42 a single database 43 44 operation under the control of a transaction. 45 Rather than force you to obtain a transaction, perform the single 46 write operation, and then either commit or abort the transaction, 47 you can automatically group this sequence of events using 48 <span class="emphasis"><em>auto commit</em></span>. 49 </p> 50 <p> 51 To use auto commit: 52 </p> 53 <div class="orderedlist"> 54 <ol type="1"> 55 <li> 56 <p> 57 Open your environment and your databases 58 59 so that they support 60 transactions. See <a href="enabletxn.html">Enabling Transactions</a> 61 for details. 62 </p> 63 <p> 64 Note that frequently auto commit is used for the environment 65 or database open. To use auto commit for either your 66 environment or database open, specify 67 <tt class="literal">DB_AUTO_COMMIT</tt> to the 68 69 70 <tt class="methodname">DbEnv::set_flags()</tt> 71 or 72 73 <tt class="methodname">Db::open()</tt> 74 method. If you specify auto commit for the environment 75 open, then you do not need to also specify auto commit 76 for the database open. 77 </p> 78 </li> 79 <li> 80 <p> 81 Do not provide a transactional handle to the method that is 82 performing the database 83 84 write operation. 85 </p> 86 </li> 87 </ol> 88 </div> 89 <p> 90 Note that auto commit is not available for cursors. You must always 91 open your cursor using a transaction if you want the cursor's 92 operations to be transactional protected. See 93 <a href="txncursor.html">Transactional Cursors</a> for details on using 94 transactional cursors. 95 </p> 96 <p> 97 For example, the following uses auto commit to perform the database write operation: 98 </p> 99 <pre class="programlisting">#include "db_cxx.h" 100 101... 102 103int main(void) 104{ 105 u_int32_t env_flags = DB_CREATE | // If the environment does not 106 // exist, create it. 107 DB_INIT_LOCK | // Initialize locking 108 DB_INIT_LOG | // Initialize logging 109 DB_INIT_MPOOL | // Initialize the cache 110 DB_INIT_TXN; // Initialize transactions 111 112 u_int32_t db_flags = DB_CREATE | DB_AUTO_COMMIT; 113 Db *dbp = NULL; 114 const char *file_name = "mydb.db"; 115 const char *keystr ="thekey"; 116 const char *datastr = "thedata"; 117 118 std::string envHome("/export1/testEnv"); 119 DbEnv myEnv(0); 120 121 try { 122 123 myEnv.open(envHome.c_str(), env_flags, 0); 124 dbp = new Db(&myEnv, 0); 125 126 // Open the database. Note that we are using auto commit for 127 // the open, so the database is able to support transactions. 128 dbp->open(NULL, // Txn pointer 129 file_name, // File name 130 NULL, // Logical db name */ 131 DB_BTREE, // Database type (using btree) 132 db_flags, // Open flags 133 0); // File mode. Using defaults 134 135 Dbt key, data; 136 key.set_data(keystr); 137 key.set_size((strlen(keystr) + 1) * sizeof(char)); 138 key.set_data(datastr); 139 key.set_size((strlen(datastr) + 1) * sizeof(char)); 140 141 // Perform the write. Because the database was opened to support 142 // auto commit, this write is performed using auto commit. 143 db->put(NULL, &key, &data, 0); 144 145 } catch(DbException &e) { 146 std::cerr << "Error opening database and environment: " 147 << file_name << ", " 148 << envHome << std::endl; 149 std::cerr << e.what() << std::endl; 150 } 151 152 try { 153 if (dbp != NULL) 154 dbp->close(0); 155 myEnv.close(0); 156 } catch(DbException &e) { 157 std::cerr << "Error closing database and environment: " 158 << file_name << ", " 159 << envHome << std::endl; 160 std::cerr << e.what() << std::endl; 161 return (EXIT_FAILURE); 162 } 163 164 return (EXIT_SUCCESS); 165} </pre> 166 </div> 167 <div class="navfooter"> 168 <hr /> 169 <table width="100%" summary="Navigation footer"> 170 <tr> 171 <td width="40%" align="left"><a accesskey="p" href="abortresults.html">Prev</a>��</td> 172 <td width="20%" align="center"> 173 <a accesskey="u" href="usingtxns.html">Up</a> 174 </td> 175 <td width="40%" align="right">��<a accesskey="n" href="nestedtxn.html">Next</a></td> 176 </tr> 177 <tr> 178 <td width="40%" align="left" valign="top">Aborting a Transaction��</td> 179 <td width="20%" align="center"> 180 <a accesskey="h" href="index.html">Home</a> 181 </td> 182 <td width="40%" align="right" valign="top">��Nested Transactions</td> 183 </tr> 184 </table> 185 </div> 186 </body> 187</html> 188