Debian Codegraph @

docs/gsg_txn/CXX/envopen.html

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