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>Opening a Transactional Environment and 7 8 Store or Database 9 10 </title> 11 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 12 <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> 13 <link rel="home" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" /> 14 <link rel="up" href="enabletxn.html" title="Chapter��2.��Enabling Transactions" /> 15 <link rel="previous" href="enabletxn.html" title="Chapter��2.��Enabling Transactions" /> 16 <link rel="next" href="usingtxns.html" title="Chapter��3.��Transaction Basics" /> 17 </head> 18 <body> 19 <div class="navheader"> 20 <table width="100%" summary="Navigation header"> 21 <tr> 22 <th colspan="3" align="center">Opening a Transactional Environment and 23 24 Store or Database 25 26 </th> 27 </tr> 28 <tr> 29 <td width="20%" align="left"><a accesskey="p" href="enabletxn.html">Prev</a>��</td> 30 <th width="60%" align="center">Chapter��2.��Enabling Transactions</th> 31 <td width="20%" align="right">��<a accesskey="n" href="usingtxns.html">Next</a></td> 32 </tr> 33 </table> 34 <hr /> 35 </div> 36 <div class="sect1" lang="en" xml:lang="en"> 37 <div class="titlepage"> 38 <div> 39 <div> 40 <h2 class="title" style="clear: both"><a id="envopen"></a>Opening a Transactional Environment and 41 42 <span>Store or Database</span> 43 44 </h2> 45 </div> 46 </div> 47 <div></div> 48 </div> 49 <p> 50 To enable transactions for your environment, you must initialize the 51 transactional subsystem. Note that doing this also initializes the 52 logging subsystem. In addition, you must initialize the memory pool 53 (in-memory cache). Frequently, but not always, you will also 54 initialize the locking subsystem. 55 56 57 58 <span> 59 For example, to do this with the DPL: 60 </span> 61 </p> 62 <pre class="programlisting">package persist.txn; 63 64import com.sleepycat.db.DatabaseException; 65import com.sleepycat.db.Environment; 66import com.sleepycat.db.EnvironmentConfig; 67 68import com.sleepycat.persist.EntityStore; 69import com.sleepycat.persist.StoreConfig; 70 71import java.io.File; 72import java.io.FileNotFoundException; 73 74... 75 76Environment myEnv = null; 77EntityStore myStore = null; 78try { 79 EnvironmentConfig myEnvConfig = new EnvironmentConfig(); 80 StoreConfig storeConfig = new StoreConfig(); 81 82 myEnvConfig.setInitializeCache(true); 83 myEnvConfig.setInitializeLocking(true); 84 myEnvConfig.setInitializeLogging(true); 85 myEnvConfig.setTransactional(true); 86 87 storeConfig.setTransactional(true); 88 89 myEnv = new Environment(new File("/my/env/home"), 90 myEnvConfig); 91 92 myStore = new EntityStore(myEnv, "EntityStore", storeConfig); 93 94} catch (DatabaseException de) { 95 // Exception handling goes here 96} catch (FileNotFoundException fnfe) { 97 // Exception handling goes here 98}</pre> 99 <p> 100 And when using the base API: 101 </p> 102 <pre class="programlisting">package db.txn; 103 104import com.sleepycat.db.DatabaseException; 105import com.sleepycat.db.Environment; 106import com.sleepycat.db.EnvironmentConfig; 107 108import java.io.File; 109import java.io.FileNotFoundException; 110 111... 112 113Environment myEnv = null; 114try { 115 EnvironmentConfig myEnvConfig = new EnvironmentConfig(); 116 myEnvConfig.setInitializeCache(true); 117 myEnvConfig.setInitializeLocking(true); 118 myEnvConfig.setInitializeLogging(true); 119 myEnvConfig.setTransactional(true); 120 121 myEnv = new Environment(new File("/my/env/home"), 122 myEnvConfig); 123 124} catch (DatabaseException de) { 125 // Exception handling goes here 126} catch (FileNotFoundException fnfe) { 127 // Exception handling goes here 128}</pre> 129 <p> 130 You then can use the <tt class="classname">Environment</tt> handle to open 131 your database(s) using <tt class="methodname">Environment.openDatabase()</tt>. 132 Note that when you do this, you must set 133 <tt class="methodname">DatabaseConfig.setTransactional()</tt> 134 to <tt class="literal">true</tt>. Note that in effect this causes the 135 database open to be transactional protected because it results in 136 auto commit being used for the open (if a transaction is not explicitly 137 used to protect the open). 138 For example: 139 </p> 140 <pre class="programlisting">package db.txn; 141 142<b class="userinput"><tt>import com.sleepycat.db.Database; 143import com.sleepycat.db.DatabaseType; 144import com.sleepycat.db.DatabaseConfig;</tt></b> 145import com.sleepycat.db.DatabaseException; 146import com.sleepycat.db.Environment; 147import com.sleepycat.db.EnvironmentConfig; 148 149import java.io.File; 150import java.io.FileNotFoundException; 151 152... 153 154<b class="userinput"><tt>Database myDatabase = null;</tt></b> 155Environment myEnv = null; 156try { 157 EnvironmentConfig myEnvConfig = new EnvironmentConfig(); 158 myEnvConfig.setInitializeCache(true); 159 myEnvConfig.setInitializeLocking(true); 160 myEnvConfig.setInitializeLogging(true); 161 myEnvConfig.setTransactional(true); 162 163 myEnv = new Environment(new File("/my/env/home"), 164 myEnvConfig); 165 166 <b class="userinput"><tt>// Open the database. 167 DatabaseConfig dbConfig = new DatabaseConfig(); 168 dbConfig.setTransactional(true); 169 dbConfig.setType(DatabaseType.BTREE); 170 myDatabase = myEnv.openDatabase(null, // txn handle 171 "sampleDatabase", // db file name 172 null, // db name 173 dbConfig);</tt></b> 174} catch (DatabaseException de) { 175 // Exception handling goes here 176} catch (FileNotFoundException fnfe) { 177 // Exception handling goes here 178}</pre> 179 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> 180 <h3 class="title">Note</h3> 181 <p> 182 Never close a database <span> or 183 store </span> that has active transactions. Make sure 184 all transactions are resolved (either committed or aborted) 185 before closing the database. 186 </p> 187 </div> 188 </div> 189 <div class="navfooter"> 190 <hr /> 191 <table width="100%" summary="Navigation footer"> 192 <tr> 193 <td width="40%" align="left"><a accesskey="p" href="enabletxn.html">Prev</a>��</td> 194 <td width="20%" align="center"> 195 <a accesskey="u" href="enabletxn.html">Up</a> 196 </td> 197 <td width="40%" align="right">��<a accesskey="n" href="usingtxns.html">Next</a></td> 198 </tr> 199 <tr> 200 <td width="40%" align="left" valign="top">Chapter��2.��Enabling Transactions��</td> 201 <td width="20%" align="center"> 202 <a accesskey="h" href="index.html">Home</a> 203 </td> 204 <td width="40%" align="right" valign="top">��Chapter��3.��Transaction Basics</td> 205 </tr> 206 </table> 207 </div> 208 </body> 209</html> 210