mirror of
https://github.com/berkeleydb/je.git
synced 2024-11-15 01:46:24 +00:00
226 lines
8.7 KiB
HTML
226 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>Transactional Cursors and Concurrent Applications</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="txnconcurrency.html" title="Chapter 4. Concurrency" />
|
||
<link rel="prev" href="isolation.html" title="Isolation" />
|
||
<link rel="next" href="readmodifywrite.html" title="Read/Modify/Write" />
|
||
</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">Transactional Cursors and Concurrent Applications</th>
|
||
</tr>
|
||
<tr>
|
||
<td width="20%" align="left"><a accesskey="p" href="isolation.html">Prev</a> </td>
|
||
<th width="60%" align="center">Chapter 4. Concurrency</th>
|
||
<td width="20%" align="right"> <a accesskey="n" href="readmodifywrite.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="txn_ccursor"></a>Transactional Cursors and Concurrent Applications</h2>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="toc">
|
||
<dl>
|
||
<dt>
|
||
<span class="sect2">
|
||
<a href="txn_ccursor.html#cursordirtyreads">Using Cursors with Uncommitted Data</a>
|
||
</span>
|
||
</dt>
|
||
</dl>
|
||
</div>
|
||
<p>
|
||
When you use transactional cursors with a concurrent application, remember that
|
||
in the event of a deadlock you must make sure that you close your cursor before you abort and retry your
|
||
transaction. <span>This is true of both
|
||
base API and DPL cursors.</span>
|
||
</p>
|
||
<p>
|
||
Also, remember that when you are using the default isolation level,
|
||
every time your cursor reads a record it locks
|
||
that record until the encompassing transaction is resolved. This
|
||
means that walking your database with a transactional cursor
|
||
increases the chance of lock contention.
|
||
</p>
|
||
<p>
|
||
For this reason, if you must routinely walk your database with a
|
||
transactional cursor, consider using a reduced isolation level
|
||
such as read committed. <span>This is
|
||
true of both base API and DPL cursors.</span>
|
||
</p>
|
||
<div class="sect2" lang="en" xml:lang="en">
|
||
<div class="titlepage">
|
||
<div>
|
||
<div>
|
||
<h3 class="title"><a id="cursordirtyreads"></a>Using Cursors with Uncommitted Data</h3>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p>
|
||
|
||
As described in <a class="xref" href="isolation.html#dirtyreads" title="Reading Uncommitted Data">Reading Uncommitted Data</a>
|
||
above, it is possible to relax your transaction's isolation
|
||
level such that it can read data modified but not yet committed
|
||
by another transaction. You can configure this when you create
|
||
your transaction handle, and when you do so then all cursors opened
|
||
inside that transaction will automatically use uncommitted reads.
|
||
</p>
|
||
<p>
|
||
You can also do this when you create a cursor handle from within
|
||
a serializable transaction. When you do this, only those
|
||
cursors configured for uncommitted reads uses uncommitted reads.
|
||
</p>
|
||
<p>
|
||
The following example shows how to configure an individual cursor handle
|
||
to read uncommitted data from within a serializable (full isolation) transaction.
|
||
For an example of
|
||
configuring a transaction to perform uncommitted reads in
|
||
general, see <a class="xref" href="isolation.html#dirtyreads" title="Reading Uncommitted Data">Reading Uncommitted Data</a>.
|
||
</p>
|
||
<pre class="programlisting">package je.txn;
|
||
|
||
import com.sleepycat.je.Cursor;
|
||
import com.sleepycat.je.CursorConfig;
|
||
import com.sleepycat.je.Database;
|
||
import com.sleepycat.je.DatabaseConfig;
|
||
import com.sleepycat.je.Environment;
|
||
import com.sleepycat.je.EnvironmentConfig;
|
||
|
||
import java.io.File;
|
||
|
||
...
|
||
|
||
Database myDatabase = null;
|
||
Environment myEnv = null;
|
||
try {
|
||
|
||
EnvironmentConfig myEnvConfig = new EnvironmentConfig();
|
||
myEnvConfig.setTransactional(true);
|
||
|
||
myEnv = new Environment(new File("/my/env/home"),
|
||
myEnvConfig);
|
||
|
||
// Open the database.
|
||
DatabaseConfig dbConfig = new DatabaseConfig();
|
||
dbConfig.setTransactional(true);
|
||
myDatabase = myEnv.openDatabase(null, // txn handle
|
||
"sampleDatabase", // db file name
|
||
dbConfig);
|
||
|
||
// Open the transaction. Note that this is a repeatable
|
||
// read transaction.
|
||
Transaction txn = myEnv.beginTransaction(null, null);
|
||
Cursor cursor = null;
|
||
try {
|
||
// Use the transaction handle here
|
||
// Get our cursor. Note that we pass the transaction
|
||
// handle here. Note also that we cause the cursor
|
||
// to perform uncommitted reads.
|
||
CursorConfig cconfig = new CursorConfig();
|
||
cconfig.setReadUncommitted(true);
|
||
cursor = db.openCursor(txn, cconfig);
|
||
|
||
// From here, you perform your cursor reads and writes
|
||
// as normal, committing and aborting the transactions as
|
||
// is necessary, and testing for deadlock exceptions as
|
||
// normal (omitted for brevity).
|
||
|
||
... </pre>
|
||
<p>
|
||
If you are using the DPL:
|
||
</p>
|
||
<pre class="programlisting">package persist.txn;
|
||
|
||
import com.sleepycat.je.CursorConfig;
|
||
import com.sleepycat.je.Environment;
|
||
import com.sleepycat.je.EnvironmentConfig;
|
||
|
||
import com.sleepycat.persist.EntityCursor;
|
||
import com.sleepycat.persist.EntityStore;
|
||
import com.sleepycat.persist.PrimaryIndex;
|
||
import com.sleepycat.persist.StoreConfig;
|
||
|
||
import java.util.Iterator;
|
||
|
||
import java.io.File;
|
||
|
||
...
|
||
|
||
EntityStore myStore = null;
|
||
Environment myEnv = null;
|
||
PrimaryIndex<String,AnEntityClass> pKey;
|
||
try {
|
||
|
||
EnvironmentConfig myEnvConfig = new EnvironmentConfig();
|
||
myEnvConfig.setTransactional(true);
|
||
|
||
myEnv = new Environment(new File("/my/env/home"),
|
||
myEnvConfig);
|
||
|
||
// Set up the entity store
|
||
StoreConfig myStoreConfig = new StoreConfig();
|
||
myStoreConfig.setAllowCreate(true);
|
||
myStoreConfig.setTransactional(true);
|
||
|
||
// Instantiate the store
|
||
myStore = new EntityStore(myEnv, storeName, myStoreConfig);
|
||
|
||
// Open the transaction. Note that this is a repeatable
|
||
// read transaction.
|
||
Transaction txn = myEnv.beginTransaction(null, null);
|
||
|
||
//Configure our cursor for uncommitted reads.
|
||
CursorConfig cconfig = new CursorConfig();
|
||
cconfig.setReadUncommitted(true);
|
||
|
||
// Get our cursor. Note that we pass the transaction
|
||
// handle here. Note also that we cause the cursor
|
||
// to perform uncommitted reads.
|
||
EntityCursor<AnEntityClass> cursor = pKey.entities(txn, cconfig);
|
||
|
||
try {
|
||
// From here, you perform your cursor reads and writes
|
||
// as normal, committing and aborting the transactions as
|
||
// is necessary, and testing for deadlock exceptions as
|
||
// normal (omitted for brevity).
|
||
|
||
... </pre>
|
||
</div>
|
||
</div>
|
||
<div class="navfooter">
|
||
<hr />
|
||
<table width="100%" summary="Navigation footer">
|
||
<tr>
|
||
<td width="40%" align="left"><a accesskey="p" href="isolation.html">Prev</a> </td>
|
||
<td width="20%" align="center">
|
||
<a accesskey="u" href="txnconcurrency.html">Up</a>
|
||
</td>
|
||
<td width="40%" align="right"> <a accesskey="n" href="readmodifywrite.html">Next</a></td>
|
||
</tr>
|
||
<tr>
|
||
<td width="40%" align="left" valign="top">Isolation </td>
|
||
<td width="20%" align="center">
|
||
<a accesskey="h" href="index.html">Home</a>
|
||
</td>
|
||
<td width="40%" align="right" valign="top"> Read/Modify/Write</td>
|
||
</tr>
|
||
</table>
|
||
</div>
|
||
</body>
|
||
</html>
|