2
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
4
<!--See the file LICENSE for redistribution information.-->
7
<title>Berkeley DB Reference Guide: Opening the environment</title>
8
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
9
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
12
<table width="100%"><tr valign=top>
13
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Berkeley DB Transactional Data Store Applications</dl></h3></td>
14
<td align=right><a href="../../ref/transapp/app.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/transapp/data_open.html"><img src="../../images/next.gif" alt="Next"></a>
17
<h1 align=center>Opening the environment</h1>
18
<p>Creating transaction-protected applications using the Berkeley DB library is
19
quite easy. Applications first use <a href="../../api_c/env_open.html">DB_ENV->open</a> to initialize
20
the database environment. Transaction-protected applications normally
21
require all four Berkeley DB subsystems, so the <a href="../../api_c/env_open.html#DB_INIT_MPOOL">DB_INIT_MPOOL</a>,
22
<a href="../../api_c/env_open.html#DB_INIT_LOCK">DB_INIT_LOCK</a>, <a href="../../api_c/env_open.html#DB_INIT_LOG">DB_INIT_LOG</a>, and <a href="../../api_c/env_open.html#DB_INIT_TXN">DB_INIT_TXN</a> flags
24
<p>Once the application has called <a href="../../api_c/env_open.html">DB_ENV->open</a>, it opens its
25
databases within the environment. Once the databases are opened, the
26
application makes changes to the databases inside of transactions. Each
27
set of changes that entails a unit of work should be surrounded by the
28
appropriate <a href="../../api_c/txn_begin.html">DB_ENV->txn_begin</a>, <a href="../../api_c/txn_commit.html">DB_TXN->commit</a>, and <a href="../../api_c/txn_abort.html">DB_TXN->abort</a>
29
calls. The Berkeley DB access methods will make the appropriate calls into
30
the Lock, Log and Memory Pool subsystems in order to guarantee
31
transaction semantics. When the application is ready to exit, all
32
outstanding transactions should have been committed or aborted.
33
<p>Databases accessed by a transaction must not be closed during the
34
transaction. Once all outstanding transactions are finished, all open
35
Berkeley DB files should be closed. When the Berkeley DB database files have been
36
closed, the environment should be closed by calling
37
<a href="../../api_c/env_close.html">DB_ENV->close</a>.
38
<p>The following code fragment creates the database environment directory
39
then opens the environment, running recovery. Our <a href="../../api_c/env_class.html">DB_ENV</a>
40
database environment handle is declared to be free-threaded using the
41
<a href="../../api_c/env_open.html#DB_THREAD">DB_THREAD</a> flag, and so may be used by any number of threads that
42
we may subsequently create.
43
<p><blockquote><pre>#include <sys/types.h>
44
#include <sys/stat.h>
46
#include <errno.h>
47
#include <stdarg.h>
48
#include <stdlib.h>
49
#include <string.h>
50
#include <unistd.h>
54
#define ENV_DIRECTORY "TXNAPP"
56
void env_dir_create(void);
57
void env_open(DB_ENV **);
60
main(int argc, char *argv)
64
DB *db_cats, *db_color, *db_fruit;
68
while ((ch = getopt(argc, argv, "")) != EOF)
89
* If the directory exists, we're done. We do not further check
90
* the type of the file, DB will fail appropriately if it's the
93
if (stat(ENV_DIRECTORY, &sb) == 0)
96
/* Create the directory, read/write/access owner only. */
97
if (mkdir(ENV_DIRECTORY, S_IRWXU) != 0) {
99
"txnapp: mkdir: %s: %s\n", ENV_DIRECTORY, strerror(errno));
105
env_open(DB_ENV **dbenvp)
110
/* Create the environment handle. */
111
if ((ret = db_env_create(&dbenv, 0)) != 0) {
113
"txnapp: db_env_create: %s\n", db_strerror(ret));
117
/* Set up error handling. */
118
dbenv->set_errpfx(dbenv, "txnapp");
121
* Open a transactional environment:
122
* create if it doesn't exist
123
* free-threaded handle
125
* read/write owner only
127
if ((ret = dbenv->open(dbenv, ENV_DIRECTORY,
128
DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG |
129
DB_INIT_MPOOL | DB_INIT_TXN | DB_RECOVER | DB_THREAD,
130
S_IRUSR | S_IWUSR)) != 0) {
131
(void)dbenv->close(dbenv, 0);
132
dbenv->err(dbenv, ret, "dbenv->open: %s", ENV_DIRECTORY);
138
<p>After running this initial program, we can use the <a href="../../utility/db_stat.html">db_stat</a>
139
utility to display the contents of the environment directory:
140
<p><blockquote><pre>prompt> db_stat -e -h TXNAPP
141
3.2.1 Environment version.
145
6 Locks granted without waiting.
146
0 Locks granted after waiting.
147
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
149
264KB Size (270336 bytes).
151
1 Locks granted without waiting.
152
0 Locks granted after waiting.
153
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
155
96KB Size (98304 bytes).
157
3 Locks granted without waiting.
158
0 Locks granted after waiting.
159
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
161
240KB Size (245760 bytes).
163
1 Locks granted without waiting.
164
0 Locks granted after waiting.
165
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
167
8KB Size (8192 bytes).
169
1 Locks granted without waiting.
170
0 Locks granted after waiting.</pre></blockquote>
171
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/transapp/app.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/transapp/data_open.html"><img src="../../images/next.gif" alt="Next"></a>
173
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>