mirror of
https://github.com/berkeleydb/je.git
synced 2024-11-15 01:46:24 +00:00
261 lines
8.7 KiB
HTML
261 lines
8.7 KiB
HTML
|
<?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>Chapter 2. Enabling Transactions</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, Java Edition Transaction Processing" />
|
|||
|
<link rel="up" href="index.html" title="Getting Started with Berkeley DB, Java Edition Transaction Processing" />
|
|||
|
<link rel="prev" href="perftune-intro.html" title="Performance Tuning" />
|
|||
|
<link rel="next" href="usingtxns.html" title="Chapter 3. Transaction Basics" />
|
|||
|
</head>
|
|||
|
<body>
|
|||
|
<div xmlns="" class="navheader">
|
|||
|
<div class="libver">
|
|||
|
<p>Library Version 12.2.7.5</p>
|
|||
|
</div>
|
|||
|
<table width="100%" summary="Navigation header">
|
|||
|
<tr>
|
|||
|
<th colspan="3" align="center">Chapter 2. Enabling Transactions</th>
|
|||
|
</tr>
|
|||
|
<tr>
|
|||
|
<td width="20%" align="left"><a accesskey="p" href="perftune-intro.html">Prev</a> </td>
|
|||
|
<th width="60%" align="center"> </th>
|
|||
|
<td width="20%" align="right"> <a accesskey="n" href="usingtxns.html">Next</a></td>
|
|||
|
</tr>
|
|||
|
</table>
|
|||
|
<hr />
|
|||
|
</div>
|
|||
|
<div class="chapter" lang="en" xml:lang="en">
|
|||
|
<div class="titlepage">
|
|||
|
<div>
|
|||
|
<div>
|
|||
|
<h2 class="title"><a id="enabletxn"></a>Chapter 2. Enabling Transactions</h2>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="toc">
|
|||
|
<p>
|
|||
|
<b>Table of Contents</b>
|
|||
|
</p>
|
|||
|
<dl>
|
|||
|
<dt>
|
|||
|
<span class="sect1">
|
|||
|
<a href="enabletxn.html#envopen">Opening a Transactional Environment and
|
|||
|
|
|||
|
<span>Store or Database</span>
|
|||
|
|
|||
|
</a>
|
|||
|
</span>
|
|||
|
</dt>
|
|||
|
</dl>
|
|||
|
</div>
|
|||
|
<p>
|
|||
|
In order to use transactions with your application, you must turn them
|
|||
|
on. To do this you must:
|
|||
|
</p>
|
|||
|
<div class="itemizedlist">
|
|||
|
<ul type="disc">
|
|||
|
<li>
|
|||
|
<p>
|
|||
|
Turn on transactions for your environment.
|
|||
|
|
|||
|
<span>
|
|||
|
You do this by using the
|
|||
|
<code class="methodname">EnvironmentConfig.setTransactional()</code>
|
|||
|
method<span>, or by using the
|
|||
|
<code class="literal">je.env.isTransactional</code>
|
|||
|
<code class="filename">je.properties</code> parameter</span>.
|
|||
|
</span>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>
|
|||
|
If you are using the DPL, transaction-enable your stores.
|
|||
|
You do this by using the
|
|||
|
<code class="methodname">StoreConfig.setTransactional() method.</code>
|
|||
|
</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>
|
|||
|
<span>
|
|||
|
Transaction-enable your databases.
|
|||
|
</span>
|
|||
|
<span>
|
|||
|
If you are using the base API, transaction-enable your databases.
|
|||
|
</span>
|
|||
|
You do this by
|
|||
|
<span>
|
|||
|
using the
|
|||
|
<code class="methodname">DatabaseConfig.setTransactional()</code>
|
|||
|
method, and then opening the database from within a transaction.
|
|||
|
</span>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<span>
|
|||
|
Note that the common practice is for auto commit to be used to
|
|||
|
transaction-protect the database open. To use auto-commit, you
|
|||
|
must still enable transactions as described here, but you do
|
|||
|
not have to explicitly use a transaction when you open your
|
|||
|
database. An example of this is given in the next section.
|
|||
|
</span>
|
|||
|
</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</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. For example, do this with the DPL:
|
|||
|
</p>
|
|||
|
<pre class="programlisting">package persist.txn;
|
|||
|
|
|||
|
import com.sleepycat.je.DatabaseException;
|
|||
|
import com.sleepycat.je.Environment;
|
|||
|
import com.sleepycat.je.EnvironmentConfig;
|
|||
|
|
|||
|
import com.sleepycat.persist.EntityStore;
|
|||
|
import com.sleepycat.persist.StoreConfig;
|
|||
|
|
|||
|
import java.io.File;
|
|||
|
|
|||
|
...
|
|||
|
|
|||
|
Environment myEnv = null;
|
|||
|
EntityStore myStore = null;
|
|||
|
try {
|
|||
|
EnvironmentConfig myEnvConfig = new EnvironmentConfig();
|
|||
|
StoreConfig storeConfig = new StoreConfig();
|
|||
|
|
|||
|
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
|
|||
|
}</pre>
|
|||
|
<p>
|
|||
|
And when using the base API:
|
|||
|
</p>
|
|||
|
<pre class="programlisting">package je.txn;
|
|||
|
|
|||
|
import com.sleepycat.je.DatabaseException;
|
|||
|
import com.sleepycat.je.Environment;
|
|||
|
import com.sleepycat.je.EnvironmentConfig;
|
|||
|
|
|||
|
import java.io.File;
|
|||
|
|
|||
|
...
|
|||
|
|
|||
|
Environment myEnv = null;
|
|||
|
try {
|
|||
|
EnvironmentConfig myEnvConfig = new EnvironmentConfig();
|
|||
|
myEnvConfig.setTransactional(true);
|
|||
|
|
|||
|
myEnv = new Environment(new File("/my/env/home"),
|
|||
|
myEnvConfig);
|
|||
|
|
|||
|
} catch (DatabaseException de) {
|
|||
|
// Exception handling goes here
|
|||
|
}</pre>
|
|||
|
<p>
|
|||
|
You then create and open your database(s) as you would for a non-transactional system.
|
|||
|
|
|||
|
|
|||
|
<span>
|
|||
|
The only difference is that you must set
|
|||
|
<code class="methodname">DatabaseConfig.setTransactional()</code>
|
|||
|
to <code class="literal">true</code>. Note that your database open must be
|
|||
|
transactional-protected. However, if you do not give the
|
|||
|
<code class="methodname">openDatabase()</code> method a transaction handle,
|
|||
|
then the open is automatically protected using auto commit.
|
|||
|
Typically auto commit is used for this purpose.
|
|||
|
For example:
|
|||
|
|
|||
|
</span>
|
|||
|
</p>
|
|||
|
<pre class="programlisting">package je.txn;
|
|||
|
|
|||
|
<strong class="userinput"><code>import com.sleepycat.je.Database;
|
|||
|
import com.sleepycat.je.DatabaseConfig;</code></strong>
|
|||
|
import com.sleepycat.je.DatabaseException;
|
|||
|
import com.sleepycat.je.Environment;
|
|||
|
import com.sleepycat.je.EnvironmentConfig;
|
|||
|
|
|||
|
import java.io.File;
|
|||
|
|
|||
|
...
|
|||
|
|
|||
|
<strong class="userinput"><code>Database myDatabase = null;</code></strong>
|
|||
|
Environment myEnv = null;
|
|||
|
try {
|
|||
|
EnvironmentConfig myEnvConfig = new EnvironmentConfig();
|
|||
|
myEnvConfig.setTransactional(true);
|
|||
|
myEnv = new Environment(new File("/my/env/home"),
|
|||
|
myEnvConfig);
|
|||
|
|
|||
|
<strong class="userinput"><code>// Open the database. Create it if it does not already exist.
|
|||
|
DatabaseConfig dbConfig = new DatabaseConfig();
|
|||
|
dbConfig.setTransactional(true);
|
|||
|
myDatabase = myEnv.openDatabase(null,
|
|||
|
"sampleDatabase",
|
|||
|
dbConfig);</code></strong>
|
|||
|
|
|||
|
} catch (DatabaseException de) {
|
|||
|
// 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>
|
|||
|
<div class="navfooter">
|
|||
|
<hr />
|
|||
|
<table width="100%" summary="Navigation footer">
|
|||
|
<tr>
|
|||
|
<td width="40%" align="left"><a accesskey="p" href="perftune-intro.html">Prev</a> </td>
|
|||
|
<td width="20%" align="center"> </td>
|
|||
|
<td width="40%" align="right"> <a accesskey="n" href="usingtxns.html">Next</a></td>
|
|||
|
</tr>
|
|||
|
<tr>
|
|||
|
<td width="40%" align="left" valign="top">Performance Tuning </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>
|