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>Auto Commit</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="abortresults.html" title="Aborting a Transaction" />
12    <link rel="next" href="nestedtxn.html" title="Nested Transactions" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Auto Commit</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="abortresults.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="nestedtxn.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="autocommit"></a>Auto Commit</h2>
33          </div>
34        </div>
35      </div>
36      <p>
37            While transactions are frequently used to provide atomicity to
38            multiple database 
39            
40            operations, it is sometimes necessary to perform
41            a single database 
42            
43            operation under the control of a transaction.
44            Rather than force you to obtain a transaction, perform the single 
45            write operation, and then either commit or abort the transaction,
46            you can automatically group this sequence of events using
47            <span class="emphasis"><em>auto commit</em></span>.
48        </p>
49      <p>
50            To use auto commit:
51        </p>
52      <div class="orderedlist">
53        <ol type="1">
54          <li>
55            <p>
56                        Open your environment and your databases 
57                        
58                        so that they support
59                    transactions.  See <a class="xref" href="enabletxn.html" title="Chapter��2.��Enabling Transactions">Enabling Transactions</a> 
60                    for details.
61                </p>
62            <p>
63                    Note that frequently auto commit is used for the environment
64                    or database open.  To use auto commit for either your
65                    environment or database open, specify 
66                    <code class="literal">DB_AUTO_COMMIT</code> to the 
67
68                    <code class="methodname">DB_ENV-&gt;set_flags()</code>
69                    
70                    or
71                    <code class="methodname">DB-&gt;open()</code>
72                    
73                    method. If you specify auto commit for the environment
74                    open, then you do not need to also specify auto commit
75                    for the database open.
76                </p>
77          </li>
78          <li>
79            <p>
80                    Do not provide a transactional handle to the method that is
81                    performing the database 
82                        
83                    write operation.
84                </p>
85          </li>
86        </ol>
87      </div>
88      <p>
89            Note that auto commit is not available for cursors. You must always
90            open your cursor using a transaction if you want the cursor's
91            operations to be transactional protected. See 
92            <a class="xref" href="txncursor.html" title="Transactional Cursors">Transactional Cursors</a> for details on using
93            transactional cursors.
94        </p>
95      <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
96        <h3 class="title">Note</h3>
97        <p>
98                Never have more than one active transaction in your thread
99                at a time. This is especially a problem if you mix an
100                explicit transaction with another operation that uses auto
101                commit. Doing so can result in undetectable deadlocks.
102            </p>
103      </div>
104      <p>
105            For example, the following uses auto commit to perform the database write operation:
106        </p>
107      <pre class="programlisting">#include &lt;stdio.h&gt;
108#include &lt;stdlib.h&gt;
109
110#include "db.h"
111
112int
113main(void)
114{
115    int ret, ret_c;
116    u_int32_t db_flags, env_flags;
117    DB *dbp;
118    DB_ENV *envp;
119    DBT key, data;
120    const char *db_home_dir = "/tmp/myEnvironment";
121    const char *file_name = "mydb.db";
122    const char *keystr ="thekey";
123    const char *datastr = "thedata";
124    
125    dbp = NULL;
126    envp = NULL;
127
128    /* Open the environment */
129    ret = db_env_create(&amp;envp, 0);
130    if (ret != 0) {
131        fprintf(stderr, "Error creating environment handle: %s\n",
132            db_strerror(ret));
133        return (EXIT_FAILURE);
134    }
135                                                                                                                                  
136    env_flags = DB_CREATE |    /* Create the environment if it does 
137                                * not already exist. */
138                DB_INIT_TXN  | /* Initialize transactions */
139                DB_INIT_LOCK | /* Initialize locking. */
140                DB_INIT_LOG  | /* Initialize logging */
141                DB_INIT_MPOOL; /* Initialize the in-memory cache. */
142
143    ret = envp-&gt;open(envp, db_home_dir, env_flags, 0);
144    if (ret != 0) {
145        fprintf(stderr, "Error opening environment: %s\n",
146            db_strerror(ret));
147        goto err;
148    }
149
150    /* Initialize the DB handle */
151    ret = db_create(&amp;dbp, envp, 0);
152    if (ret != 0) {
153        envp-&gt;err(envp, ret, "Database creation failed");
154        goto err;
155    }
156
157    db_flags = DB_CREATE | DB_AUTO_COMMIT;
158    /* 
159     * Open the database. Note that we are using auto commit for the open, 
160     * so the database is able to support transactions.
161     */
162    ret = dbp-&gt;open(dbp,        /* Pointer to the database */
163                    NULL,       /* Txn pointer */
164                    file_name,  /* File name */
165                    NULL,       /* Logical db name */
166                    DB_BTREE,   /* Database type (using btree) */
167                    db_flags,   /* Open flags */
168                    0);         /* File mode. Using defaults */
169    if (ret != 0) {
170        envp-&gt;err(envp, ret, "Database '%s' open failed",
171            file_name);
172        goto err;
173    }
174
175    /* Prepare the DBTs */
176    memset(&amp;key, 0, sizeof(DBT));
177    memset(&amp;data, 0, sizeof(DBT));
178
179    key.data = &amp;keystr;
180    key.size = strlen(keystr) + 1;
181    data.data = &amp;datastr;
182    data.size = strlen(datastr) + 1;
183
184
185    /*
186     * Perform the database write. A txn handle is not provided, but the
187     * database support auto commit, so auto commit is used for the write.
188     */
189    ret = dbp-&gt;put(dbp, NULL, &amp;key, &amp;data, 0);
190    if (ret != 0) {
191        envp-&gt;err(envp, ret, "Database put failed.");
192        goto err;
193    }
194
195err:
196    /* Close the database */
197    if (dbp != NULL) {
198        ret_c = dbp-&gt;close(dbp, 0);
199        if (ret_c != 0) {
200            envp-&gt;err(envp, ret_c, "Database close failed.");
201            ret = ret_c
202        }
203    }
204
205
206    /* Close the environment */
207    if (envp != NULL) {
208        ret_c = envp-&gt;close(envp, 0);
209        if (ret_c != 0) {
210            fprintf(stderr, "environment close failed: %s\n",
211                db_strerror(ret_c));
212            ret = ret_c;
213        }
214    }
215
216    return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
217} </pre>
218    </div>
219    <div class="navfooter">
220      <hr />
221      <table width="100%" summary="Navigation footer">
222        <tr>
223          <td width="40%" align="left"><a accesskey="p" href="abortresults.html">Prev</a>��</td>
224          <td width="20%" align="center">
225            <a accesskey="u" href="usingtxns.html">Up</a>
226          </td>
227          <td width="40%" align="right">��<a accesskey="n" href="nestedtxn.html">Next</a></td>
228        </tr>
229        <tr>
230          <td width="40%" align="left" valign="top">Aborting a Transaction��</td>
231          <td width="20%" align="center">
232            <a accesskey="h" href="index.html">Home</a>
233          </td>
234          <td width="40%" align="right" valign="top">��Nested Transactions</td>
235        </tr>
236      </table>
237    </div>
238  </body>
239</html>
240