| 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 |
<span> |
| 43 |
or store |
| 44 |
</span> |
| 45 |
operations, it is sometimes necessary to perform |
| 46 |
a single database |
| 47 |
<span> |
| 48 |
or store |
| 49 |
</span> |
| 50 |
operation under the control of a transaction. |
| 51 |
Rather than force you to obtain a transaction, perform the single |
| 52 |
write operation, and then either commit or abort the transaction, |
| 53 |
you can automatically group this sequence of events using |
| 54 |
<span class="emphasis"><em>auto commit</em></span>. |
| 55 |
</p> |
| 56 |
<p> |
| 57 |
To use auto commit: |
| 58 |
</p> |
| 59 |
<div class="orderedlist"> |
| 60 |
<ol type="1"> |
| 61 |
<li> |
| 62 |
<p> |
| 63 |
Open your environment and your databases |
| 64 |
<span> |
| 65 |
or store |
| 66 |
</span> |
| 67 |
so that they support |
| 68 |
transactions. See <a class="xref" href="enabletxn.html" title="Chapter 2. Enabling Transactions">Enabling Transactions</a> |
| 69 |
for details. |
| 70 |
</p> |
| 71 |
</li> |
| 72 |
<li> |
| 73 |
<p> |
| 74 |
Do not provide a transactional handle to the method that is |
| 75 |
performing the database |
| 76 |
<span> |
| 77 |
or store |
| 78 |
</span> |
| 79 |
write operation. |
| 80 |
</p> |
| 81 |
</li> |
| 82 |
</ol> |
| 83 |
</div> |
| 84 |
<p> |
| 85 |
Note that auto commit is not available for cursors. You must always |
| 86 |
open your cursor using a transaction if you want the cursor's |
| 87 |
operations to be transactional protected. See |
| 88 |
<a class="xref" href="txncursor.html" title="Transactional Cursors">Transactional Cursors</a> for details on using |
| 89 |
transactional cursors. |
| 90 |
</p> |
| 91 |
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 92 |
<h3 class="title">Note</h3> |
| 93 |
<p> |
| 94 |
Never have more than one active transaction in your thread |
| 95 |
at a time. This is especially a problem if you mix an |
| 96 |
explicit transaction with another operation that uses auto |
| 97 |
commit. Doing so can result in undetectable deadlocks. |
| 98 |
</p> |
| 99 |
</div> |
| 100 |
<p> |
| 101 |
For example, the following uses auto commit to perform the database write operation: |
| 102 |
</p> |
| 103 |
<pre class="programlisting">package db.txn; |
| 104 |
|
| 105 |
import com.sleepycat.db.Database; |
| 106 |
import com.sleepycat.db.DatabaseConfig; |
| 107 |
import com.sleepycat.db.DatabaseEntry; |
| 108 |
import com.sleepycat.db.DatabaseException; |
| 109 |
import com.sleepycat.db.Environment; |
| 110 |
import com.sleepycat.db.EnvironmentConfig; |
| 111 |
|
| 112 |
import java.io.File; |
| 113 |
|
| 114 |
... |
| 115 |
|
| 116 |
Database myDatabase = null; |
| 117 |
Environment myEnv = null; |
| 118 |
try { |
| 119 |
EnvironmentConfig myEnvConfig = new EnvironmentConfig(); |
| 120 |
myEnvConfig.setInitializeCache(true); |
| 121 |
myEnvConfig.setInitializeLocking(true); |
| 122 |
myEnvConfig.setInitializeLogging(true); |
| 123 |
myEnvConfig.setTransactional(true); |
| 124 |
|
| 125 |
myEnv = new Environment(new File("/my/env/home"), |
| 126 |
myEnvConfig); |
| 127 |
|
| 128 |
// Open the database. |
| 129 |
DatabaseConfig dbConfig = new DatabaseConfig(); |
| 130 |
dbConfig.setTransactional(true); |
| 131 |
dbConfig.setType(DatabaseType.BTREE); |
| 132 |
myDatabase = myEnv.openDatabase(null, // txn handle |
| 133 |
"sampleDatabase", // db file name |
| 134 |
null, // db name |
| 135 |
dbConfig); |
| 136 |
String keyString = "thekey"; |
| 137 |
String dataString = "thedata"; |
| 138 |
DatabaseEntry key = |
| 139 |
new DatabaseEntry(keyString.getBytes("UTF-8")); |
| 140 |
DatabaseEntry data = |
| 141 |
new DatabaseEntry(dataString.getBytes("UTF-8")); |
| 142 |
|
| 143 |
// Perform the write. Because the database was opened to |
| 144 |
// support transactions, this write is performed using auto commit. |
| 145 |
myDatabase.put(null, key, data); |
| 146 |
|
| 147 |
} catch (DatabaseException de) { |
| 148 |
// Exception handling goes here |
| 149 |
} </pre> |
| 150 |
</div> |
| 151 |
<div class="navfooter"> |
| 152 |
<hr /> |
| 153 |
<table width="100%" summary="Navigation footer"> |
| 154 |
<tr> |
| 155 |
<td width="40%" align="left"><a accesskey="p" href="abortresults.html">Prev</a> </td> |
| 156 |
<td width="20%" align="center"> |
| 157 |
<a accesskey="u" href="usingtxns.html">Up</a> |
| 158 |
</td> |
| 159 |
<td width="40%" align="right"> <a accesskey="n" href="nestedtxn.html">Next</a></td> |
| 160 |
</tr> |
| 161 |
<tr> |
| 162 |
<td width="40%" align="left" valign="top">Aborting a Transaction </td> |
| 163 |
<td width="20%" align="center"> |
| 164 |
<a accesskey="h" href="index.html">Home</a> |
| 165 |
</td> |
| 166 |
<td width="40%" align="right" valign="top"> Nested Transactions</td> |
| 167 |
</tr> |
| 168 |
</table> |
| 169 |
</div> |
| 170 |
</body> |
| 171 |
</html> |