2
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
4
<!--See the file LICENSE for redistribution information.-->
7
<title>Berkeley DB Reference Guide: Transactional cursors</title>
8
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
9
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
12
<table width="100%"><tr valign=top>
13
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Berkeley DB Transactional Data Store Applications</dl></h3></td>
14
<td align=right><a href="../../ref/transapp/read.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/transapp/nested.html"><img src="../../images/next.gif" alt="Next"></a>
17
<h1 align=center>Transactional cursors</h1>
18
<p>Berkeley DB cursors may be used inside a transaction, exactly as any other
19
<a href="../../api_c/db_class.html">DB</a> method. The enclosing transaction ID must be specified when
20
the cursor is created, but it does not then need to be further specified
21
on operations performed using the cursor. One important point to
22
remember is that a cursor <b>must be closed</b> before the enclosing
23
transaction is committed or aborted.
24
<p>The following code fragment uses a cursor to store a new key in the cats
25
database with four associated data items. The key is a name. The data
26
items are a company name, an address, and a list of the breeds of cat
27
owned. Each of the data entries is stored as a duplicate data item.
28
In this example, transactions are necessary to ensure that either all or none
29
of the data items appear in case of system or application failure.
30
<p><blockquote><pre>int
31
main(int argc, char *argv)
35
DB *db_cats, *db_color, *db_fruit;
40
while ((ch = getopt(argc, argv, "")) != EOF)
52
/* Open database: Key is fruit class; Data is specific type. */
53
db_open(dbenv, &db_fruit, "fruit", 0);
55
/* Open database: Key is a color; Data is an integer. */
56
db_open(dbenv, &db_color, "color", 0);
60
* Key is a name; Data is: company name, cat breeds.
62
db_open(dbenv, &db_cats, "cats", 1);
64
add_fruit(dbenv, db_fruit, "apple", "yellow delicious");
66
add_color(dbenv, db_color, "blue", 0);
67
add_color(dbenv, db_color, "blue", 3);
69
<b> add_cat(dbenv, db_cats,
81
add_cat(DB_ENV *dbenv, DB *db, char *name, ...)
93
memset(&key, 0, sizeof(key));
94
memset(&data, 0, sizeof(data));
96
key.size = strlen(name);
98
retry: /* Begin the transaction. */
99
if ((ret = dbenv->txn_begin(dbenv, NULL, &tid, 0)) != 0) {
100
dbenv->err(dbenv, ret, "DB_ENV->txn_begin");
104
/* Delete any previously existing item. */
105
switch (ret = db->del(db, tid, &key, 0)) {
109
case DB_LOCK_DEADLOCK:
111
/* Retry the operation. */
112
if ((t_ret = tid->abort(tid)) != 0) {
113
dbenv->err(dbenv, t_ret, "DB_TXN->abort");
116
if (++fail == MAXIMUM_RETRY)
121
/* Create a cursor. */
122
if ((ret = db->cursor(db, tid, &dbc, 0)) != 0) {
123
dbenv->err(dbenv, ret, "db->cursor");
127
/* Append the items, in order. */
129
while ((s = va_arg(ap, char *)) != NULL) {
131
data.size = strlen(s);
132
switch (ret = dbc->c_put(dbc, &key, &data, DB_KEYLAST)) {
135
case DB_LOCK_DEADLOCK:
139
/* Retry the operation. */
140
if ((t_ret = dbc->c_close(dbc)) != 0) {
142
dbenv, t_ret, "dbc->c_close");
145
if ((t_ret = tid->abort(tid)) != 0) {
146
dbenv->err(dbenv, t_ret, "DB_TXN->abort");
149
if (++fail == MAXIMUM_RETRY)
156
/* Success: commit the change. */
157
if ((ret = dbc->c_close(dbc)) != 0) {
158
dbenv->err(dbenv, ret, "dbc->c_close");
161
if ((ret = tid->commit(tid, 0)) != 0) {
162
dbenv->err(dbenv, ret, "DB_TXN->commit");
166
}</b></pre></blockquote>
167
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/transapp/read.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/transapp/nested.html"><img src="../../images/next.gif" alt="Next"></a>
169
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>