| 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 |
<code class="methodname">DB_ENV->set_flags()</code> |
| 72 |
|
| 73 |
or |
| 74 |
<code class="methodname">DB->open()</code> |
| 75 |
|
| 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 <stdio.h> |
| 111 |
#include <stdlib.h> |
| 112 |
|
| 113 |
#include "db.h" |
| 114 |
|
| 115 |
int |
| 116 |
main(void) |
| 117 |
{ |
| 118 |
int ret, ret_c; |
| 119 |
u_int32_t db_flags, env_flags; |
| 120 |
DB *dbp; |
| 121 |
DB_ENV *envp; |
| 122 |
DBT key, data; |
| 123 |
const char *db_home_dir = "/tmp/myEnvironment"; |
| 124 |
const char *file_name = "mydb.db"; |
| 125 |
const char *keystr ="thekey"; |
| 126 |
const char *datastr = "thedata"; |
| 127 |
|
| 128 |
dbp = NULL; |
| 129 |
envp = NULL; |
| 130 |
|
| 131 |
/* Open the environment */ |
| 132 |
ret = db_env_create(&envp, 0); |
| 133 |
if (ret != 0) { |
| 134 |
fprintf(stderr, "Error creating environment handle: %s\n", |
| 135 |
db_strerror(ret)); |
| 136 |
return (EXIT_FAILURE); |
| 137 |
} |
| 138 |
|
| 139 |
env_flags = DB_CREATE | /* Create the environment if it does |
| 140 |
* not already exist. */ |
| 141 |
DB_INIT_TXN | /* Initialize transactions */ |
| 142 |
DB_INIT_LOCK | /* Initialize locking. */ |
| 143 |
DB_INIT_LOG | /* Initialize logging */ |
| 144 |
DB_INIT_MPOOL; /* Initialize the in-memory cache. */ |
| 145 |
|
| 146 |
ret = envp->open(envp, db_home_dir, env_flags, 0); |
| 147 |
if (ret != 0) { |
| 148 |
fprintf(stderr, "Error opening environment: %s\n", |
| 149 |
db_strerror(ret)); |
| 150 |
goto err; |
| 151 |
} |
| 152 |
|
| 153 |
/* Initialize the DB handle */ |
| 154 |
ret = db_create(&dbp, envp, 0); |
| 155 |
if (ret != 0) { |
| 156 |
envp->err(envp, ret, "Database creation failed"); |
| 157 |
goto err; |
| 158 |
} |
| 159 |
|
| 160 |
db_flags = DB_CREATE | DB_AUTO_COMMIT; |
| 161 |
/* |
| 162 |
* Open the database. Note that we are using auto commit for the open, |
| 163 |
* so the database is able to support transactions. |
| 164 |
*/ |
| 165 |
ret = dbp->open(dbp, /* Pointer to the database */ |
| 166 |
NULL, /* Txn pointer */ |
| 167 |
file_name, /* File name */ |
| 168 |
NULL, /* Logical db name */ |
| 169 |
DB_BTREE, /* Database type (using btree) */ |
| 170 |
db_flags, /* Open flags */ |
| 171 |
0); /* File mode. Using defaults */ |
| 172 |
if (ret != 0) { |
| 173 |
envp->err(envp, ret, "Database '%s' open failed", |
| 174 |
file_name); |
| 175 |
goto err; |
| 176 |
} |
| 177 |
|
| 178 |
/* Prepare the DBTs */ |
| 179 |
memset(&key, 0, sizeof(DBT)); |
| 180 |
memset(&data, 0, sizeof(DBT)); |
| 181 |
|
| 182 |
key.data = &keystr; |
| 183 |
key.size = strlen(keystr) + 1; |
| 184 |
data.data = &datastr; |
| 185 |
data.size = strlen(datastr) + 1; |
| 186 |
|
| 187 |
|
| 188 |
/* |
| 189 |
* Perform the database write. A txn handle is not provided, but the |
| 190 |
* database support auto commit, so auto commit is used for the write. |
| 191 |
*/ |
| 192 |
ret = dbp->put(dbp, NULL, &key, &data, 0); |
| 193 |
if (ret != 0) { |
| 194 |
envp->err(envp, ret, "Database put failed."); |
| 195 |
goto err; |
| 196 |
} |
| 197 |
|
| 198 |
err: |
| 199 |
/* Close the database */ |
| 200 |
if (dbp != NULL) { |
| 201 |
ret_c = dbp->close(dbp, 0); |
| 202 |
if (ret_c != 0) { |
| 203 |
envp->err(envp, ret_c, "Database close failed."); |
| 204 |
ret = ret_c |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
/* Close the environment */ |
| 210 |
if (envp != NULL) { |
| 211 |
ret_c = envp->close(envp, 0); |
| 212 |
if (ret_c != 0) { |
| 213 |
fprintf(stderr, "environment close failed: %s\n", |
| 214 |
db_strerror(ret_c)); |
| 215 |
ret = ret_c; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); |
| 220 |
} </pre> |
| 221 |
</div> |
| 222 |
<div class="navfooter"> |
| 223 |
<hr /> |
| 224 |
<table width="100%" summary="Navigation footer"> |
| 225 |
<tr> |
| 226 |
<td width="40%" align="left"><a accesskey="p" href="abortresults.html">Prev</a> </td> |
| 227 |
<td width="20%" align="center"> |
| 228 |
<a accesskey="u" href="usingtxns.html">Up</a> |
| 229 |
</td> |
| 230 |
<td width="40%" align="right"> <a accesskey="n" href="nestedtxn.html">Next</a></td> |
| 231 |
</tr> |
| 232 |
<tr> |
| 233 |
<td width="40%" align="left" valign="top">Aborting a Transaction </td> |
| 234 |
<td width="20%" align="center"> |
| 235 |
<a accesskey="h" href="index.html">Home</a> |
| 236 |
</td> |
| 237 |
<td width="40%" align="right" valign="top"> Nested Transactions</td> |
| 238 |
</tr> |
| 239 |
</table> |
| 240 |
</div> |
| 241 |
</body> |
| 242 |
</html> |