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>Opening a Transactional Environment and Database</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="enabletxn.html" title="Chapter��2.��Enabling Transactions" /> 11 <link rel="prev" href="enabletxn.html" title="Chapter��2.��Enabling Transactions" /> 12 <link rel="next" href="usingtxns.html" title="Chapter��3.��Transaction Basics" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Opening a Transactional Environment and 19 <span>Database</span> 20 21 22 </th> 23 </tr> 24 <tr> 25 <td width="20%" align="left"><a accesskey="p" href="enabletxn.html">Prev</a>��</td> 26 <th width="60%" align="center">Chapter��2.��Enabling Transactions</th> 27 <td width="20%" align="right">��<a accesskey="n" href="usingtxns.html">Next</a></td> 28 </tr> 29 </table> 30 <hr /> 31 </div> 32 <div class="sect1" lang="en" xml:lang="en"> 33 <div class="titlepage"> 34 <div> 35 <div> 36 <h2 class="title" style="clear: both"><a id="envopen"></a>Opening a Transactional Environment and 37 <span>Database</span> 38 39 40 </h2> 41 </div> 42 </div> 43 </div> 44 <p> 45 To enable transactions for your environment, you must initialize the 46 transactional subsystem. Note that doing this also initializes the 47 logging subsystem. In addition, you must initialize the memory pool 48 (in-memory cache). You must also initialize the locking subsystem. 49 50 51 <span> 52 For example: 53 </span> 54 55 </p> 56 <pre class="programlisting">#include "db_cxx.h" 57 58... 59 60int main(void) 61{ 62 u_int32_t env_flags = DB_CREATE | // If the environment does not 63 // exist, create it. 64 DB_INIT_LOCK | // Initialize locking 65 DB_INIT_LOG | // Initialize logging 66 DB_INIT_MPOOL | // Initialize the cache 67 DB_INIT_TXN; // Initialize transactions 68 69 std::string envHome("/export1/testEnv"); 70 DbEnv myEnv(0); 71 72 try { 73 74 myEnv.open(envHome.c_str(), env_flags, 0); 75 76 } catch(DbException &e) { 77 std::cerr << "Error opening database environment: " 78 << envHome << std::endl; 79 std::cerr << e.what() << std::endl; 80 return (EXIT_FAILURE); 81 } 82 83 try { 84 myEnv.close(0); 85 } catch(DbException &e) { 86 std::cerr << "Error closing database environment: " 87 << envHome << std::endl; 88 std::cerr << e.what() << std::endl; 89 return (EXIT_FAILURE); 90 } 91 92 return (EXIT_SUCCESS); 93} </pre> 94 <p> 95 You then create and open your database(s) as you would for a non-transactional system. 96 <span> 97 The only difference is that you must pass the environment handle to 98 the 99 100 <span> 101 <code class="methodname">DbEnv::open()</code> method, 102 </span> 103 and you must open the database within a transaction. 104 Typically auto commit is used for this purpose. To do so, pass 105 <code class="literal">DB_AUTO_COMMIT</code> to the database open command. 106 Also, make sure you close all your databases before you close 107 your environment. 108 For example: 109 </span> 110 111 112 </p> 113 <pre class="programlisting">#include "db_cxx.h" 114 115... 116 117int main(void) 118{ 119 u_int32_t env_flags = DB_CREATE | // If the environment does not 120 // exist, create it. 121 DB_INIT_LOCK | // Initialize locking 122 DB_INIT_LOG | // Initialize logging 123 DB_INIT_MPOOL | // Initialize the cache 124 DB_INIT_TXN; // Initialize transactions 125 126 <strong class="userinput"><code>u_int32_t db_flags = DB_CREATE | DB_AUTO_COMMIT; 127 Db *dbp = NULL; 128 const char *file_name = "mydb.db";</code></strong> 129 130 std::string envHome("/export1/testEnv"); 131 DbEnv myEnv(0); 132 133 try { 134 135 myEnv.open(envHome.c_str(), env_flags, 0); 136 <strong class="userinput"><code>dbp = new Db(&myEnv, 0); 137 dbp->open(NULL, // Txn pointer 138 file_name, // File name 139 NULL, // Logical db name 140 DB_BTREE, // Database type (using btree) 141 db_flags, // Open flags 142 0); // File mode. Using defaults </code></strong> 143 144 } catch(DbException &e) { 145 std::cerr << "Error opening database and environment: " 146 << file_name << ", " 147 << envHome << std::endl; 148 std::cerr << e.what() << std::endl; 149 } 150 151 try { 152 <strong class="userinput"><code>dbp->close(0);</code></strong> 153 myEnv.close(0); 154 } catch(DbException &e) { 155 std::cerr << "Error closing database and environment: " 156 << file_name << ", " 157 << envHome << std::endl; 158 std::cerr << e.what() << std::endl; 159 return (EXIT_FAILURE); 160 } 161 162 return (EXIT_SUCCESS); 163} </pre> 164 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> 165 <h3 class="title">Note</h3> 166 <p> 167 Never close a database that has active transactions. Make sure 168 all transactions are resolved (either committed or aborted) 169 before closing the database. 170 </p> 171 </div> 172 </div> 173 <div class="navfooter"> 174 <hr /> 175 <table width="100%" summary="Navigation footer"> 176 <tr> 177 <td width="40%" align="left"><a accesskey="p" href="enabletxn.html">Prev</a>��</td> 178 <td width="20%" align="center"> 179 <a accesskey="u" href="enabletxn.html">Up</a> 180 </td> 181 <td width="40%" align="right">��<a accesskey="n" href="usingtxns.html">Next</a></td> 182 </tr> 183 <tr> 184 <td width="40%" align="left" valign="top">Chapter��2.��Enabling Transactions��</td> 185 <td width="20%" align="center"> 186 <a accesskey="h" href="index.html">Home</a> 187 </td> 188 <td width="40%" align="right" valign="top">��Chapter��3.��Transaction Basics</td> 189 </tr> 190 </table> 191 </div> 192 </body> 193</html> 194