Debian Codegraph @

docs/programmer_reference/transapp_env_open.html

raw symbols SCIP
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 the environment</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="Berkeley DB Programmer's Reference Guide" />
10 <link rel="up" href="transapp.html" title="Chapter 11.  Berkeley DB Transactional Data Store Applications" />
11 <link rel="prev" href="transapp_app.html" title="Architecting Transactional Data Store applications" />
12 <link rel="next" href="transapp_data_open.html" title="Opening the databases" />
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 the environment</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="transapp_app.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 11. 
26 Berkeley DB Transactional Data Store Applications
27 </th>
28 <td width="20%" align="right"> <a accesskey="n" href="transapp_data_open.html">Next</a></td>
29 </tr>
30 </table>
31 <hr />
32 </div>
33 <div class="sect1" lang="en" xml:lang="en">
34 <div class="titlepage">
35 <div>
36 <div>
37 <h2 class="title" style="clear: both"><a id="transapp_env_open"></a>Opening the environment</h2>
38 </div>
39 </div>
40 </div>
41 <p>
42 Creating transaction-protected applications using the Berkeley DB
43 library is quite easy. Applications first use <a href="../api_reference/C/envopen.html" class="olink">DB_ENV-&gt;open()</a> to initialize
44 the database environment. Transaction-protected applications normally
45 require all four Berkeley DB subsystems, so the <a href="../api_reference/C/envopen.html#envopen_DB_INIT_MPOOL" class="olink">DB_INIT_MPOOL</a>,
46 <a href="../api_reference/C/envopen.html#envopen_DB_INIT_LOCK" class="olink">DB_INIT_LOCK</a>, <a href="../api_reference/C/envopen.html#envopen_DB_INIT_LOG" class="olink">DB_INIT_LOG</a>, and <a href="../api_reference/C/envopen.html#envopen_DB_INIT_TXN" class="olink">DB_INIT_TXN</a> flags should be
47 specified.
48 </p>
49 <p>
50 Once the application has called <a href="../api_reference/C/envopen.html" class="olink">DB_ENV-&gt;open()</a>, it opens its databases
51 within the environment. Once the databases are opened, the application
52 makes changes to the databases inside of transactions. Each set of
53 changes that entails a unit of work should be surrounded by the
54 appropriate <a href="../api_reference/C/txnbegin.html" class="olink">DB_ENV-&gt;txn_begin()</a>, <a href="../api_reference/C/txncommit.html" class="olink">DB_TXN-&gt;commit()</a> and <a href="../api_reference/C/txnabort.html" class="olink">DB_TXN-&gt;abort()</a> calls. The Berkeley
55 DB access methods will make the appropriate calls into the Lock, Log
56 and Memory Pool subsystems in order to guarantee transaction semantics.
57 When the application is ready to exit, all outstanding transactions
58 should have been committed or aborted.
59 </p>
60 <p>
61 Databases accessed by a transaction must not be closed during the
62 transaction. Once all outstanding transactions are finished, all open
63 Berkeley DB files should be closed. When the Berkeley DB database
64 files have been closed, the environment should be closed by calling
65 <a href="../api_reference/C/envclose.html" class="olink">DB_ENV-&gt;close()</a>.
66 </p>
67 <p>
68 The following code fragment creates the database environment directory
69 then opens the environment, running recovery. Our <a href="../api_reference/C/env.html" class="olink">DB_ENV</a> database
70 environment handle is declared to be free-threaded using the
71 <a href="../api_reference/C/dbopen.html#open_DB_THREAD" class="olink">DB_THREAD</a> flag, and so may be used by any number of threads that we
72 may subsequently create.
73 </p>
74 <a id="prog_transapp1"></a>
75 <pre class="programlisting">#include &lt;sys/types.h&gt;
76 #include &lt;sys/stat.h&gt;
77
78 #include &lt;errno.h&gt;
79 #include &lt;stdarg.h&gt;
80 #include &lt;stdlib.h&gt;
81 #include &lt;string.h&gt;
82 #include &lt;unistd.h&gt;
83
84 #include &lt;db.h&gt;
85
86 #define ENV_DIRECTORY "TXNAPP"
87
88 void env_dir_create(void);
89 void env_open(DB_ENV **);
90 ...
91
92 int
93 main(int argc, char *argv[])
94 {
95 extern int optind;
96 DB_ENV *dbenv;
97 int ch;
98
99 while ((ch = getopt(argc, argv, "")) != EOF)
100 switch (ch) {
101 case '?':
102 default:
103 usage();
104 }
105 argc -= optind;
106 argv += optind;
107
108 env_dir_create();
109 env_open(&amp;dbenv);
110 ...
111
112 return (0);
113 }
114
115 ...
116
117 void
118 env_dir_create()
119 {
120 struct stat sb;
121
122 /*
123 * If the directory exists, we're done. We do not further check
124 * the type of the file, DB will fail appropriately if it's the
125 * wrong type.
126 */
127 if (stat(ENV_DIRECTORY, &amp;sb) == 0)
128 return;
129
130 /* Create the directory, read/write/access owner only. */
131 if (mkdir(ENV_DIRECTORY, S_IRWXU) != 0) {
132 fprintf(stderr,
133 "txnapp: mkdir: %s: %s\n", ENV_DIRECTORY, strerror(errno));
134 exit (1);
135 }
136 }
137
138 void
139 env_open(DB_ENV **dbenvp)
140 {
141 DB_ENV *dbenv;
142 int ret;
143
144 /* Create the environment handle. */
145 if ((ret = db_env_create(&amp;dbenv, 0)) != 0) {
146 fprintf(stderr,
147 "txnapp: db_env_create: %s\n", db_strerror(ret));
148 exit (1);
149 }
150
151 /* Set up error handling. */
152 dbenv-&gt;set_errpfx(dbenv, "txnapp");
153 dbenv-&gt;set_errfile(dbenv, stderr);
154
155 /*
156 * Open a transactional environment:
157 * create if it doesn't exist
158 * free-threaded handle
159 * run recovery
160 * read/write owner only
161 */
162 if ((ret = dbenv-&gt;open(dbenv, ENV_DIRECTORY,
163 DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG |
164 DB_INIT_MPOOL | DB_INIT_TXN | DB_RECOVER | DB_THREAD,
165 S_IRUSR | S_IWUSR)) != 0) {
166 (void)dbenv-&gt;close(dbenv, 0);
167 fprintf(stderr, "dbenv-&gt;open: %s: %s\n",
168 ENV_DIRECTORY, db_strerror(ret));
169 exit (1);
170 }
171
172 *dbenvp = dbenv;
173 }</pre>
174 <p>
175 After running this initial program, we can use the <a href="../api_reference/C/db_stat.html" class="olink">db_stat</a> utility to display
176 the contents of the environment directory:
177 </p>
178 <pre class="programlisting">prompt&gt; db_stat -e -h TXNAPP
179 3.2.1 Environment version.
180 120897 Magic number.
181 0 Panic value.
182 1 References.
183 6 Locks granted without waiting.
184 0 Locks granted after waiting.
185 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
186 Mpool Region: 4.
187 264KB Size (270336 bytes).
188 -1 Segment ID.
189 1 Locks granted without waiting.
190 0 Locks granted after waiting.
191 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
192 Log Region: 3.
193 96KB Size (98304 bytes).
194 -1 Segment ID.
195 3 Locks granted without waiting.
196 0 Locks granted after waiting.
197 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
198 Lock Region: 2.
199 240KB Size (245760 bytes).
200 -1 Segment ID.
201 1 Locks granted without waiting.
202 0 Locks granted after waiting.
203 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
204 Txn Region: 5.
205 8KB Size (8192 bytes).
206 -1 Segment ID.
207 1 Locks granted without waiting.
208 0 Locks granted after waiting.
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="transapp_app.html">Prev</a> </td>
216 <td width="20%" align="center">
217 <a accesskey="u" href="transapp.html">Up</a>
218 </td>
219 <td width="40%" align="right"> <a accesskey="n" href="transapp_data_open.html">Next</a></td>
220 </tr>
221 <tr>
222 <td width="40%" align="left" valign="top">Architecting Transactional Data Store applications </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"> Opening the databases</td>
227 </tr>
228 </table>
229 </div>
230 </body>
231 </html>