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 &lt;db_cxx.h&gt;
81
82...
83
84// Environment and primary database open omitted
85...
86
87Db my_index(&amp;envp, 0);    // Secondary
88
89// Open the secondary
90my_index.open(NULL,              // Transaction pointer
91              "my_secondary.db", // On-disk file that holds the database.
92              NULL,              // Optional logical database name
93              DB_BTREE,          // Database access method
94              DB_AUTO_COMMIT,    // Open flags.
95              0);                // File mode (using defaults)
96
97// Now associate the primary and the secondary
98my_database.associate(NULL,             // Txn id
99                      &amp;my_index,        // Associated secondary database
100                      get_sales_rep,    // Callback used for key extraction.
101                                        // This is described in the Getting 
102                                        // Started guide.
103                      DB_AUTO_COMMIT);  // Flags </pre>
104    </div>
105    <div class="navfooter">
106      <hr />
107      <table width="100%" summary="Navigation footer">
108        <tr>
109          <td width="40%" align="left"><a accesskey="p" href="txncursor.html">Prev</a> </td>
110          <td width="20%" align="center">
111            <a accesskey="u" href="usingtxns.html">Up</a>
112          </td>
113          <td width="40%" align="right"> <a accesskey="n" href="maxtxns.html">Next</a></td>
114        </tr>
115        <tr>
116          <td width="40%" align="left" valign="top">Transactional Cursors </td>
117          <td width="20%" align="center">
118            <a accesskey="h" href="index.html">Home</a>
119          </td>
120          <td width="40%" align="right" valign="top"> Configuring the Transaction Subsystem</td>
121        </tr>
122      </table>
123    </div>
124  </body>
125</html>
126