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