Debian Codegraph @

docs/gsg_txn/C/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 <p>
60 Notice in the following example that you create your environment
61 handle using the <code class="function">db_env_create()</code> function before you open
62 the environment:
63 </p>
64 <pre class="programlisting">#include &lt;stdio.h&gt;
65 #include &lt;stdlib.h&gt;
66
67 #include "db.h"
68
69 int
70 main(void)
71 {
72 int ret, ret_c;
73 u_int32_t env_flags;
74 DB_ENV *envp;
75 const char *db_home_dir = "/tmp/myEnvironment";
76
77 envp = NULL;
78
79 /* Open the environment */
80 ret = db_env_create(&amp;envp, 0);
81 if (ret != 0) {
82 fprintf(stderr, "Error creating environment handle: %s\n",
83 db_strerror(ret));
84 return (EXIT_FAILURE);
85 }
86
87 env_flags = DB_CREATE | /* Create the environment if it does
88 * not already exist. */
89 DB_INIT_TXN | /* Initialize transactions */
90 DB_INIT_LOCK | /* Initialize locking. */
91 DB_INIT_LOG | /* Initialize logging */
92 DB_INIT_MPOOL; /* Initialize the in-memory cache. */
93
94 ret = envp-&gt;open(envp, db_home_dir, env_flags, 0);
95 if (ret != 0) {
96 fprintf(stderr, "Error opening environment: %s\n",
97 db_strerror(ret));
98 goto err;
99 }
100
101 err:
102 /* Close the environment */
103 if (envp != NULL) {
104 ret_c = envp-&gt;close(envp, 0);
105 if (ret_c != 0) {
106 fprintf(stderr, "environment close failed: %s\n",
107 db_strerror(ret_c));
108 ret = ret_c;
109 }
110 }
111
112 return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
113 } </pre>
114 <p>
115 You then create and open your database(s) as you would for a non-transactional system.
116 <span>
117 The only difference is that you must pass the environment handle to
118 the
119 <span>
120 <code class="function">db_create()</code> function,
121 </span>
122
123 and you must open the database within a transaction.
124 Typically auto commit is used for this purpose. To do so, pass
125 <code class="literal">DB_AUTO_COMMIT</code> to the database open command.
126 It is recommended that you close all your databases before you close
127 your environment.
128 For example:
129 </span>
130
131
132 </p>
133 <pre class="programlisting">#include &lt;stdio.h&gt;
134 #include &lt;stdlib.h&gt;
135
136 #include "db.h"
137
138 int
139 main(void)
140 {
141 int ret, ret_c;
142 u_int32_t <strong class="userinput"><code>db_flags,</code></strong> env_flags;
143 <strong class="userinput"><code>DB *dbp;</code></strong>
144 DB_ENV *envp;
145 const char *db_home_dir = "/tmp/myEnvironment";
146 <strong class="userinput"><code>const char *file_name = "mydb.db";
147
148 dbp = NULL;</code></strong>
149 envp = NULL;
150
151 /* Open the environment */
152 ret = db_env_create(&amp;envp, 0);
153 if (ret != 0) {
154 fprintf(stderr, "Error creating environment handle: %s\n",
155 db_strerror(ret));
156 return (EXIT_FAILURE);
157 }
158
159 env_flags = DB_CREATE | /* Create the environment if it does
160 * not already exist. */
161 DB_INIT_TXN | /* Initialize transactions */
162 DB_INIT_LOCK | /* Initialize locking. */
163 DB_INIT_LOG | /* Initialize logging */
164 DB_INIT_MPOOL; /* Initialize the in-memory cache. */
165
166 ret = envp-&gt;open(envp, db_home_dir, env_flags, 0);
167 if (ret != 0) {
168 fprintf(stderr, "Error opening environment: %s\n",
169 db_strerror(ret));
170 goto err;
171 }
172
173 <strong class="userinput"><code>/* Initialize the DB handle */
174 ret = db_create(&amp;dbp, envp, 0);
175 if (ret != 0) {
176 envp-&gt;err(envp, ret, "Database creation failed");
177 goto err;
178 }
179
180 db_flags = DB_CREATE | DB_AUTO_COMMIT;
181 ret = dbp-&gt;open(dbp, /* Pointer to the database */
182 NULL, /* Txn pointer */
183 file_name, /* File name */
184 NULL, /* Logical db name */
185 DB_BTREE, /* Database type (using btree) */
186 db_flags, /* Open flags */
187 0); /* File mode. Using defaults */
188 if (ret != 0) {
189 envp-&gt;err(envp, ret, "Database '%s' open failed",
190 file_name);
191 goto err;
192 }</code></strong>
193
194
195 err:
196 <strong class="userinput"><code>/* 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 }</code></strong>
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 class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
219 <h3 class="title">Note</h3>
220 <p>
221 Never close a database that has active transactions. Make sure
222 all transactions are resolved (either committed or aborted)
223 before closing the database.
224 </p>
225 </div>
226 </div>
227 <div class="navfooter">
228 <hr />
229 <table width="100%" summary="Navigation footer">
230 <tr>
231 <td width="40%" align="left"><a accesskey="p" href="enabletxn.html">Prev</a> </td>
232 <td width="20%" align="center">
233 <a accesskey="u" href="enabletxn.html">Up</a>
234 </td>
235 <td width="40%" align="right"> <a accesskey="n" href="usingtxns.html">Next</a></td>
236 </tr>
237 <tr>
238 <td width="40%" align="left" valign="top">Chapter 2. Enabling Transactions </td>
239 <td width="20%" align="center">
240 <a accesskey="h" href="index.html">Home</a>
241 </td>
242 <td width="40%" align="right" valign="top"> Chapter 3. Transaction Basics</td>
243 </tr>
244 </table>
245 </div>
246 </body>
247 </html>