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