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