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