| 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 <stdio.h> |
| 51 |
#include <stdlib.h> |
| 52 |
|
| 53 |
#include "db.h" |
| 54 |
|
| 55 |
int |
| 56 |
main(void) |
| 57 |
{ |
| 58 |
DBT key, data; |
| 59 |
DBC *cursorp; |
| 60 |
DB_TXN *txn = NULL; |
| 61 |
int ret, c_ret; |
| 62 |
char *replacementString = "new string"; |
| 63 |
|
| 64 |
... |
| 65 |
/* environment and db handle creation omitted */ |
| 66 |
... |
| 67 |
|
| 68 |
/* Get the txn handle */ |
| 69 |
txn = NULL; |
| 70 |
ret = envp->txn_begin(envp, NULL, &txn, 0); |
| 71 |
if (ret != 0) { |
| 72 |
envp->err(envp, ret, "Transaction begin failed."); |
| 73 |
goto err; |
| 74 |
} |
| 75 |
|
| 76 |
/* Get the cursor, supply the txn handle at that time */ |
| 77 |
ret = dbp->cursor(dbp, txn, &cursorp, 0); |
| 78 |
if (ret != 0) { |
| 79 |
envp->err(envp, ret, "Cursor open failed."); |
| 80 |
txn->abort(txn); |
| 81 |
goto err; |
| 82 |
} |
| 83 |
|
| 84 |
/* |
| 85 |
* Now use the cursor. Note that we do not supply any txn handles to |
| 86 |
* these methods. |
| 87 |
*/ |
| 88 |
/* Prepare the DBTs */ |
| 89 |
memset(&key, 0, sizeof(DBT)); |
| 90 |
memset(&data, 0, sizeof(DBT)); |
| 91 |
while (cursor->get(&key, &data, DB_NEXT) == 0) { |
| 92 |
data->data = (void *)replacementString; |
| 93 |
data->size = (strlen(replacementString) + 1) * sizeof(char); |
| 94 |
c_ret = cursor->put(cursor, &key, &data, DB_CURRENT); |
| 95 |
if (c_ret != 0) { |
| 96 |
/* abort the transaction and goto error */ |
| 97 |
envp->err(envp, ret, "Cursor put failed."); |
| 98 |
cursorp->close(cursorp); |
| 99 |
cursorp = NULL; |
| 100 |
txn->abort(txn); |
| 101 |
goto err; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/* |
| 106 |
* Commit the transaction. Note that the transaction handle |
| 107 |
* can no longer be used. |
| 108 |
*/ |
| 109 |
ret = cursorp->close(cursorp); |
| 110 |
if (ret != 0) { |
| 111 |
envp->err(envp, ret, "Cursor close failed."); |
| 112 |
txn->abort(txn); |
| 113 |
goto err; |
| 114 |
} |
| 115 |
ret = txn->commit(txn, 0); |
| 116 |
if (ret != 0) { |
| 117 |
envp->err(envp, ret, "Transaction commit failed."); |
| 118 |
goto err; |
| 119 |
} |
| 120 |
|
| 121 |
err: |
| 122 |
/* Close the cursor (if the handle is not NULL) |
| 123 |
* and perform whatever other cleanup is required */ |
| 124 |
|
| 125 |
/* Close the database */ |
| 126 |
|
| 127 |
/* Close the environment */ |
| 128 |
|
| 129 |
... |
| 130 |
|
| 131 |
if (c_ret != 0) |
| 132 |
ret = c_ret; |
| 133 |
|
| 134 |
return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); |
| 135 |
} </pre> |
| 136 |
</div> |
| 137 |
<div class="navfooter"> |
| 138 |
<hr /> |
| 139 |
<table width="100%" summary="Navigation footer"> |
| 140 |
<tr> |
| 141 |
<td width="40%" align="left"><a accesskey="p" href="nestedtxn.html">Prev</a> </td> |
| 142 |
<td width="20%" align="center"> |
| 143 |
<a accesskey="u" href="usingtxns.html">Up</a> |
| 144 |
</td> |
| 145 |
<td width="40%" align="right"> <a accesskey="n" href="txnindices.html">Next</a></td> |
| 146 |
</tr> |
| 147 |
<tr> |
| 148 |
<td width="40%" align="left" valign="top">Nested Transactions </td> |
| 149 |
<td width="20%" align="center"> |
| 150 |
<a accesskey="h" href="index.html">Home</a> |
| 151 |
</td> |
| 152 |
<td width="40%" align="right" valign="top"> Secondary Indices with Transaction Applications</td> |
| 153 |
</tr> |
| 154 |
</table> |
| 155 |
</div> |
| 156 |
</body> |
| 157 |
</html> |