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>Transactional Cursors</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="nestedtxn.html" title="Nested Transactions" />
12    <link rel="next" href="txnindices.html" title="Secondary Indices with Transaction Applications" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Transactional Cursors</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="nestedtxn.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="txnindices.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="txncursor"></a>Transactional Cursors</h2>
33          </div>
34        </div>
35      </div>
36      <p>
37            You can transaction-protect your cursor operations by
38            specifying a transaction handle at the time that you create
39            your cursor.  Beyond that, you do not ever
40            provide a transaction handle directly to a cursor method.
41        </p>
42      <p>
43            Note that if you transaction-protect a cursor, then you must
44            make sure that the cursor is closed before you either commit or
45            abort the transaction. For example:
46        </p>
47      <pre class="programlisting">#include "db_cxx.h"
48
49...
50                                                                                                                                  
51int main(void)
52{
53    // Environment and database opens omitted
54    ...
55
56    DbTxn *txn = NULL;
57    Dbc *cursorp = NULL;
58
59    try {
60
61        Dbt key, data;
62        key.set_data(keystr);
63        key.set_size((strlen(keystr) + 1) * sizeof(char));
64        key.set_data(datastr);
65        key.set_size((strlen(datastr) + 1) * sizeof(char));
66
67        DbTxn *txn = NULL;
68        myEnv.txn_begin(NULL, &amp;txn, 0);
69        try {
70            // Get our cursor. Note that we pass the transaction handle here.
71            db.cursor(txn, &amp;cursorp, 0); 
72
73            // Perform our operations. Note that we do not pass a transaction
74            // handle here.
75            char *replacementString = "new string";
76            while (cursor-&gt;get(&amp;key, &amp;data, DB_NEXT) == 0) {
77                data.set_data(void *)replacementString);
78                data.set_size((strlen(replacementString) + 1) * sizeof(char));
79                cursor-&gt;put(&amp;key, &amp;data, DB_CURRENT);
80            }
81
82            // We're done. Commit the transaction.
83            cursor-&gt;close();
84            txn-&gt;commit(0);
85        } catch (DbException &amp;e) {
86            std::cerr &lt;&lt; "Error in transaction: "
87                       &lt;&lt; e.what() &lt;&lt; std::endl;
88            cursor-&gt;close();
89            txn-&gt;abort();
90        }
91
92    } catch(DbException &amp;e) {
93        std::cerr &lt;&lt; "Error opening database and environment: "
94                  &lt;&lt; file_name &lt;&lt; ", "
95                  &lt;&lt; envHome &lt;&lt; std::endl;
96        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
97    }
98
99
100    return (EXIT_SUCCESS);
101} </pre>
102    </div>
103    <div class="navfooter">
104      <hr />
105      <table width="100%" summary="Navigation footer">
106        <tr>
107          <td width="40%" align="left"><a accesskey="p" href="nestedtxn.html">Prev</a> </td>
108          <td width="20%" align="center">
109            <a accesskey="u" href="usingtxns.html">Up</a>
110          </td>
111          <td width="40%" align="right"> <a accesskey="n" href="txnindices.html">Next</a></td>
112        </tr>
113        <tr>
114          <td width="40%" align="left" valign="top">Nested Transactions </td>
115          <td width="20%" align="center">
116            <a accesskey="h" href="index.html">Home</a>
117          </td>
118          <td width="40%" align="right" valign="top"> Secondary Indices with Transaction Applications</td>
119        </tr>
120      </table>
121    </div>
122  </body>
123</html>
124