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