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.73.2" /> 9 <link rel="start" 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="prev" 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> 36 <p> 37 While transactions are frequently used to provide atomicity to 38 multiple database 39 40 operations, it is sometimes necessary to perform 41 a single database 42 43 operation under the control of a transaction. 44 Rather than force you to obtain a transaction, perform the single 45 write operation, and then either commit or abort the transaction, 46 you can automatically group this sequence of events using 47 <span class="emphasis"><em>auto commit</em></span>. 48 </p> 49 <p> 50 To use auto commit: 51 </p> 52 <div class="orderedlist"> 53 <ol type="1"> 54 <li> 55 <p> 56 Open your environment and your databases 57 58 so that they support 59 transactions. See <a class="xref" href="enabletxn.html" title="Chapter��2.��Enabling Transactions">Enabling Transactions</a> 60 for details. 61 </p> 62 <p> 63 Note that frequently auto commit is used for the environment 64 or database open. To use auto commit for either your 65 environment or database open, specify 66 <code class="literal">DB_AUTO_COMMIT</code> to the 67 68 69 <code class="methodname">DbEnv::set_flags()</code> 70 or 71 72 <code class="methodname">Db::open()</code> 73 method. If you specify auto commit for the environment 74 open, then you do not need to also specify auto commit 75 for the database open. 76 </p> 77 </li> 78 <li> 79 <p> 80 Do not provide a transactional handle to the method that is 81 performing the database 82 83 write operation. 84 </p> 85 </li> 86 </ol> 87 </div> 88 <p> 89 Note that auto commit is not available for cursors. You must always 90 open your cursor using a transaction if you want the cursor's 91 operations to be transactional protected. See 92 <a class="xref" href="txncursor.html" title="Transactional Cursors">Transactional Cursors</a> for details on using 93 transactional cursors. 94 </p> 95 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> 96 <h3 class="title">Note</h3> 97 <p> 98 Never have more than one active transaction in your thread 99 at a time. This is especially a problem if you mix an 100 explicit transaction with another operation that uses auto 101 commit. Doing so can result in undetectable deadlocks. 102 </p> 103 </div> 104 <p> 105 For example, the following uses auto commit to perform the database write operation: 106 </p> 107 <pre class="programlisting">#include "db_cxx.h" 108 109... 110 111int main(void) 112{ 113 u_int32_t env_flags = DB_CREATE | // If the environment does not 114 // exist, create it. 115 DB_INIT_LOCK | // Initialize locking 116 DB_INIT_LOG | // Initialize logging 117 DB_INIT_MPOOL | // Initialize the cache 118 DB_INIT_TXN; // Initialize transactions 119 120 u_int32_t db_flags = DB_CREATE | DB_AUTO_COMMIT; 121 Db *dbp = NULL; 122 const char *file_name = "mydb.db"; 123 const char *keystr ="thekey"; 124 const char *datastr = "thedata"; 125 126 std::string envHome("/export1/testEnv"); 127 DbEnv myEnv(0); 128 129 try { 130 131 myEnv.open(envHome.c_str(), env_flags, 0); 132 dbp = new Db(&myEnv, 0); 133 134 // Open the database. Note that we are using auto commit for 135 // the open, so the database is able to support transactions. 136 dbp->open(NULL, // Txn pointer 137 file_name, // File name 138 NULL, // Logical db name */ 139 DB_BTREE, // Database type (using btree) 140 db_flags, // Open flags 141 0); // File mode. Using defaults 142 143 Dbt key, data; 144 key.set_data(keystr); 145 key.set_size((strlen(keystr) + 1) * sizeof(char)); 146 key.set_data(datastr); 147 key.set_size((strlen(datastr) + 1) * sizeof(char)); 148 149 // Perform the write. Because the database was opened to support 150 // auto commit, this write is performed using auto commit. 151 db->put(NULL, &key, &data, 0); 152 153 } catch(DbException &e) { 154 std::cerr << "Error opening database and environment: " 155 << file_name << ", " 156 << envHome << std::endl; 157 std::cerr << e.what() << std::endl; 158 } 159 160 try { 161 if (dbp != NULL) 162 dbp->close(0); 163 myEnv.close(0); 164 } catch(DbException &e) { 165 std::cerr << "Error closing database and environment: " 166 << file_name << ", " 167 << envHome << std::endl; 168 std::cerr << e.what() << std::endl; 169 return (EXIT_FAILURE); 170 } 171 172 return (EXIT_SUCCESS); 173} </pre> 174 </div> 175 <div class="navfooter"> 176 <hr /> 177 <table width="100%" summary="Navigation footer"> 178 <tr> 179 <td width="40%" align="left"><a accesskey="p" href="abortresults.html">Prev</a>��</td> 180 <td width="20%" align="center"> 181 <a accesskey="u" href="usingtxns.html">Up</a> 182 </td> 183 <td width="40%" align="right">��<a accesskey="n" href="nestedtxn.html">Next</a></td> 184 </tr> 185 <tr> 186 <td width="40%" align="left" valign="top">Aborting a Transaction��</td> 187 <td width="20%" align="center"> 188 <a accesskey="h" href="index.html">Home</a> 189 </td> 190 <td width="40%" align="right" valign="top">��Nested Transactions</td> 191 </tr> 192 </table> 193 </div> 194 </body> 195</html> 196