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>Chapter��2.��Database Environments</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" />
10    <link rel="up" href="index.html" title="Getting Started with Berkeley DB" />
11    <link rel="prev" href="gettingit.html" title="Getting and Using DB" />
12    <link rel="next" href="EnvClose.html" title="Closing Database Environments" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Chapter��2.��Database Environments</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="gettingit.html">Prev</a>��</td>
22          <th width="60%" align="center">��</th>
23          <td width="20%" align="right">��<a accesskey="n" href="EnvClose.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="chapter" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title"><a id="Env"></a>Chapter��2.��Database Environments</h2>
33          </div>
34        </div>
35      </div>
36      <div class="toc">
37        <p>
38          <b>Table of Contents</b>
39        </p>
40        <dl>
41          <dt>
42            <span class="sect1">
43              <a href="Env.html#EnvOpen">Opening Database Environments</a>
44            </span>
45          </dt>
46          <dt>
47            <span class="sect1">
48              <a href="EnvClose.html">Closing Database Environments</a>
49            </span>
50          </dt>
51          <dt>
52            <span class="sect1">
53              <a href="EnvProps.html">Environment Properties</a>
54            </span>
55          </dt>
56          <dd>
57            <dl>
58              <dt>
59                <span class="sect2">
60                  <a href="EnvProps.html#envconfig">The EnvironmentConfig Class</a>
61                </span>
62              </dt>
63              <dt>
64                <span class="sect2">
65                  <a href="EnvProps.html#envhandleconfig">EnvironmentMutableConfig</a>
66                </span>
67              </dt>
68            </dl>
69          </dd>
70        </dl>
71      </div>
72      <p>
73    Environments are optional, but very commonly used, for Berkeley DB
74    applications built using the base API. If you are using the DPL, then
75    environments are required.
76  </p>
77      <p>
78  	Database environments encapsulate one or more databases. This encapsulation 
79  	provides your threads with efficient access to your databases by allowing a single
80  	in-memory cache to be used for each of the databases contained in the
81  	environment. This encapsulation also allows you to group operations performed against 
82    multiple databases inside a single
83  	transactions (see the <em class="citetitle">Berkeley DB Java Edition Getting Started with Transaction Processing</em> guide for more information).
84  </p>
85      <p>
86  	Most commonly you use database environments to create and open
87  	databases (you close individual databases using the individual
88  	database handles). You can also use environments to delete and rename
89  	databases. For transactional applications, you use the environment to start
90  	transactions. For non-transactional
91  	applications, you use the environment to sync your in-memory cache to disk.
92 </p>
93      <div class="sect1" lang="en" xml:lang="en">
94        <div class="titlepage">
95          <div>
96            <div>
97              <h2 class="title" style="clear: both"><a id="EnvOpen"></a>Opening Database Environments</h2>
98            </div>
99          </div>
100        </div>
101        <p>You open a database environment by instantiating an
102    <code class="classname">Environment</code> object. You must provide to the
103    constructor the name of the on-disk directory where the environment is to reside.
104    This directory location must exist or the open will fail.</p>
105        <p>By default, the environment is not created for you if it does not exist. Set the 
106    <a class="link" href="EnvProps.html" title="Environment Properties">creation property</a> to <code class="literal">true</code> if
107    you want the environment to be created. For example:</p>
108        <a id="je_env1"></a>
109        <pre class="programlisting">
110    
111import com.sleepycat.je.DatabaseException;
112import com.sleepycat.je.Environment;
113import com.sleepycat.je.EnvironmentConfig;
114
115import java.io.File;
116
117...
118
119// Open the environment. Allow it to be created if it does not already exist.
120Environment myDbEnvironment = null;
121
122try {
123    EnvironmentConfig envConfig = new EnvironmentConfig();
124    envConfig.setAllowCreate(true);
125    myDbEnvironment = new Environment(new File("/export/dbEnv"), envConfig);
126} catch (DatabaseException dbe) {
127    // Exception handling goes here
128} </pre>
129        <pre class="programlisting">package db.gettingStarted;
130    
131import com.sleepycat.db.DatabaseException;
132import com.sleepycat.db.Environment;
133import com.sleepycat.db.EnvironmentConfig;
134
135import java.io.File;
136import java.io.FileNotFoundException;
137
138...
139
140// Open the environment. Allow it to be created if it does not already exist.
141Environment myDbEnvironment = null;
142
143try {
144    EnvironmentConfig envConfig = new EnvironmentConfig();
145    envConfig.setAllowCreate(true);
146    myDbEnvironment = new Environment(new File("/export/dbEnv"), envConfig);
147} catch (DatabaseException dbe) {
148    // Exception handling goes here
149} catch (FileNotFoundException fnfe) {
150// Exception handling goes here
151} </pre>
152        <p>Your application can open and use as many environments as you have
153    disk and memory to manage, although most applications will use just one
154    environment. Also, you can instantiate multiple <code class="classname">Environment</code>
155    objects for the same physical environment.</p>
156      </div>
157    </div>
158    <div class="navfooter">
159      <hr />
160      <table width="100%" summary="Navigation footer">
161        <tr>
162          <td width="40%" align="left"><a accesskey="p" href="gettingit.html">Prev</a>��</td>
163          <td width="20%" align="center">��</td>
164          <td width="40%" align="right">��<a accesskey="n" href="EnvClose.html">Next</a></td>
165        </tr>
166        <tr>
167          <td width="40%" align="left" valign="top">Getting and Using DB ��</td>
168          <td width="20%" align="center">
169            <a accesskey="h" href="index.html">Home</a>
170          </td>
171          <td width="40%" align="right" valign="top">��Closing Database Environments</td>
172        </tr>
173      </table>
174    </div>
175  </body>
176</html>
177