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