| 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>Chapter 3. Transaction Basics</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="index.html" title="Getting Started with Berkeley DB Transaction Processing" /> |
| 11 |
<link rel="prev" href="envopen.html" title="Opening a Transactional Environment and Database" /> |
| 12 |
<link rel="next" href="nodurabletxn.html" title="Non-Durable 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">Chapter 3. Transaction Basics</th> |
| 22 |
</tr> |
| 23 |
<tr> |
| 24 |
<td width="20%" align="left"><a accesskey="p" href="envopen.html">Prev</a> </td> |
| 25 |
<th width="60%" align="center"> </th> |
| 26 |
<td width="20%" align="right"> <a accesskey="n" href="nodurabletxn.html">Next</a></td> |
| 27 |
</tr> |
| 28 |
</table> |
| 29 |
<hr /> |
| 30 |
</div> |
| 31 |
<div class="chapter" lang="en" xml:lang="en"> |
| 32 |
<div class="titlepage"> |
| 33 |
<div> |
| 34 |
<div> |
| 35 |
<h2 class="title"><a id="usingtxns"></a>Chapter 3. Transaction Basics</h2> |
| 36 |
</div> |
| 37 |
</div> |
| 38 |
</div> |
| 39 |
<div class="toc"> |
| 40 |
<p> |
| 41 |
<b>Table of Contents</b> |
| 42 |
</p> |
| 43 |
<dl> |
| 44 |
<dt> |
| 45 |
<span class="sect1"> |
| 46 |
<a href="usingtxns.html#commitresults">Committing a Transaction</a> |
| 47 |
</span> |
| 48 |
</dt> |
| 49 |
<dt> |
| 50 |
<span class="sect1"> |
| 51 |
<a href="nodurabletxn.html">Non-Durable Transactions</a> |
| 52 |
</span> |
| 53 |
</dt> |
| 54 |
<dt> |
| 55 |
<span class="sect1"> |
| 56 |
<a href="abortresults.html">Aborting a Transaction</a> |
| 57 |
</span> |
| 58 |
</dt> |
| 59 |
<dt> |
| 60 |
<span class="sect1"> |
| 61 |
<a href="autocommit.html">Auto Commit</a> |
| 62 |
</span> |
| 63 |
</dt> |
| 64 |
<dt> |
| 65 |
<span class="sect1"> |
| 66 |
<a href="nestedtxn.html">Nested Transactions</a> |
| 67 |
</span> |
| 68 |
</dt> |
| 69 |
<dt> |
| 70 |
<span class="sect1"> |
| 71 |
<a href="txncursor.html">Transactional Cursors</a> |
| 72 |
</span> |
| 73 |
</dt> |
| 74 |
<dt> |
| 75 |
<span class="sect1"> |
| 76 |
<a href="txnindices.html">Secondary Indices with Transaction Applications</a> |
| 77 |
</span> |
| 78 |
</dt> |
| 79 |
<dt> |
| 80 |
<span class="sect1"> |
| 81 |
<a href="maxtxns.html">Configuring the Transaction Subsystem</a> |
| 82 |
</span> |
| 83 |
</dt> |
| 84 |
</dl> |
| 85 |
</div> |
| 86 |
<p> |
| 87 |
Once you have enabled transactions for your environment and your databases, |
| 88 |
you can use them to protect your database operations. You do this by |
| 89 |
acquiring a transaction handle and then using that handle for any |
| 90 |
database operation that you want to participate in that transaction. |
| 91 |
</p> |
| 92 |
<p> |
| 93 |
You obtain a transaction handle using the |
| 94 |
<span><code class="methodname">DB_ENV->txn_begin()</code> method.</span> |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
</p> |
| 100 |
<p> |
| 101 |
Once you have completed all of the operations that you want to include |
| 102 |
in the transaction, you must commit the transaction using the |
| 103 |
<span><code class="methodname">DB_TXN->commit()</code> method.</span> |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
</p> |
| 110 |
<p> |
| 111 |
If, for any reason, you want to abandon the transaction, you abort |
| 112 |
it using |
| 113 |
<span><code class="methodname">DB_TXN->abort()</code>.</span> |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
</p> |
| 121 |
<p> |
| 122 |
Any transaction handle that has been committed or aborted can no longer |
| 123 |
be used by your application. |
| 124 |
</p> |
| 125 |
<p> |
| 126 |
Finally, you must make sure that all transaction handles are either |
| 127 |
committed or aborted before closing your databases and environment. |
| 128 |
</p> |
| 129 |
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 130 |
<h3 class="title">Note</h3> |
| 131 |
<p> |
| 132 |
If you only want to transaction protect a single database write operation, you can use auto commit to |
| 133 |
perform the transaction administration. When you use auto commit, you do not need an explicit transaction |
| 134 |
handle. See <a class="xref" href="autocommit.html" title="Auto Commit">Auto Commit</a> for more information. |
| 135 |
</p> |
| 136 |
</div> |
| 137 |
<p> |
| 138 |
For example, the following example opens a transactional-enabled environment and |
| 139 |
database, obtains a transaction handle, and then performs a write |
| 140 |
operation under its protection. In the event of any failure in the |
| 141 |
write operation, the transaction is aborted and the database is left in a |
| 142 |
state as if no operations had ever been attempted in the first place. |
| 143 |
</p> |
| 144 |
<pre class="programlisting">#include <stdio.h> |
| 145 |
#include <stdlib.h> |
| 146 |
|
| 147 |
#include "db.h" |
| 148 |
|
| 149 |
int |
| 150 |
main(void) |
| 151 |
{ |
| 152 |
int ret, ret_c; |
| 153 |
u_int32_t db_flags, env_flags; |
| 154 |
DB *dbp; |
| 155 |
DB_ENV *envp; |
| 156 |
DBT key, data; |
| 157 |
DB_TXN *txn; |
| 158 |
const char *db_home_dir = "/tmp/myEnvironment"; |
| 159 |
const char *file_name = "mydb.db"; |
| 160 |
const char *keystr ="thekey"; |
| 161 |
const char *datastr = "thedata"; |
| 162 |
|
| 163 |
dbp = NULL; |
| 164 |
envp = NULL; |
| 165 |
|
| 166 |
/* Open the environment */ |
| 167 |
ret = db_env_create(&envp, 0); |
| 168 |
if (ret != 0) { |
| 169 |
fprintf(stderr, "Error creating environment handle: %s\n", |
| 170 |
db_strerror(ret)); |
| 171 |
return (EXIT_FAILURE); |
| 172 |
} |
| 173 |
|
| 174 |
env_flags = DB_CREATE | /* Create the environment if it does |
| 175 |
* not already exist. */ |
| 176 |
DB_INIT_TXN | /* Initialize transactions */ |
| 177 |
DB_INIT_LOCK | /* Initialize locking. */ |
| 178 |
DB_INIT_LOG | /* Initialize logging */ |
| 179 |
DB_INIT_MPOOL; /* Initialize the in-memory cache. */ |
| 180 |
|
| 181 |
ret = envp->open(envp, db_home_dir, env_flags, 0); |
| 182 |
if (ret != 0) { |
| 183 |
fprintf(stderr, "Error opening environment: %s\n", |
| 184 |
db_strerror(ret)); |
| 185 |
goto err; |
| 186 |
} |
| 187 |
|
| 188 |
/* Initialize the DB handle */ |
| 189 |
ret = db_create(&dbp, envp, 0); |
| 190 |
if (ret != 0) { |
| 191 |
envp->err(envp, ret, "Database creation failed"); |
| 192 |
goto err; |
| 193 |
} |
| 194 |
|
| 195 |
db_flags = DB_CREATE | DB_AUTO_COMMIT; |
| 196 |
/* |
| 197 |
* Open the database. Note that we are using auto commit for the open, |
| 198 |
* so the database is able to support transactions. |
| 199 |
*/ |
| 200 |
ret = dbp->open(dbp, /* Pointer to the database */ |
| 201 |
NULL, /* Txn pointer */ |
| 202 |
file_name, /* File name */ |
| 203 |
NULL, /* Logical db name */ |
| 204 |
DB_BTREE, /* Database type (using btree) */ |
| 205 |
db_flags, /* Open flags */ |
| 206 |
0); /* File mode. Using defaults */ |
| 207 |
if (ret != 0) { |
| 208 |
envp->err(envp, ret, "Database '%s' open failed", |
| 209 |
file_name); |
| 210 |
goto err; |
| 211 |
} |
| 212 |
|
| 213 |
/* Prepare the DBTs */ |
| 214 |
memset(&key, 0, sizeof(DBT)); |
| 215 |
memset(&data, 0, sizeof(DBT)); |
| 216 |
|
| 217 |
key.data = &keystr; |
| 218 |
key.size = strlen(keystr) + 1; |
| 219 |
data.data = &datastr; |
| 220 |
data.size = strlen(datastr) + 1; |
| 221 |
|
| 222 |
/* Get the txn handle */ |
| 223 |
txn = NULL; |
| 224 |
ret = envp->txn_begin(envp, NULL, &txn, 0); |
| 225 |
if (ret != 0) { |
| 226 |
envp->err(envp, ret, "Transaction begin failed."); |
| 227 |
goto err; |
| 228 |
} |
| 229 |
|
| 230 |
/* |
| 231 |
* Perform the database write. If this fails, abort the transaction. |
| 232 |
*/ |
| 233 |
ret = dbp->put(dbp, txn, &key, &data, 0); |
| 234 |
if (ret != 0) { |
| 235 |
envp->err(envp, ret, "Database put failed."); |
| 236 |
txn->abort(txn); |
| 237 |
goto err; |
| 238 |
} |
| 239 |
|
| 240 |
/* |
| 241 |
* Commit the transaction. Note that the transaction handle |
| 242 |
* can no longer be used. |
| 243 |
*/ |
| 244 |
ret = txn->commit(txn, 0); |
| 245 |
if (ret != 0) { |
| 246 |
envp->err(envp, ret, "Transaction commit failed."); |
| 247 |
goto err; |
| 248 |
} |
| 249 |
|
| 250 |
err: |
| 251 |
/* Close the database */ |
| 252 |
if (dbp != NULL) { |
| 253 |
ret_c = dbp->close(dbp, 0); |
| 254 |
if (ret_c != 0) { |
| 255 |
envp->err(envp, ret_c, "Database close failed."); |
| 256 |
ret = ret_c |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
/* Close the environment */ |
| 262 |
if (envp != NULL) { |
| 263 |
ret_c = envp->close(envp, 0); |
| 264 |
if (ret_c != 0) { |
| 265 |
fprintf(stderr, "environment close failed: %s\n", |
| 266 |
db_strerror(ret_c)); |
| 267 |
ret = ret_c; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); |
| 272 |
} </pre> |
| 273 |
<div class="sect1" lang="en" xml:lang="en"> |
| 274 |
<div class="titlepage"> |
| 275 |
<div> |
| 276 |
<div> |
| 277 |
<h2 class="title" style="clear: both"><a id="commitresults"></a>Committing a Transaction</h2> |
| 278 |
</div> |
| 279 |
</div> |
| 280 |
</div> |
| 281 |
<p> |
| 282 |
In order to fully understand what is happening when you commit |
| 283 |
a transaction, you must first understand a little about what |
| 284 |
DB is doing with |
| 285 |
|
| 286 |
<span> |
| 287 |
the logging subsystem. |
| 288 |
</span> |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
Logging causes all database write operations to be identified in |
| 293 |
|
| 294 |
<span>logs, and by default these |
| 295 |
logs are backed by files on disk. These logs are used to restore your databases |
| 296 |
|
| 297 |
</span> |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
in the event of a system or application failure, so by performing |
| 302 |
logging, DB ensures the integrity of your data. |
| 303 |
</p> |
| 304 |
<p> |
| 305 |
Moreover, DB performs <span class="emphasis"><em>write-ahead</em></span> |
| 306 |
logging. This means that information is written to the logs |
| 307 |
<span class="emphasis"><em>before</em></span> the actual database |
| 308 |
|
| 309 |
is changed. |
| 310 |
This means that all write activity performed under the |
| 311 |
protection of the transaction is noted in the log before |
| 312 |
the transaction is committed. Be aware, however, that database |
| 313 |
maintains logs in-memory. If you are backing your logs on |
| 314 |
disk, the log information will eventually be written to the log |
| 315 |
files, but while the transaction is on-going the log data may be |
| 316 |
held only in memory. |
| 317 |
</p> |
| 318 |
<p> |
| 319 |
When you commit a transaction, the following occurs: |
| 320 |
</p> |
| 321 |
<div class="itemizedlist"> |
| 322 |
<ul type="disc"> |
| 323 |
<li> |
| 324 |
<p> |
| 325 |
A commit record is written to the log. This |
| 326 |
indicates that the modifications made by the |
| 327 |
transaction are now permanent. By default, this write is performed synchronously to disk so the |
| 328 |
commit record arrives in the log files before any other actions are taken. |
| 329 |
</p> |
| 330 |
</li> |
| 331 |
<li> |
| 332 |
<p> |
| 333 |
Any log information held in memory is (by default) |
| 334 |
synchronously written to disk. Note that this requirement can be |
| 335 |
relaxed, depending on the type of commit you perform. |
| 336 |
See <a class="xref" href="nodurabletxn.html" title="Non-Durable Transactions">Non-Durable Transactions</a> for |
| 337 |
more information. |
| 338 |
<span>Also, if you are |
| 339 |
maintaining your logs entirely in-memory, then this |
| 340 |
step will of course not be taken. To configure your |
| 341 |
logging system for in-memory usage, see |
| 342 |
<a class="xref" href="logconfig.html#inmemorylogging" title="Configuring In-Memory Logging">Configuring In-Memory Logging</a>. |
| 343 |
</span> |
| 344 |
</p> |
| 345 |
</li> |
| 346 |
<li> |
| 347 |
<p> |
| 348 |
All locks held by the transaction are released. This means |
| 349 |
that read operations performed by other transactions or |
| 350 |
threads of control can now see the modifications without |
| 351 |
resorting to uncommitted reads (see <a class="xref" href="isolation.html#dirtyreads" title="Reading Uncommitted Data">Reading Uncommitted Data</a> for more information). |
| 352 |
</p> |
| 353 |
</li> |
| 354 |
</ul> |
| 355 |
</div> |
| 356 |
<p> |
| 357 |
To commit a transaction, you simply call |
| 358 |
<span><code class="methodname">DB_TXN->commit()</code>.</span> |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
</p> |
| 364 |
<p> |
| 365 |
Notice that committing a transaction does not necessarily cause data |
| 366 |
modified in your memory cache to be written to the files |
| 367 |
backing your databases on disk. Dirtied database pages are written |
| 368 |
for a number of reasons, but a transactional |
| 369 |
commit is not one of them. The following are the things that can cause a dirtied |
| 370 |
database page to be written to the backing database file: |
| 371 |
</p> |
| 372 |
<div class="itemizedlist"> |
| 373 |
<ul type="disc"> |
| 374 |
<li> |
| 375 |
<p> |
| 376 |
Checkpoints. |
| 377 |
</p> |
| 378 |
<p> |
| 379 |
Checkpoints cause all dirtied pages currently existing |
| 380 |
in the cache to be written to disk, and a checkpoint |
| 381 |
record is then written to the logs. You can run checkpoints |
| 382 |
explicitly. For more information on checkpoints, |
| 383 |
see <a class="xref" href="filemanagement.html#checkpoints" title="Checkpoints">Checkpoints</a>. |
| 384 |
</p> |
| 385 |
</li> |
| 386 |
<li> |
| 387 |
<p> |
| 388 |
Cache is full. |
| 389 |
</p> |
| 390 |
<p> |
| 391 |
If the in-memory cache fills up, then dirtied pages |
| 392 |
might be written to disk in order to free up space for other |
| 393 |
pages that your application needs to use. Note that if |
| 394 |
dirtied pages are written to the database files, then |
| 395 |
any log records that describe how those pages were |
| 396 |
dirtied are written to disk before the database |
| 397 |
pages are written. |
| 398 |
</p> |
| 399 |
</li> |
| 400 |
</ul> |
| 401 |
</div> |
| 402 |
<p> |
| 403 |
Be aware that because your transaction commit caused database |
| 404 |
|
| 405 |
modifications recorded in your logs to be forced to disk, your modifications |
| 406 |
are by default "persistent" in that they can be recovered in the event of |
| 407 |
an application or system failure. However, recovery time is |
| 408 |
gated by how much data has been modified since the last |
| 409 |
checkpoint, so for applications that perform a lot of writes, |
| 410 |
you may want to run a checkpoint with some frequency. |
| 411 |
</p> |
| 412 |
<p> |
| 413 |
Note that once you have committed a transaction, the transaction |
| 414 |
handle that you used for the transaction is no longer valid. To |
| 415 |
perform database activities under the control of a new |
| 416 |
transaction, you must obtain a fresh transaction handle. |
| 417 |
</p> |
| 418 |
</div> |
| 419 |
</div> |
| 420 |
<div class="navfooter"> |
| 421 |
<hr /> |
| 422 |
<table width="100%" summary="Navigation footer"> |
| 423 |
<tr> |
| 424 |
<td width="40%" align="left"><a accesskey="p" href="envopen.html">Prev</a> </td> |
| 425 |
<td width="20%" align="center"> </td> |
| 426 |
<td width="40%" align="right"> <a accesskey="n" href="nodurabletxn.html">Next</a></td> |
| 427 |
</tr> |
| 428 |
<tr> |
| 429 |
<td width="40%" align="left" valign="top">Opening a Transactional Environment and |
| 430 |
<span>Database</span> |
| 431 |
|
| 432 |
|
| 433 |
</td> |
| 434 |
<td width="20%" align="center"> |
| 435 |
<a accesskey="h" href="index.html">Home</a> |
| 436 |
</td> |
| 437 |
<td width="40%" align="right" valign="top"> Non-Durable Transactions</td> |
| 438 |
</tr> |
| 439 |
</table> |
| 440 |
</div> |
| 441 |
</body> |
| 442 |
</html> |