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