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>MyDbEnv</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="dpl_example.html" title="Chapter��6.��A DPL Example" />
11    <link rel="prev" href="inventoryclass.html" title="Inventory.java" />
12    <link rel="next" href="dataaccessorclass.html" title="DataAccessor.java" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">MyDbEnv</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="inventoryclass.html">Prev</a>��</td>
22          <th width="60%" align="center">Chapter��6.��A DPL Example</th>
23          <td width="20%" align="right">��<a accesskey="n" href="dataaccessorclass.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="mydbenv-persist"></a>MyDbEnv</h2>
33          </div>
34        </div>
35      </div>
36      <p>
37                The applications that we are building for our example both
38                must open and close environments and entity stores. One
39                of our applications is writing to the entity store, so this
40                application needs to open the store as read-write. It also
41                wants to be able to create the store if it does not exist.
42            </p>
43      <p>
44                Our second application only reads from the store. In this
45                case, the store should be opened as read-only.
46            </p>
47      <p>
48                We perform these activities by creating a single class that
49                is responsible for opening and closing our store and
50                environment. This class is shared by both our applications.
51                To use it, callers need to only provide the path to the
52                environment home directory, and to indicate whether the
53                object is meant to be read-only. The class implementation
54                is as follows:
55            </p>
56      <pre class="programlisting">package persist.gettingStarted;
57
58import java.io.File;
59import java.io.FileNotFoundException;
60
61import com.sleepycat.db.DatabaseException;
62import com.sleepycat.db.Environment;
63import com.sleepycat.db.EnvironmentConfig;
64
65import com.sleepycat.persist.EntityStore;
66import com.sleepycat.persist.StoreConfig;
67
68public class MyDbEnv {
69
70    private Environment myEnv;
71    private EntityStore store;
72
73    // Our constructor does nothing
74    public MyDbEnv() {}
75
76    // The setup() method opens the environment and store
77    // for us.
78    public void setup(File envHome, boolean readOnly)
79        throws DatabaseException {
80
81        EnvironmentConfig myEnvConfig = new EnvironmentConfig();
82        StoreConfig storeConfig = new StoreConfig();
83
84        myEnvConfig.setReadOnly(readOnly);
85        storeConfig.setReadOnly(readOnly);
86
87        // If the environment is opened for write, then we want to be 
88        // able to create the environment and entity store if 
89        // they do not exist.
90        myEnvConfig.setAllowCreate(!readOnly);
91        storeConfig.setAllowCreate(!readOnly);
92
93        try {
94            // Open the environment and entity store
95            myEnv = new Environment(envHome, myEnvConfig);
96            store = new EntityStore(myEnv, "EntityStore", storeConfig);
97        } catch (FileNotFoundException fnfe) {
98            System.err.println("setup(): " + fnfe.toString());
99            System.exit(-1);
100        }
101
102    }
103
104    // Return a handle to the entity store
105    public EntityStore getEntityStore() {
106        return store;
107    }
108
109    // Return a handle to the environment
110    public Environment getEnv() {
111        return myEnv;
112    }
113
114    // Close the store and environment.
115    public void close() {
116        if (store != null) {
117            try {
118                store.close();
119            } catch(DatabaseException dbe) {
120                System.err.println("Error closing store: " +
121                                    dbe.toString());
122               System.exit(-1);
123            }
124        }
125
126        if (myEnv != null) {
127            try {
128                // Finally, close the environment.
129                myEnv.close();
130            } catch(DatabaseException dbe) {
131                System.err.println("Error closing MyDbEnv: " +
132                                    dbe.toString());
133               System.exit(-1);
134            }
135        }
136    }
137} </pre>
138    </div>
139    <div class="navfooter">
140      <hr />
141      <table width="100%" summary="Navigation footer">
142        <tr>
143          <td width="40%" align="left"><a accesskey="p" href="inventoryclass.html">Prev</a>��</td>
144          <td width="20%" align="center">
145            <a accesskey="u" href="dpl_example.html">Up</a>
146          </td>
147          <td width="40%" align="right">��<a accesskey="n" href="dataaccessorclass.html">Next</a></td>
148        </tr>
149        <tr>
150          <td width="40%" align="left" valign="top">Inventory.java��</td>
151          <td width="20%" align="center">
152            <a accesskey="h" href="index.html">Home</a>
153          </td>
154          <td width="40%" align="right" valign="top">��DataAccessor.java</td>
155        </tr>
156      </table>
157    </div>
158  </body>
159</html>
160