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