libdb/docs/gsg_txn/JAVA/envopen.html
2011-09-13 13:44:24 -04:00

206 lines
7.4 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Opening a Transactional Environment and Store or Database</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
<link rel="start" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
<link rel="up" href="enabletxn.html" title="Chapter 2. Enabling Transactions" />
<link rel="prev" href="enabletxn.html" title="Chapter 2. Enabling Transactions" />
<link rel="next" href="usingtxns.html" title="Chapter 3. Transaction Basics" />
</head>
<body>
<div xmlns="" class="navheader">
<div class="libver">
<p>Library Version 11.2.5.2</p>
</div>
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Opening a Transactional Environment and
<span xmlns="http://www.w3.org/1999/xhtml">Store or Database</span>
</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="enabletxn.html">Prev</a> </td>
<th width="60%" align="center">Chapter 2. Enabling Transactions</th>
<td width="20%" align="right"> <a accesskey="n" href="usingtxns.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="envopen"></a>Opening a Transactional Environment and
<span>Store or Database</span>
</h2>
</div>
</div>
</div>
<p>
To enable transactions for your environment, you must initialize the
transactional subsystem. Note that doing this also initializes the
logging subsystem. In addition, you must initialize the memory pool
(in-memory cache). You must also initialize the locking subsystem.
<span>
For example, to do this with the DPL:
</span>
</p>
<pre class="programlisting">package persist.txn;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.StoreConfig;
import java.io.File;
import java.io.FileNotFoundException;
...
Environment myEnv = null;
EntityStore myStore = null;
try {
EnvironmentConfig myEnvConfig = new EnvironmentConfig();
StoreConfig storeConfig = new StoreConfig();
myEnvConfig.setInitializeCache(true);
myEnvConfig.setInitializeLocking(true);
myEnvConfig.setInitializeLogging(true);
myEnvConfig.setTransactional(true);
storeConfig.setTransactional(true);
myEnv = new Environment(new File("/my/env/home"),
myEnvConfig);
myStore = new EntityStore(myEnv, "EntityStore", storeConfig);
} catch (DatabaseException de) {
// Exception handling goes here
} catch (FileNotFoundException fnfe) {
// Exception handling goes here
}</pre>
<p>
And when using the base API:
</p>
<pre class="programlisting">package db.txn;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import java.io.File;
import java.io.FileNotFoundException;
...
Environment myEnv = null;
try {
EnvironmentConfig myEnvConfig = new EnvironmentConfig();
myEnvConfig.setInitializeCache(true);
myEnvConfig.setInitializeLocking(true);
myEnvConfig.setInitializeLogging(true);
myEnvConfig.setTransactional(true);
myEnv = new Environment(new File("/my/env/home"),
myEnvConfig);
} catch (DatabaseException de) {
// Exception handling goes here
} catch (FileNotFoundException fnfe) {
// Exception handling goes here
}</pre>
<p>
You then can use the <code class="classname">Environment</code> handle to open
your database(s) using <code class="methodname">Environment.openDatabase()</code>.
Note that when you do this, you must set
<code class="methodname">DatabaseConfig.setTransactional()</code>
to <code class="literal">true</code>. Note that in effect this causes the
database open to be transactional protected because it results in
auto commit being used for the open (if a transaction is not explicitly
used to protect the open).
For example:
</p>
<pre class="programlisting">package db.txn;
<strong class="userinput"><code>import com.sleepycat.db.Database;
import com.sleepycat.db.DatabaseType;
import com.sleepycat.db.DatabaseConfig;</code></strong>
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import java.io.File;
import java.io.FileNotFoundException;
...
<strong class="userinput"><code>Database myDatabase = null;</code></strong>
Environment myEnv = null;
try {
EnvironmentConfig myEnvConfig = new EnvironmentConfig();
myEnvConfig.setInitializeCache(true);
myEnvConfig.setInitializeLocking(true);
myEnvConfig.setInitializeLogging(true);
myEnvConfig.setTransactional(true);
myEnv = new Environment(new File("/my/env/home"),
myEnvConfig);
<strong class="userinput"><code>// Open the database.
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
dbConfig.setType(DatabaseType.BTREE);
myDatabase = myEnv.openDatabase(null, // txn handle
"sampleDatabase", // db file name
null, // db name
dbConfig);</code></strong>
} catch (DatabaseException de) {
// Exception handling goes here
} catch (FileNotFoundException fnfe) {
// Exception handling goes here
}</pre>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
Never close a database <span> or
store </span> that has active transactions. Make sure
all transactions are resolved (either committed or aborted)
before closing the database.
</p>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="enabletxn.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="enabletxn.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="usingtxns.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 2. Enabling Transactions </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Chapter 3. Transaction Basics</td>
</tr>
</table>
</div>
</body>
</html>