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