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">
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" />
15 <div xmlns="" class="navheader">
17 <p>Library Version 11.2.5.3</p>
19 <table width="100%" summary="Navigation header">
21 <th colspan="3" align="center">Secondary Indices with Transaction Applications</th>
24 <td width="20%" align="left"><a accesskey="p" href="txncursor.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 3. Transaction Basics</th>
26 <td width="20%" align="right"> <a accesskey="n" href="maxtxns.html">Next</a></td>
31 <div class="sect1" lang="en" xml:lang="en">
32 <div class="titlepage">
35 <h2 class="title" style="clear: both"><a id="txnindices"></a>Secondary Indices with Transaction Applications</h2>
40 You can use transactions with your secondary indices so long as you
43 open the secondary index so that it supports transactions (that is,
44 you wrap the database open in a transaction, or use auto commit,
45 in the same way as when you open a primary transactional database).
51 In addition, you must make sure that when you associate the
52 secondary index with the primary database, the association is
53 performed using a transaction. The easiest thing to do here is
54 to simply specify <code class="literal">DB_AUTO_COMMIT</code> when you
55 perform the association.
59 All other aspects of using secondary indices with transactions are
60 identical to using secondary indices without transactions. In
61 addition, transaction-protecting
63 cursors opened against secondary indices is performed in
64 exactly the same way as when you use transactional cursors
65 against a primary database.
68 See <a class="xref" href="txncursor.html" title="Transactional Cursors">Transactional Cursors</a> for details.
71 Note that when you use transactions to protect your database writes, your secondary indices are protected from
72 corruption because updates to the primary and the secondaries are performed in a single atomic transaction.
77 <pre class="programlisting">#include <db.h>
81 DB_ENV *envp; /* Environment pointer */
82 DB *dbp, *sdbp; /* Primary and secondary DB handles */
83 u_int32_t flags; /* Primary database open flags */
84 int ret; /* Function return value */
86 /* Environment and primary database opens omitted */
89 ret = db_create(&sdbp, envp, 0);
91 /* Error handling goes here */
93 /* open the secondary database */
94 ret = sdbp->open(sdbp, /* DB structure pointer */
95 NULL, /* Transaction pointer */
96 "my_secdb.db", /* On-disk file that holds the
98 NULL, /* Optional logical database name */
99 DB_BTREE, /* Database access method */
100 DB_AUTO_COMMIT, /* Open flags */
101 0); /* File mode (using defaults) */
103 /* Error handling goes here */
106 /* Now associate the secondary to the primary */
107 dbp->associate(dbp, /* Primary database */
109 sdbp, /* Secondary database */
110 get_sales_rep, /* Callback used for key creation. This
111 * is described in the Getting Started
113 DB_AUTO_COMMIT);/* Flags */</pre>
115 <div class="navfooter">
117 <table width="100%" summary="Navigation footer">
119 <td width="40%" align="left"><a accesskey="p" href="txncursor.html">Prev</a> </td>
120 <td width="20%" align="center">
121 <a accesskey="u" href="usingtxns.html">Up</a>
123 <td width="40%" align="right"> <a accesskey="n" href="maxtxns.html">Next</a></td>
126 <td width="40%" align="left" valign="top">Transactional Cursors </td>
127 <td width="20%" align="center">
128 <a accesskey="h" href="index.html">Home</a>
130 <td width="40%" align="right" valign="top"> Configuring the Transaction Subsystem</td>