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>Secondary Indices with Transaction Applications</title> 7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> 9 <link rel="home" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" /> 10 <link rel="up" href="usingtxns.html" title="Chapter��3.��Transaction Basics" /> 11 <link rel="previous" href="txncursor.html" title="Transactional Cursors" /> 12 <link rel="next" href="maxtxns.html" title="Configuring the Transaction Subsystem" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Secondary Indices with Transaction Applications</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="txncursor.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��3.��Transaction Basics</th> 23 <td width="20%" align="right">��<a accesskey="n" href="maxtxns.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="sect1" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title" style="clear: both"><a id="txnindices"></a>Secondary Indices with Transaction Applications</h2> 33 </div> 34 </div> 35 <div></div> 36 </div> 37 <p> 38 39 <span> 40 If you are using the base API, then you 41 </span> 42 43 can use transactions with your secondary indices so long as you 44 45 46 47 <span> 48 open the secondary index so that it is transactional. 49 </span> 50 51 52 </p> 53 <p> 54 All other aspects of using secondary indices with transactions are 55 identical to using secondary indices without transactions. In 56 addition, transaction-protecting 57 58 <span> 59 secondary cursors is performed just as you protect normal 60 cursors ��� you simply have to make sure the cursor is 61 opened using a transaction handle, and that the cursor is 62 closed before the handle is either either committed or 63 aborted. 64 </span> 65 See <a href="txncursor.html">Transactional Cursors</a> for details. 66 </p> 67 <p> 68 Note that when you use transactions to protect your database writes, your secondary indices are protected from 69 corruption because updates to the primary and the secondaries are performed in a single atomic transaction. 70 </p> 71 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> 72 <h3 class="title">Note</h3> 73 <p> 74 If you are using the DPL, then be aware that 75 you never have to provide a transactional 76 handle when opening an index, be it a primary 77 or a secondary. However, transactions are 78 enabled for your store, then all of the indexes 79 that you open will be enabled for transactional 80 usage. More, any write operation performed 81 using that index will be done using a 82 transaction, regardless of whether you 83 explictly provide a transactional handle to the 84 write operation. 85 </p> 86 <p> 87 If you do not explictly provide a transaction 88 handle to DPL write operations performed on a 89 transactional store, then auto commit is 90 silently used for that operation. 91 </p> 92 </div> 93 <p> 94 For example: 95 </p> 96 <pre class="programlisting">package db.GettingStarted; 97 98import com.sleepycat.bind.tuple.TupleBinding; 99import com.sleepycat.db.Database; 100import com.sleepycat.db.DatabaseType; 101import com.sleepycat.db.DatabaseConfig; 102import com.sleepycat.db.DatabaseException; 103import com.sleepycat.db.Environment; 104import com.sleepycat.db.EnvironmentConfig; 105import com.sleepycat.db.SecondaryDatabase; 106import com.sleepycat.db.SecondaryConfig; 107 108import java.io.FileNotFoundException; 109 110... 111 112// Environment and primary database opens omitted. 113 114SecondaryConfig mySecConfig = new SecondaryConfig(); 115mySecConfig.setAllowCreate(true); 116mySecConfig.setType(DatabaseType.BTREE); 117mySecConfig.setTransactional(true); 118 119SecondaryDatabase mySecDb = null; 120try { 121 // A fake tuple binding that is not actually implemented anywhere. 122 // The tuple binding is dependent on the data in use. 123 // See the Getting Started Guide for details 124 TupleBinding myTupleBinding = new MyTupleBinding(); 125 126 // Open the secondary. FullNameKeyCreator is not actually implemented 127 // anywhere. See the Getting Started Guide for details. 128 FullNameKeyCreator keyCreator = new FullNameKeyCreator(myTupleBinding); 129 130 // Set the key creator on the secondary config object. 131 mySecConfig.setKeyCreator(keyCreator); 132 133 // Perform the actual open. Because this database is configured to be 134 // transactional, the open is automatically wrapped in a transaction. 135 // - myEnv is the environment handle. 136 // - myDb is the primary database handle. 137 String secDbName = "mySecondaryDatabase"; 138 mySecDb = myEnv.openSecondary(null, secDbName, null, myDb, mySecConfig); 139} catch (DatabaseException de) { 140 // Exception handling goes here ... 141} catch (FileNotFoundException fnfe) { 142 // Exception handling goes here ... 143}</pre> 144 </div> 145 <div class="navfooter"> 146 <hr /> 147 <table width="100%" summary="Navigation footer"> 148 <tr> 149 <td width="40%" align="left"><a accesskey="p" href="txncursor.html">Prev</a>��</td> 150 <td width="20%" align="center"> 151 <a accesskey="u" href="usingtxns.html">Up</a> 152 </td> 153 <td width="40%" align="right">��<a accesskey="n" href="maxtxns.html">Next</a></td> 154 </tr> 155 <tr> 156 <td width="40%" align="left" valign="top">Transactional Cursors��</td> 157 <td width="20%" align="center"> 158 <a accesskey="h" href="index.html">Home</a> 159 </td> 160 <td width="40%" align="right" valign="top">��Configuring the Transaction Subsystem</td> 161 </tr> 162 </table> 163 </div> 164 </body> 165</html> 166