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 <span> 39 You 40 </span> 41 42 43 can use transactions with your secondary indices so long as you 44 45 <span> 46 open the secondary index so that it supports transactions (that is, 47 you wrap the database open in a transaction, or use auto commit, 48 in the same way as when you open a primary transactional database). 49 </span> 50 51 52 53 <span> 54 In addition, you must make sure that when you associate the 55 secondary index with the primary database, the association is 56 performed using a transaction. The easiest thing to do here is 57 to simply specify <tt class="literal">DB_AUTO_COMMIT</tt> when you 58 perform the association. 59 </span> 60 </p> 61 <p> 62 All other aspects of using secondary indices with transactions are 63 identical to using secondary indices without transactions. In 64 addition, transaction-protecting 65 <span> 66 cursors opened against secondary indices is performed in 67 exactly the same way as when you use transactional cursors 68 against a primary database. 69 </span> 70 71 See <a href="txncursor.html">Transactional Cursors</a> for details. 72 </p> 73 <p> 74 Note that when you use transactions to protect your database writes, your secondary indices are protected from 75 corruption because updates to the primary and the secondaries are performed in a single atomic transaction. 76 </p> 77 <p> 78 For example: 79 </p> 80 <pre class="programlisting">#include <db.h> 81 82... 83 84DB_ENV *envp; /* Environment pointer */ 85DB *dbp, *sdbp; /* Primary and secondary DB handles */ 86u_int32_t flags; /* Primary database open flags */ 87int ret; /* Function return value */ 88 89/* Environment and primary database opens omitted */ 90 91/* Secondary */ 92ret = db_create(&sdbp, envp, 0); 93if (ret != 0) { 94 /* Error handling goes here */ 95} 96/* open the secondary database */ 97ret = sdbp->open(sdbp, /* DB structure pointer */ 98 NULL, /* Transaction pointer */ 99 "my_secdb.db", /* On-disk file that holds the database. */ 100 NULL, /* Optional logical database name */ 101 DB_BTREE, /* Database access method */ 102 DB_AUTO_COMMIT, /* Open flags */ 103 0); /* File mode (using defaults) */ 104if (ret != 0) { 105 /* Error handling goes here */ 106} 107 108/* Now associate the secondary to the primary */ 109dbp->associate(dbp, /* Primary database */ 110 NULL, /* TXN id */ 111 sdbp, /* Secondary database */ 112 get_sales_rep, /* Callback used for key creation. This 113 * is described in the Getting Started 114 * guide. */ 115 DB_AUTO_COMMIT);/* Flags */</pre> 116 </div> 117 <div class="navfooter"> 118 <hr /> 119 <table width="100%" summary="Navigation footer"> 120 <tr> 121 <td width="40%" align="left"><a accesskey="p" href="txncursor.html">Prev</a> </td> 122 <td width="20%" align="center"> 123 <a accesskey="u" href="usingtxns.html">Up</a> 124 </td> 125 <td width="40%" align="right"> <a accesskey="n" href="maxtxns.html">Next</a></td> 126 </tr> 127 <tr> 128 <td width="40%" align="left" valign="top">Transactional Cursors </td> 129 <td width="20%" align="center"> 130 <a accesskey="h" href="index.html">Home</a> 131 </td> 132 <td width="40%" align="right" valign="top"> Configuring the Transaction Subsystem</td> 133 </tr> 134 </table> 135 </div> 136 </body> 137</html> 138