| 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 xmlns="" class="navheader"> |
| 16 |
<div class="libver"> |
| 17 |
<p>Library Version 11.2.5.3</p> |
| 18 |
</div> |
| 19 |
<table width="100%" summary="Navigation header"> |
| 20 |
<tr> |
| 21 |
<th colspan="3" align="center">Secondary Indices with Transaction Applications</th> |
| 22 |
</tr> |
| 23 |
<tr> |
| 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> |
| 27 |
</tr> |
| 28 |
</table> |
| 29 |
<hr /> |
| 30 |
</div> |
| 31 |
<div class="sect1" lang="en" xml:lang="en"> |
| 32 |
<div class="titlepage"> |
| 33 |
<div> |
| 34 |
<div> |
| 35 |
<h2 class="title" style="clear: both"><a id="txnindices"></a>Secondary Indices with Transaction Applications</h2> |
| 36 |
</div> |
| 37 |
</div> |
| 38 |
</div> |
| 39 |
<p> |
| 40 |
You can use transactions with your secondary indices so long as you |
| 41 |
|
| 42 |
<span> |
| 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). |
| 46 |
</span> |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
<span> |
| 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. |
| 56 |
</span> |
| 57 |
</p> |
| 58 |
<p> |
| 59 |
All other aspects of using secondary indices with transactions are |
| 60 |
identical to using secondary indices without transactions. In |
| 61 |
addition, transaction-protecting |
| 62 |
<span> |
| 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. |
| 66 |
</span> |
| 67 |
|
| 68 |
See <a class="xref" href="txncursor.html" title="Transactional Cursors">Transactional Cursors</a> for details. |
| 69 |
</p> |
| 70 |
<p> |
| 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. |
| 73 |
</p> |
| 74 |
<p> |
| 75 |
For example: |
| 76 |
</p> |
| 77 |
<pre class="programlisting">#include <db_cxx.h> |
| 78 |
|
| 79 |
... |
| 80 |
|
| 81 |
// Environment and primary database open omitted |
| 82 |
... |
| 83 |
|
| 84 |
Db my_index(&envp, 0); // Secondary |
| 85 |
|
| 86 |
// Open the secondary |
| 87 |
my_index.open(NULL, // Transaction pointer |
| 88 |
"my_secondary.db", // On-disk file that holds the database. |
| 89 |
NULL, // Optional logical database name |
| 90 |
DB_BTREE, // Database access method |
| 91 |
DB_AUTO_COMMIT, // Open flags. |
| 92 |
0); // File mode (using defaults) |
| 93 |
|
| 94 |
// Now associate the primary and the secondary |
| 95 |
my_database.associate(NULL, // Txn id |
| 96 |
&my_index, // Associated secondary database |
| 97 |
get_sales_rep, // Callback used for key |
| 98 |
// extraction. This is described |
| 99 |
// in the Getting Started guide. |
| 100 |
DB_AUTO_COMMIT); // Flags </pre> |
| 101 |
</div> |
| 102 |
<div class="navfooter"> |
| 103 |
<hr /> |
| 104 |
<table width="100%" summary="Navigation footer"> |
| 105 |
<tr> |
| 106 |
<td width="40%" align="left"><a accesskey="p" href="txncursor.html">Prev</a> </td> |
| 107 |
<td width="20%" align="center"> |
| 108 |
<a accesskey="u" href="usingtxns.html">Up</a> |
| 109 |
</td> |
| 110 |
<td width="40%" align="right"> <a accesskey="n" href="maxtxns.html">Next</a></td> |
| 111 |
</tr> |
| 112 |
<tr> |
| 113 |
<td width="40%" align="left" valign="top">Transactional Cursors </td> |
| 114 |
<td width="20%" align="center"> |
| 115 |
<a accesskey="h" href="index.html">Home</a> |
| 116 |
</td> |
| 117 |
<td width="40%" align="right" valign="top"> Configuring the Transaction Subsystem</td> |
| 118 |
</tr> |
| 119 |
</table> |
| 120 |
</div> |
| 121 |
</body> |
| 122 |
</html> |