<!--$Id: cs_bdb.html,v 1.2 2004/03/30 01:22:44 jtownsen Exp $--> <!--Copyright 1997-2003 by Sleepycat Software, Inc.--> <!--All rights reserved.--> <!--See the file LICENSE for redistribution information.--> <html> <head> <title>Berkeley DB Reference Guide: Access Methods and Transaction Options</title> <meta name="description" content="Berkeley DB: An embedded database programmatic toolkit."> <meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++"> </head> <body bgcolor=white> <table width="100%"><tr valign=top> <td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Java API</dl></h3></td> <td align=right><a href="../bdb/cs_bdb_bind.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../bdb/cs_bdb_collection.html"><img src="../../images/next.gif" alt="Next"></a> </td></tr></table> <p> <h3 align=center>Access Methods and Transaction Options</h3> <p align=center><img src="bdb_fig_1.gif" alt="bdb_fig_1"> <h3>Using Java API</h3> <p>A <a href="../../java/com/sleepycat/db/DbEnv.html">DbEnv</a> manages the resources for one or more data stores.</p> <p>A <a href="../../java/com/sleepycat/db/Db.html">Db</a> objects represent a single database and are created with a reference to the environment. An access method must be chosen for each database.</p> <p> <a href="../../java/com/sleepycat/bdb/DataStore.html">DataStore</a> and <a href="../../java/com/sleepycat/bdb/DataIndex.html">DataIndex</a> objects are created to wrap the database objects for use as a primary store or index. Data stores and data indices are then used to create collection views, as described in the <a href="cs_bdb_collection.html">com.sleepycat.bdb.collection</a> package.</p> <h3>Using Transactions</h3> <p>Once you have an environment, one or more data stores and data indices, and one or more collection views, you are ready to access (read and write) stored data. For a transactional environment, a transaction must be started before accessing data, and must be committed or aborted after access is complete. Java API provides several ways of managing transactions.</p> <p>The recommended technique is to use the <a href="../../java/com/sleepycat/bdb/TransactionRunner.html">TransactionRunner</a> class along with your own implementation of the <a href="../../java/com/sleepycat/bdb/TransactionWorker.html">TransactionWorker</a> interface. <a href="../../java/com/sleepycat/bdb/TransactionRunner.html">TransactionRunner</a> will call your <a href="../../java/com/sleepycat/bdb/TransactionWorker.html">TransactionWorker</a> implementation class to perform the data access or work of the transaction. This technique has the following benefits:</p> <p><ul type=disc> <li>Transaction exceptions will be handled transparently and retries will be performed when deadlocks are detected. <li>The transaction will automatically be committed if your <a href="../../java/com/sleepycat/bdb/TransactionWorker.html#doWork()">TransactionWorker.doWork()</a> method returns normally, or will be aborted if doWork() throws an exception. <li>TransactionRunner can be used for non-transactional environments as well, allowing you to write your application independently of the environment. </ul> <p>If you don't want to use <a href="../../java/com/sleepycat/bdb/TransactionRunner.html">TransactionRunner</a> , the alternative is to use the class <a href="../../java/com/sleepycat/bdb/CurrentTransaction.html">CurrentTransaction</a> .</p> <ol> <p><li>Obtain a CurrentTransaction instance by calling the <a href="../../java/com/sleepycat/bdb/CurrentTransaction.html#getInstance(com.sleepycat.db.DbEnv)">CurrentTransaction.getInstance(com.sleepycat.db.DbEnv)</a> method. The instance returned can be used by all threads in a program. <p><li>Use <a href="../../java/com/sleepycat/bdb/CurrentTransaction.html#beginTxn()">CurrentTransaction.beginTxn()</a> , <a href="../../java/com/sleepycat/bdb/CurrentTransaction.html#commitTxn()">CurrentTransaction.commitTxn()</a> and <a href="../../java/com/sleepycat/bdb/CurrentTransaction.html#abortTxn()">CurrentTransaction.abortTxn()</a> to directly begin, commit and abort transactions. </ol> <p>If you choose to use CurrentTransaction directly you must handle the <a href="../../java/com/sleepycat/db/DbDeadlockException.html">DbDeadlockException</a> exception and perform retries yourself. Also note that CurrentTransaction may only be used in a transactional environment.</p> <p>The Java API supports nested transactions. If <a href="../../java/com/sleepycat/bdb/TransactionRunner.html#run(com.sleepycat.bdb.TransactionWorker)">TransactionRunner.run(com.sleepycat.bdb.TransactionWorker)</a> or <a href="../../java/com/sleepycat/bdb/CurrentTransaction.html#beginTxn()">CurrentTransaction.beginTxn()</a> is called while another transaction is active, a child transaction will be created. When <a href="../../java/com/sleepycat/bdb/TransactionRunner.html#run(com.sleepycat.bdb.TransactionWorker)">TransactionRunner.run(com.sleepycat.bdb.TransactionWorker)</a> returns, or when <a href="../../java/com/sleepycat/bdb/CurrentTransaction.html#commitTxn()">CurrentTransaction.commitTxn()</a> or <a href="../../java/com/sleepycat/bdb/CurrentTransaction.html#abortTxn()">CurrentTransaction.abortTxn()</a> is called, the parent transaction becomes active again. Note that because only one transaction is active per-thread, it is impossible to accidentally use a parent transaction while a child transaction is active.</p> <p>The Java API also supports transaction auto-commit and dirty-read via the <a href="../../java/com/sleepycat/bdb/collection/StoredCollections.html">StoredCollections</a> class.</p> <p>If auto-commit is enabled for a collection or for the entire <a href="../../java/com/sleepycat/db/DbEnv.html">DbEnv</a> , a transaction will be started and committed automatically for each write operation if no transaction is already active. See <a href="../../java/com/sleepycat/bdb/collection/StoredCollections.html">StoredCollections</a> for how to create an auto-commit collection and <a href="../../java/com/sleepycat/db/DbEnv.html#setFlags">DbEnv.setFlags</a> for how to set auto-commit for an entire environment.</p> <p>When dirty-read is enabled for a collection, data will be read that has been modified by another transaction but not committed. Using dirty-read can improve concurrency since reading will not wait for other transactions to complete. For a non-transactional container, dirty-read has no effect. See <a href="../../java/com/sleepycat/bdb/collection/StoredCollections.html">StoredCollections</a> for how to create a dirty-read collection.</p> <h3>Transaction Rollback</h3> <p>When a transaction is aborted (or rolled back) the application is responsible for discarding references to any data objects that were modified during the transaction. Since Java API treats data by value, not by reference, neither the data objects nor Java API objects contain status information indicating whether the data objects are 1- in sync with the database, 2- dirty (contain changes that have not been written to the database), 3- stale (were read previously but have become out of sync with changes made to the database), or 4- contain changes that cannot be committed because of an aborted transaction.</p> <p>For example, a given data object will reflect the current state of the database after reading it within a transaction. If the object is then modified it will be out of sync with the database. When the modified object is written to the database it will then be in sync again. But if the transaction is aborted the object will then be out of sync with the database. References to such objects should no longer be used. When these objects are needed later they should be read fresh from the database.</p> <p>When an existing stored object is to be updated, special care should be taken to read the data, then modify it, and then write it to the database, all within a single transaction. If a stale data object (an object that was read previously but has since been changed in the database) is modified and then written to the database, database changes may be overwritten unintentionally.</p> <p>When an application enforces rules about concurrent access to specific data objects or all data objects, the rules described here can be relaxed. For example, if the application knows that a certain object is only modified in one place, it may be able to reliably keep a current copy of that object. In that case, it is not necessary to reread the object before updating it. That said, if arbitrary concurrent access is to be supported, the safest approach is to always read data before modifying it within a single transaction.</p> <p>Similar concerns apply to using data that may have become stale. If the application depends on current data, it should be read fresh from the database just before it is used.</p> <h3>Selecting Access Methods</h3> <p>For each data store and secondary index you must choose from one of the access methods in the table below.</p> <p>The access method determines not only whether sorted keys or duplicate keys are supported, but also what types of collection views may be used and what restrictions are imposed on the collection views.</p> <table border=1 align=center> <tr><th>Access Method</th><th>Ordered</th><th>Duplicates</th><th>Record Numbers</th><th>Berkeley DB Type</th><th>Berkeley DB Flags</th></tr> <tr><td>BTREE-UNIQUE</td><td>Yes</td><td>No</td><td>No</td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_BTREE">Db.DB_BTREE</a> </td><td>None</td></tr> <tr><td>BTREE-DUP</td><td>Yes</td><td>Yes, Unsorted</td><td>No</td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_BTREE">Db.DB_BTREE</a> </td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_DUP">Db.DB_DUP</a> </td></tr> <tr><td>BTREE-DUPSORT</td><td>Yes</td><td>Yes, Sorted</td><td>No</td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_BTREE">Db.DB_BTREE</a> </td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_DUPSORT">Db.DB_DUPSORT</a> </td></tr> <tr><td>BTREE-RECNUM</td><td>Yes</td><td>No</td><td>Yes, Renumbered</td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_BTREE">Db.DB_BTREE</a> </td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_RECNUM">Db.DB_RECNUM</a> </td></tr> <tr><td>HASH-UNIQUE</td><td>No</td><td>No</td><td>No</td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_HASH">Db.DB_HASH</a> </td><td>None</td></tr> <tr><td>HASH-DUP</td><td>No</td><td>Yes, Unsorted</td><td>No</td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_HASH">Db.DB_HASH</a> </td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_DUP">Db.DB_DUP</a> </td></tr> <tr><td>HASH-DUPSORT</td><td>No</td><td>Yes, Sorted</td><td>No</td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_HASH">Db.DB_HASH</a> </td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_DUPSORT">Db.DB_DUPSORT</a> </td></tr> <tr><td>QUEUE</td><td>Yes</td><td>No</td><td>Yes, Fixed</td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_QUEUE">Db.DB_QUEUE</a> </td><td>None</td></tr> <tr><td>RECNO</td><td>Yes</td><td>No</td><td>Yes, Fixed</td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_RECNO">Db.DB_RECNO</a> </td><td>None</td></tr> <tr><td>RECNO-RENUMBER</td><td>Yes</td><td>No</td><td>Yes, Renumbered</td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_RECNO">Db.DB_RECNO</a> </td><td> <a href="../../java/com/sleepycat/db/Db.html#DB_RENUMBER">Db.DB_RENUMBER</a> </td></tr> </table> <p>Please see <a href="../../ref/am_conf/intro.html">Available Access Methods</a> for more information on access method configuration.</p> <h3>Access Method Restrictions</h3> <p>The restrictions imposed by the access method on the database model are:</p> <p><ul type=disc> <li>If keys are ordered then data may be enumerated in key order and key ranges may be used to form subsets of a data store. The SortedMap and SortedSet interfaces are supported for collections with ordered keys. <li>If duplicate keys are allowed then more than one value may be associated with the same key. This means that the data store cannot be strictly considered a map--it is really a multi-map. See <a href="cs_bdb_collection.html">com.sleepycat.bdb.collection</a> for implications on the use of the collection interfaces. <li>If duplicate keys are allowed for a data store then the data store may not have secondary indices. <li>For secondary indices with duplicates, the duplicates must be sorted. This restriction is imposed by Java API. <li>If duplicates are sorted, then all values for the same key must be distinct. This restriction is imposed by Java API. <li>If duplicates are unsorted, then values for the same key may be repeated ("duplicate duplicates"). <li>If record number keys are used then the number of records is limited to the maximum value of an unsigned 32-bit integer. <li>If record number keys are renumbered, then standard List add/remove behavior is supported but concurrency/performance is reduced. </ul> <p>See the <a href="cs_bdb_collection.html">com.sleepycat.bdb.collection</a> package for more information on how access methods impact the use of stored collections.</p> <table width="100%"><tr><td><br></td><td align=right><a href="../bdb/cs_bdb_bind.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../bdb/cs_bdb_collection.html"><img src="../../images/next.gif" alt="Next"></a> </td></tr></table> <p><font size=1><a href="../../sleepycat/legal.html">Copyright (c) 1996-2003</a> <a href="http://www.sleepycat.com">Sleepycat Software, Inc.</a> - All rights reserved.</font> </body> </html>