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