Debian Codegraph @

docs/programmer_reference/env_create.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>Creating a database 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="env.html" title="Chapter 9.  The Berkeley DB Environment" />
11 <link rel="prev" href="env.html" title="Chapter 9.  The Berkeley DB Environment" />
12 <link rel="next" href="env_size.html" title="Sizing a database environment" />
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">Creating a database environment</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="env.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 9. 
26 The Berkeley DB Environment
27 </th>
28 <td width="20%" align="right"> <a accesskey="n" href="env_size.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="env_create"></a>Creating a database environment</h2>
38 </div>
39 </div>
40 </div>
41 <p>The Berkeley DB environment is created and described by the <a href="../api_reference/C/envcreate.html" class="olink">db_env_create()</a>
42 and <a href="../api_reference/C/envopen.html" class="olink">DB_ENV-&gt;open()</a> interfaces. In situations where customization is
43 desired, such as storing log files on a separate disk drive or selection
44 of a particular cache size, applications must describe the customization
45 by either creating an environment configuration file in the environment
46 home directory or by arguments passed to other <a href="../api_reference/C/env.html" class="olink">DB_ENV</a> handle methods.</p>
47 <p>Once an environment has been created, database files specified using
48 relative pathnames will be named relative to the home directory. Using
49 pathnames relative to the home directory allows the entire environment
50 to be easily moved, simplifying restoration and recovery of a database
51 in a different directory or on a different system.</p>
52 <p>Applications first obtain an environment handle using the
53 <a href="../api_reference/C/envcreate.html" class="olink">db_env_create()</a> method, then call the <a href="../api_reference/C/envopen.html" class="olink">DB_ENV-&gt;open()</a> method which creates
54 or joins the database environment. There are a number of options you
55 can set to customize <a href="../api_reference/C/envopen.html" class="olink">DB_ENV-&gt;open()</a> for your environment. These
56 options fall into four broad categories:</p>
57 <div class="variablelist">
58 <dl>
59 <dt>
60 <span class="term">Subsystem Initialization:</span>
61 </dt>
62 <dd>These flags indicate which Berkeley DB subsystems will be initialized for the
63 environment, and what operations will happen automatically when
64 databases are accessed within the environment. The flags include
65 <a href="../api_reference/C/envopen.html#envopen_DB_INIT_CDB" class="olink">DB_INIT_CDB</a>, <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>, <a href="../api_reference/C/envopen.html#envopen_DB_INIT_MPOOL" class="olink">DB_INIT_MPOOL</a>, and <a href="../api_reference/C/envopen.html#envopen_DB_INIT_TXN" class="olink">DB_INIT_TXN</a>. The <a href="../api_reference/C/envopen.html#envopen_DB_INIT_CDB" class="olink">DB_INIT_CDB</a>
66 flag does initialization for Berkeley DB Concurrent Data Store applications. (See
67 <a class="xref" href="cam.html#cam_intro" title="Concurrent Data Store introduction">Concurrent Data Store introduction</a> for more
68 information.) The rest of the flags initialize a single subsystem; that
69 is, when <a href="../api_reference/C/envopen.html#envopen_DB_INIT_LOCK" class="olink">DB_INIT_LOCK</a> is specified, applications reading and
70 writing databases opened in this environment will be using locking to
71 ensure that they do not overwrite each other's changes.</dd>
72 <dt>
73 <span class="term">Recovery options:</span>
74 </dt>
75 <dd>These flags, which include <a href="../api_reference/C/envopen.html#envopen_DB_RECOVER" class="olink">DB_RECOVER</a> and
76 <a href="../api_reference/C/envopen.html#envopen_DB_RECOVER_FATAL" class="olink">DB_RECOVER_FATAL</a>, indicate what recovery is to be performed on
77 the environment before it is opened for normal use.</dd>
78 <dt>
79 <span class="term">Naming options:</span>
80 </dt>
81 <dd>These flags, which include
82 <a href="../api_reference/C/envopen.html#envopen_DB_USE_ENVIRON" class="olink">DB_USE_ENVIRON</a> and <a href="../api_reference/C/envopen.html#envopen_DB_USE_ENVIRON_ROOT" class="olink">DB_USE_ENVIRON_ROOT</a>, modify how file naming happens in the
83 environment.</dd>
84 <dt>
85 <span class="term">Miscellaneous:</span>
86 </dt>
87 <dd>Finally, there are a number of miscellaneous flags, for example,
88 <a href="../api_reference/C/dbopen.html#open_DB_CREATE" class="olink">DB_CREATE</a> which causes underlying files to be created as
89 necessary. See the <a href="../api_reference/C/envopen.html" class="olink">DB_ENV-&gt;open()</a> manual pages for further
90 information.</dd>
91 </dl>
92 </div>
93 <p>Most applications either specify only the <a href="../api_reference/C/envopen.html#envopen_DB_INIT_MPOOL" class="olink">DB_INIT_MPOOL</a> flag or
94 they specify all four subsystem initialization flags
95 (<a href="../api_reference/C/envopen.html#envopen_DB_INIT_MPOOL" class="olink">DB_INIT_MPOOL</a>, <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>).
96 The former configuration is for applications that
97 simply want to use the basic Access Method interfaces with a shared
98 underlying buffer pool, but don't care about recoverability after
99 application or system failure. The latter is for applications that need
100 recoverability. There are situations in which other combinations of
101 the initialization flags make sense, but they are rare.</p>
102 <p>The <a href="../api_reference/C/envopen.html#envopen_DB_RECOVER" class="olink">DB_RECOVER</a> flag is specified by applications that want to perform any necessary database recovery when they start running. That is, if there was a system or application failure the last time they ran, they want the databases to be made consistent before they start running again. It is not an error to specify this flag when no recovery needs to be done.</p>
103 <p>The <a href="../api_reference/C/envopen.html#envopen_DB_RECOVER_FATAL" class="olink">DB_RECOVER_FATAL</a> flag is more special-purpose. It performs catastrophic database recovery, and normally requires that some initial arrangements be made; that is, archived log files be brought back into the filesystem. Applications should not normally specify this flag. Instead, under these rare conditions, the <a href="../api_reference/C/db_recover.html" class="olink">db_recover</a> utility should be used.</p>
104 <p>The following is a simple example of a function that opens a database
105 environment for a transactional program.</p>
106 <a id="prog_env27"></a>
107 <pre class="programlisting">DB_ENV *
108 db_setup(char *home, char *data_dir, FILE *errfp, char *progname)
109 {
110 DB_ENV *dbenv;
111 int ret;
112
113 /*
114 * Create an environment and initialize it for additional error
115 * reporting.
116 */
117 if ((ret = db_env_create(&amp;dbenv, 0)) != 0) {
118 fprintf(errfp, "%s: %s\n", progname, db_strerror(ret));
119 return (NULL);
120 }
121 dbenv-&gt;set_errfile(dbenv, errfp);
122 dbenv-&gt;set_errpfx(dbenv, progname);
123
124 /*
125 * Specify the shared memory buffer pool cachesize: 5MB.
126 * Databases are in a subdirectory of the environment home.
127 */
128 if ((ret = dbenv-&gt;set_cachesize(dbenv, 0,
129 5 * 1024 * 1024, 0)) != 0) {
130 dbenv-&gt;err(dbenv, ret, "set_cachesize");
131 goto err;
132 }
133 if ((ret = dbenv-&gt;set_data_dir(dbenv, data_dir)) != 0) {
134 dbenv-&gt;err(dbenv, ret, "set_data_dir: %s", data_dir);
135 goto err;
136 }
137
138 /* Open the environment with full transactional support. */
139 if ((ret = dbenv-&gt;open(dbenv, home, DB_CREATE | DB_INIT_LOG |
140 DB_INIT_LOCK | DB_INIT_MPOOL |
141 DB_INIT_TXN, 0)) != 0) {
142 dbenv-&gt;err(dbenv, ret, "environment open: %s", home);
143 goto err;
144 }
145
146 return (dbenv);
147
148 err: (void)dbenv-&gt;close(dbenv, 0);
149 return (NULL);
150 }</pre>
151 </div>
152 <div class="navfooter">
153 <hr />
154 <table width="100%" summary="Navigation footer">
155 <tr>
156 <td width="40%" align="left"><a accesskey="p" href="env.html">Prev</a> </td>
157 <td width="20%" align="center">
158 <a accesskey="u" href="env.html">Up</a>
159 </td>
160 <td width="40%" align="right"> <a accesskey="n" href="env_size.html">Next</a></td>
161 </tr>
162 <tr>
163 <td width="40%" align="left" valign="top">Chapter 9. 
164 The Berkeley DB Environment
165  </td>
166 <td width="20%" align="center">
167 <a accesskey="h" href="index.html">Home</a>
168 </td>
169 <td width="40%" align="right" valign="top"> Sizing a database environment</td>
170 </tr>
171 </table>
172 </div>
173 </body>
174 </html>