| 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.h> |
| 78 |
|
| 79 |
... |
| 80 |
|
| 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 */ |
| 85 |
|
| 86 |
/* Environment and primary database opens omitted */ |
| 87 |
|
| 88 |
/* Secondary */ |
| 89 |
ret = db_create(&sdbp, envp, 0); |
| 90 |
if (ret != 0) { |
| 91 |
/* Error handling goes here */ |
| 92 |
} |
| 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 |
| 97 |
* database. */ |
| 98 |
NULL, /* Optional logical database name */ |
| 99 |
DB_BTREE, /* Database access method */ |
| 100 |
DB_AUTO_COMMIT, /* Open flags */ |
| 101 |
0); /* File mode (using defaults) */ |
| 102 |
if (ret != 0) { |
| 103 |
/* Error handling goes here */ |
| 104 |
} |
| 105 |
|
| 106 |
/* Now associate the secondary to the primary */ |
| 107 |
dbp->associate(dbp, /* Primary database */ |
| 108 |
NULL, /* TXN id */ |
| 109 |
sdbp, /* Secondary database */ |
| 110 |
get_sales_rep, /* Callback used for key creation. This |
| 111 |
* is described in the Getting Started |
| 112 |
* guide. */ |
| 113 |
DB_AUTO_COMMIT);/* Flags */</pre> |
| 114 |
</div> |
| 115 |
<div class="navfooter"> |
| 116 |
<hr /> |
| 117 |
<table width="100%" summary="Navigation footer"> |
| 118 |
<tr> |
| 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> |
| 122 |
</td> |
| 123 |
<td width="40%" align="right"> <a accesskey="n" href="maxtxns.html">Next</a></td> |
| 124 |
</tr> |
| 125 |
<tr> |
| 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> |
| 129 |
</td> |
| 130 |
<td width="40%" align="right" valign="top"> Configuring the Transaction Subsystem</td> |
| 131 |
</tr> |
| 132 |
</table> |
| 133 |
</div> |
| 134 |
</body> |
| 135 |
</html> |