| 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 Store or 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 |
<dd> |
| 75 |
<dl> |
| 76 |
<dt> |
| 77 |
<span class="sect2"> |
| 78 |
<a href="txncursor.html#dplcursors">Using Transactional DPL Cursors</a> |
| 79 |
</span> |
| 80 |
</dt> |
| 81 |
</dl> |
| 82 |
</dd> |
| 83 |
<dt> |
| 84 |
<span class="sect1"> |
| 85 |
<a href="txnindices.html">Secondary Indices with Transaction Applications</a> |
| 86 |
</span> |
| 87 |
</dt> |
| 88 |
<dt> |
| 89 |
<span class="sect1"> |
| 90 |
<a href="maxtxns.html">Configuring the Transaction Subsystem</a> |
| 91 |
</span> |
| 92 |
</dt> |
| 93 |
</dl> |
| 94 |
</div> |
| 95 |
<p> |
| 96 |
Once you have enabled transactions for your environment and your databases, |
| 97 |
you can use them to protect your database operations. You do this by |
| 98 |
acquiring a transaction handle and then using that handle for any |
| 99 |
database operation that you want to participate in that transaction. |
| 100 |
</p> |
| 101 |
<p> |
| 102 |
You obtain a transaction handle using the |
| 103 |
|
| 104 |
|
| 105 |
<span><code class="methodname">Environment.beginTransaction()</code> method.</span> |
| 106 |
|
| 107 |
|
| 108 |
</p> |
| 109 |
<p> |
| 110 |
Once you have completed all of the operations that you want to include |
| 111 |
in the transaction, you must commit the transaction using the |
| 112 |
|
| 113 |
|
| 114 |
<span><code class="methodname">Transaction.commit()</code> method.</span> |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
</p> |
| 119 |
<p> |
| 120 |
If, for any reason, you want to abandon the transaction, you abort |
| 121 |
it using |
| 122 |
|
| 123 |
|
| 124 |
<span><code class="methodname">Transaction.abort()</code>.</span> |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
</p> |
| 130 |
<p> |
| 131 |
Any transaction handle that has been committed or aborted can no longer |
| 132 |
be used by your application. |
| 133 |
</p> |
| 134 |
<p> |
| 135 |
Finally, you must make sure that all transaction handles are either |
| 136 |
committed or aborted before closing your databases and environment. |
| 137 |
</p> |
| 138 |
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 139 |
<h3 class="title">Note</h3> |
| 140 |
<p> |
| 141 |
If you only want to transaction protect a single database write operation, you can use auto commit to |
| 142 |
perform the transaction administration. When you use auto commit, you do not need an explicit transaction |
| 143 |
handle. See <a class="xref" href="autocommit.html" title="Auto Commit">Auto Commit</a> for more information. |
| 144 |
</p> |
| 145 |
</div> |
| 146 |
<p> |
| 147 |
For example, the following example opens a transactional-enabled environment and |
| 148 |
store, obtains a transaction handle, and then performs a write |
| 149 |
operation under its protection. In the event of any failure in the |
| 150 |
write operation, the transaction is aborted and the store is left in a |
| 151 |
state as if no operations had ever been attempted in the first place. |
| 152 |
|
| 153 |
</p> |
| 154 |
<pre class="programlisting">package persist.txn; |
| 155 |
|
| 156 |
import com.sleepycat.db.DatabaseException; |
| 157 |
import com.sleepycat.db.Environment; |
| 158 |
import com.sleepycat.db.EnvironmentConfig; |
| 159 |
import com.sleepycat.db.Transaction; |
| 160 |
|
| 161 |
import com.sleepycat.persist.EntityStore; |
| 162 |
import com.sleepycat.persist.StoreConfig; |
| 163 |
|
| 164 |
import java.io.File; |
| 165 |
import java.io.FileNotFoundException; |
| 166 |
|
| 167 |
... |
| 168 |
|
| 169 |
Environment myEnv = null; |
| 170 |
EntityStore store = null; |
| 171 |
|
| 172 |
// Our convenience data accessor class, used for easy access to |
| 173 |
// EntityClass indexes. |
| 174 |
DataAccessor da; |
| 175 |
|
| 176 |
try { |
| 177 |
EnvironmentConfig myEnvConfig = new EnvironmentConfig(); |
| 178 |
myEnvConfig.setInitializeCache(true); |
| 179 |
myEnvConfig.setInitializeLocking(true); |
| 180 |
myEnvConfig.setInitializeLogging(true); |
| 181 |
myEnvConfig.setTransactional(true); |
| 182 |
|
| 183 |
StoreConfig storeConfig = new StoreConfig(); |
| 184 |
storeConfig.setTransactional(true); |
| 185 |
|
| 186 |
myEnv = new Environment(new File("/my/env/home"), |
| 187 |
myEnvConfig); |
| 188 |
|
| 189 |
EntityStore store = new EntityStore(myEnv, |
| 190 |
"EntityStore", storeConfig); |
| 191 |
|
| 192 |
da = new DataAccessor(store); |
| 193 |
|
| 194 |
// Assume that Inventory is an entity class. |
| 195 |
Inventory theInventory = new Inventory(); |
| 196 |
theInventory.setItemName("Waffles"); |
| 197 |
theInventory.setItemSku("waf23rbni"); |
| 198 |
|
| 199 |
Transaction txn = myEnv.beginTransaction(null, null); |
| 200 |
|
| 201 |
try { |
| 202 |
// Put the object to the store using the transaction handle. |
| 203 |
da.inventoryBySku.put(txn, theInventory); |
| 204 |
|
| 205 |
// Commit the transaction. The data is now safely written to the |
| 206 |
// store. |
| 207 |
txn.commit(); |
| 208 |
// If there is a problem, abort the transaction |
| 209 |
} catch (Exception e) { |
| 210 |
if (txn != null) { |
| 211 |
txn.abort(); |
| 212 |
txn = null; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
} catch (DatabaseException de) { |
| 218 |
// Exception handling goes here |
| 219 |
} catch (FileNotFoundException fnfe) { |
| 220 |
// Exception handling goes here |
| 221 |
}</pre> |
| 222 |
<p> |
| 223 |
The same thing can be done with the base API; the |
| 224 |
database in use is left unchanged if the write operation fails: |
| 225 |
</p> |
| 226 |
<pre class="programlisting">package db.txn; |
| 227 |
|
| 228 |
import com.sleepycat.db.Database; |
| 229 |
import com.sleepycat.db.DatabaseConfig; |
| 230 |
import com.sleepycat.db.DatabaseEntry; |
| 231 |
import com.sleepycat.db.DatabaseException; |
| 232 |
import com.sleepycat.db.Environment; |
| 233 |
import com.sleepycat.db.EnvironmentConfig; |
| 234 |
import com.sleepycat.db.Transaction; |
| 235 |
|
| 236 |
import java.io.File; |
| 237 |
import java.io.FileNotFoundException; |
| 238 |
|
| 239 |
... |
| 240 |
|
| 241 |
Database myDatabase = null; |
| 242 |
Environment myEnv = null; |
| 243 |
try { |
| 244 |
EnvironmentConfig myEnvConfig = new EnvironmentConfig(); |
| 245 |
myEnvConfig.setInitializeCache(true); |
| 246 |
myEnvConfig.setInitializeLocking(true); |
| 247 |
myEnvConfig.setInitializeLogging(true); |
| 248 |
myEnvConfig.setTransactional(true); |
| 249 |
|
| 250 |
myEnv = new Environment(new File("/my/env/home"), |
| 251 |
myEnvConfig); |
| 252 |
|
| 253 |
// Open the database. |
| 254 |
DatabaseConfig dbConfig = new DatabaseConfig(); |
| 255 |
dbConfig.setTransactional(true); |
| 256 |
dbConfig.setType(DatabaseType.BTREE); |
| 257 |
myDatabase = myEnv.openDatabase(null, // txn handle |
| 258 |
"sampleDatabase", // db file name |
| 259 |
null, // db name |
| 260 |
dbConfig); |
| 261 |
String keyString = "thekey"; |
| 262 |
String dataString = "thedata"; |
| 263 |
DatabaseEntry key = |
| 264 |
new DatabaseEntry(keyString.getBytes("UTF-8")); |
| 265 |
DatabaseEntry data = |
| 266 |
new DatabaseEntry(dataString.getBytes("UTF-8")); |
| 267 |
|
| 268 |
Transaction txn = myEnv.beginTransaction(null, null); |
| 269 |
|
| 270 |
try { |
| 271 |
myDatabase.put(txn, key, data); |
| 272 |
txn.commit(); |
| 273 |
} catch (Exception e) { |
| 274 |
if (txn != null) { |
| 275 |
txn.abort(); |
| 276 |
txn = null; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
} catch (DatabaseException de) { |
| 281 |
// Exception handling goes here |
| 282 |
} catch (FileNotFoundException fnfe) { |
| 283 |
// Exception handling goes here |
| 284 |
} </pre> |
| 285 |
<div class="sect1" lang="en" xml:lang="en"> |
| 286 |
<div class="titlepage"> |
| 287 |
<div> |
| 288 |
<div> |
| 289 |
<h2 class="title" style="clear: both"><a id="commitresults"></a>Committing a Transaction</h2> |
| 290 |
</div> |
| 291 |
</div> |
| 292 |
</div> |
| 293 |
<p> |
| 294 |
In order to fully understand what is happening when you commit |
| 295 |
a transaction, you must first understand a little about what |
| 296 |
DB is doing with |
| 297 |
|
| 298 |
<span> |
| 299 |
the logging subsystem. |
| 300 |
</span> |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
Logging causes all database <span>or |
| 305 |
store</span> write operations to be identified in |
| 306 |
|
| 307 |
<span>logs, and by default these |
| 308 |
logs are backed by files on disk. These logs are used to restore your databases |
| 309 |
<span>or store</span> |
| 310 |
</span> |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
in the event of a system or application failure, so by performing |
| 315 |
logging, DB ensures the integrity of your data. |
| 316 |
</p> |
| 317 |
<p> |
| 318 |
Moreover, DB performs <span class="emphasis"><em>write-ahead</em></span> |
| 319 |
logging. This means that information is written to the logs |
| 320 |
<span class="emphasis"><em>before</em></span> the actual database |
| 321 |
<span> |
| 322 |
or store |
| 323 |
</span> |
| 324 |
is changed. |
| 325 |
This means that all write activity performed under the |
| 326 |
protection of the transaction is noted in the log before |
| 327 |
the transaction is committed. Be aware, however, that database |
| 328 |
maintains logs in-memory. If you are backing your logs on |
| 329 |
disk, the log information will eventually be written to the log |
| 330 |
files, but while the transaction is on-going the log data may be |
| 331 |
held only in memory. |
| 332 |
</p> |
| 333 |
<p> |
| 334 |
When you commit a transaction, the following occurs: |
| 335 |
</p> |
| 336 |
<div class="itemizedlist"> |
| 337 |
<ul type="disc"> |
| 338 |
<li> |
| 339 |
<p> |
| 340 |
A commit record is written to the log. This |
| 341 |
indicates that the modifications made by the |
| 342 |
transaction are now permanent. By default, this write is performed synchronously to disk so the |
| 343 |
commit record arrives in the log files before any other actions are taken. |
| 344 |
</p> |
| 345 |
</li> |
| 346 |
<li> |
| 347 |
<p> |
| 348 |
Any log information held in memory is (by default) |
| 349 |
synchronously written to disk. Note that this requirement can be |
| 350 |
relaxed, depending on the type of commit you perform. |
| 351 |
See <a class="xref" href="nodurabletxn.html" title="Non-Durable Transactions">Non-Durable Transactions</a> for |
| 352 |
more information. |
| 353 |
<span>Also, if you are |
| 354 |
maintaining your logs entirely in-memory, then this |
| 355 |
step will of course not be taken. To configure your |
| 356 |
logging system for in-memory usage, see |
| 357 |
<a class="xref" href="logconfig.html#inmemorylogging" title="Configuring In-Memory Logging">Configuring In-Memory Logging</a>. |
| 358 |
</span> |
| 359 |
</p> |
| 360 |
</li> |
| 361 |
<li> |
| 362 |
<p> |
| 363 |
All locks held by the transaction are released. This means |
| 364 |
that read operations performed by other transactions or |
| 365 |
threads of control can now see the modifications without |
| 366 |
resorting to uncommitted reads (see <a class="xref" href="isolation.html#dirtyreads" title="Reading Uncommitted Data">Reading Uncommitted Data</a> for more information). |
| 367 |
</p> |
| 368 |
</li> |
| 369 |
</ul> |
| 370 |
</div> |
| 371 |
<p> |
| 372 |
To commit a transaction, you simply call |
| 373 |
|
| 374 |
|
| 375 |
<span><code class="methodname">Transaction.commit()</code>.</span> |
| 376 |
|
| 377 |
|
| 378 |
</p> |
| 379 |
<p> |
| 380 |
Notice that committing a transaction does not necessarily cause data |
| 381 |
modified in your memory cache to be written to the files |
| 382 |
backing your databases on disk. Dirtied database pages are written |
| 383 |
for a number of reasons, but a transactional |
| 384 |
commit is not one of them. The following are the things that can cause a dirtied |
| 385 |
database page to be written to the backing database file: |
| 386 |
</p> |
| 387 |
<div class="itemizedlist"> |
| 388 |
<ul type="disc"> |
| 389 |
<li> |
| 390 |
<p> |
| 391 |
Checkpoints. |
| 392 |
</p> |
| 393 |
<p> |
| 394 |
Checkpoints cause all dirtied pages currently existing |
| 395 |
in the cache to be written to disk, and a checkpoint |
| 396 |
record is then written to the logs. You can run checkpoints |
| 397 |
explicitly. For more information on checkpoints, |
| 398 |
see <a class="xref" href="filemanagement.html#checkpoints" title="Checkpoints">Checkpoints</a>. |
| 399 |
</p> |
| 400 |
</li> |
| 401 |
<li> |
| 402 |
<p> |
| 403 |
Cache is full. |
| 404 |
</p> |
| 405 |
<p> |
| 406 |
If the in-memory cache fills up, then dirtied pages |
| 407 |
might be written to disk in order to free up space for other |
| 408 |
pages that your application needs to use. Note that if |
| 409 |
dirtied pages are written to the database files, then |
| 410 |
any log records that describe how those pages were |
| 411 |
dirtied are written to disk before the database |
| 412 |
pages are written. |
| 413 |
</p> |
| 414 |
</li> |
| 415 |
</ul> |
| 416 |
</div> |
| 417 |
<p> |
| 418 |
Be aware that because your transaction commit caused database |
| 419 |
<span> |
| 420 |
or store |
| 421 |
</span> |
| 422 |
modifications recorded in your logs to be forced to disk, your modifications |
| 423 |
are by default "persistent" in that they can be recovered in the event of |
| 424 |
an application or system failure. However, recovery time is |
| 425 |
gated by how much data has been modified since the last |
| 426 |
checkpoint, so for applications that perform a lot of writes, |
| 427 |
you may want to run a checkpoint with some frequency. |
| 428 |
</p> |
| 429 |
<p> |
| 430 |
Note that once you have committed a transaction, the transaction |
| 431 |
handle that you used for the transaction is no longer valid. To |
| 432 |
perform database activities under the control of a new |
| 433 |
transaction, you must obtain a fresh transaction handle. |
| 434 |
</p> |
| 435 |
</div> |
| 436 |
</div> |
| 437 |
<div class="navfooter"> |
| 438 |
<hr /> |
| 439 |
<table width="100%" summary="Navigation footer"> |
| 440 |
<tr> |
| 441 |
<td width="40%" align="left"><a accesskey="p" href="envopen.html">Prev</a> </td> |
| 442 |
<td width="20%" align="center"> </td> |
| 443 |
<td width="40%" align="right"> <a accesskey="n" href="nodurabletxn.html">Next</a></td> |
| 444 |
</tr> |
| 445 |
<tr> |
| 446 |
<td width="40%" align="left" valign="top">Opening a Transactional Environment and |
| 447 |
|
| 448 |
<span>Store or Database</span> |
| 449 |
|
| 450 |
</td> |
| 451 |
<td width="20%" align="center"> |
| 452 |
<a accesskey="h" href="index.html">Home</a> |
| 453 |
</td> |
| 454 |
<td width="40%" align="right" valign="top"> Non-Durable Transactions</td> |
| 455 |
</tr> |
| 456 |
</table> |
| 457 |
</div> |
| 458 |
</body> |
| 459 |
</html> |