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