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>Managing Databases in 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="DB.html" title="Chapter��7.��Databases" />
11    <link rel="prev" href="dbErrorReporting.html" title="Error Reporting Functions" />
12    <link rel="next" href="CoreJavaUsage.html" title="Database Example" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Managing Databases in Environments</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="dbErrorReporting.html">Prev</a>��</td>
22          <th width="60%" align="center">Chapter��7.��Databases</th>
23          <td width="20%" align="right">��<a accesskey="n" href="CoreJavaUsage.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="sect1" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title" style="clear: both"><a id="CoreEnvUsage"></a>Managing Databases in Environments</h2>
33          </div>
34        </div>
35      </div>
36      <p>
37            In 
38            
39            <span>
40            <a class="xref" href="Env.html" title="Chapter��2.��Database Environments">Database Environments</a>, 
41            </span>
42            
43            we introduced
44        environments. While environments are not used in the example built in this book,
45        they are so commonly used for a wide class of DB applications that it is 
46        necessary to show their basic usage, if only from a completeness perspective.
47    </p>
48      <p>
49        To use an environment, you must first
50            
51        open it. At open time, you must identify the directory in 
52        which it resides. This directory must exist prior to the open attempt. 
53        You can also identify open properties, such as whether the environment can be
54        created if it does not already exist.
55    </p>
56      <p>
57        You will also need to initialize the in-memory cache when you open your environment.
58    </p>
59      <p>
60        For example, to 
61            <span>create an environment handle and</span>
62        open an environment:
63    </p>
64      <a id="java_env1"></a>
65      <pre class="programlisting">package db.GettingStarted;
66
67import com.sleepycat.db.DatabaseException;
68import com.sleepycat.db.Environment;
69import com.sleepycat.db.EnvironmentConfig;
70
71import java.io.File;
72import java.io.FileNotFoundException;
73
74...
75
76Environment myEnv = null;
77File envHome = new File("/export1/testEnv");
78try {
79    EnvironmentConfig envConf = new EnvironmentConfig();
80    envConf.setAllowCreate(true);         // If the environment does not
81                                          // exist, create it.
82    envConf.setInitializeCache(true);     // Initialize the in-memory
83                                          // cache.
84
85    myEnv = new Environment(envHome, envConf);
86} catch (DatabaseException de) {
87    // Exception handling goes here
88} catch (FileNotFoundException fnfe) {
89    // Exception handling goes here
90} </pre>
91      <p>
92        Once an environment is opened, you can open databases in it. Note that by default databases
93        are stored in the environment's home directory, or relative to that directory if you
94        provide any sort of a path in the database's file name:
95    </p>
96      <a id="java_env2"></a>
97      <pre class="programlisting">package db.GettingStarted;
98
99<strong class="userinput"><code>import com.sleepycat.db.Database;
100import com.sleepycat.db.DatabaseConfig;
101import com.sleepycat.db.DatabaseType;</code></strong>
102import com.sleepycat.db.DatabaseException;
103import com.sleepycat.db.Environment;
104import com.sleepycat.db.EnvironmentConfig;
105
106import java.io.File;
107import java.io.FileNotFoundException;
108
109...
110
111Environment myEnv = null;
112Database myDb = null;
113File envHome = new File("/export1/testEnv");
114String dbFileName = new String("mydb.db", "UTF-8");
115
116try {
117    EnvironmentConfig envConf = new EnvironmentConfig();
118    envConf.setAllowCreate(true);
119    DatabaseConfig dbConfig = new DatabaseConfig();
120    <strong class="userinput"><code>dbConfig.setAllowCreate(true);
121    dbConfig.setType(DatabaseType.BTREE);</code></strong>
122
123    myEnv = new Environment(envHome, envConf);
124    <strong class="userinput"><code>myDb = myEnv.openDatabase(null, dbFileName, null, dbConfig);</code></strong>
125} catch (DatabaseException de) {
126    // Exception handling goes here
127} catch (FileNotFoundException fnfe) {
128    // Exception handling goes here
129} </pre>
130      <p>
131        When you are done with an environment, you must close it. Before you close an environment,
132        make sure you close any opened databases.
133    </p>
134      <a id="java_env3"></a>
135      <pre class="programlisting">finally {
136    try {
137        if (myDb != null) {
138            myDb.close();
139        }
140
141        if (myEnv != null) {
142            myEnv.close();
143        }
144    } catch (DatabaseException de) {
145        // Exception handling goes here
146    }
147} </pre>
148    </div>
149    <div class="navfooter">
150      <hr />
151      <table width="100%" summary="Navigation footer">
152        <tr>
153          <td width="40%" align="left"><a accesskey="p" href="dbErrorReporting.html">Prev</a>��</td>
154          <td width="20%" align="center">
155            <a accesskey="u" href="DB.html">Up</a>
156          </td>
157          <td width="40%" align="right">��<a accesskey="n" href="CoreJavaUsage.html">Next</a></td>
158        </tr>
159        <tr>
160          <td width="40%" align="left" valign="top">Error Reporting Functions��</td>
161          <td width="20%" align="center">
162            <a accesskey="h" href="index.html">Home</a>
163          </td>
164          <td width="40%" align="right" valign="top">��Database Example</td>
165        </tr>
166      </table>
167    </div>
168  </body>
169</html>
170