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��3.��Direct Persistence Layer First Steps</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.html" title="Part��I.��Programming with the Direct Persistence Layer" />
11    <link rel="previous" href="dpl.html" title="Part��I.��Programming with the Direct Persistence Layer" />
12    <link rel="next" href="persistobject.html" title="Persistent Objects" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Chapter��3.��Direct Persistence Layer First Steps</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="dpl.html">Prev</a>��</td>
22          <th width="60%" align="center">Part��I.��Programming with the Direct Persistence Layer</th>
23          <td width="20%" align="right">��<a accesskey="n" href="persistobject.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="persist_first"></a>Chapter��3.��Direct Persistence Layer First Steps</h2>
33          </div>
34        </div>
35        <div></div>
36      </div>
37      <div class="toc">
38        <p>
39          <b>Table of Contents</b>
40        </p>
41        <dl>
42          <dt>
43            <span class="sect1">
44              <a href="persist_first.html#entitystore">Entity Stores</a>
45            </span>
46          </dt>
47          <dd>
48            <dl>
49              <dt>
50                <span class="sect2">
51                  <a href="persist_first.html#persist-open">Opening and Closing Environments and Stores</a>
52                </span>
53              </dt>
54            </dl>
55          </dd>
56          <dt>
57            <span class="sect1">
58              <a href="persistobject.html">Persistent Objects</a>
59            </span>
60          </dt>
61          <dt>
62            <span class="sect1">
63              <a href="saveret.html">Saving a Retrieving Data</a>
64            </span>
65          </dt>
66        </dl>
67      </div>
68      <p>
69          This chapter guides you through the first few steps required to
70          use the DPL with your application. These steps include:
71  </p>
72      <div class="orderedlist">
73        <ol type="1">
74          <li>
75            <p>
76                          Opening your environment as was described in
77                          
78                          <span>
79                          <a href="Env.html#EnvOpen">Opening Database Environments</a>.
80                          </span>
81                  </p>
82          </li>
83          <li>
84            <p>
85                          Opening your entity store.
86                  </p>
87          </li>
88          <li>
89            <p>
90                          Identifying the classes that you want to store in
91                          DB as either a <tt class="literal">persistent</tt>
92                          class or an <tt class="literal">entity</tt>.
93                  </p>
94          </li>
95        </ol>
96      </div>
97      <p>
98          Once you have done these things, you can write your classes to
99          the DB databases, read them back from the databases, delete
100          them from the databases, and so forth. These activities are
101          described in the chapters that follow in this part of this manual.
102  </p>
103      <div class="sect1" lang="en" xml:lang="en">
104        <div class="titlepage">
105          <div>
106            <div>
107              <h2 class="title" style="clear: both"><a id="entitystore"></a>Entity Stores</h2>
108            </div>
109          </div>
110          <div></div>
111        </div>
112        <p>
113                Entity stores are the basic unit of storage that you use with the DPL. That is, it
114                is a unit of encapsulation for the classes that you want to store in DB. Under
115                the hood it actually interacts with DB databases, but the DPL provides a layer
116                of abstraction from the underlying DB APIs. The store, therefore, provides a
117                simplified mechanism by which you read and write your stored classes. By using a
118                store, you have access to your classes that is more simplified than if you were
119                interacting with databases directly, but this simplified access comes at the cost of
120                reduced flexibility.
121          </p>
122        <p>
123                  Entity stores have configurations in the same way that environments have
124                  configurations. You can use a <tt class="classname">StoreConfig</tt> object 
125                  to identify store properties. Among these are methods that allow you to declare
126                  whether:
127          </p>
128        <div class="itemizedlist">
129          <ul type="disc">
130            <li>
131              <p>
132                                  the store can be created if it does not exist at the time
133                                  it is opened. Use the
134                                  <tt class="methodname">StoreConfig.setAllowCreate()</tt>
135                                  method to set this.
136                          </p>
137            </li>
138            <li>
139              <p>
140                                  deferred writes are allowed for the store. Use the
141                                  <tt class="methodname">StoreConfig.setDeferredWrite()</tt>
142                                  method to set this.
143                          </p>
144            </li>
145            <li>
146              <p>
147                                  the store is read-only. Use the
148                                  <tt class="methodname">StoreConfig.setReadOnly()</tt>
149                                  method to set this.
150                          </p>
151            </li>
152            <li>
153              <p>
154                                  the store supports transactions. Use the
155                                  <tt class="methodname">StoreConfig.setTransactional()</tt>
156                                  method to set this.
157                          </p>
158              <p>
159                                  Writing DB transactional applications is described in  the
160                                  <i class="citetitle">Berkeley DB Java Edition Getting Started with Transaction Processing</i> guide.
161                          </p>
162            </li>
163          </ul>
164        </div>
165        <p>
166                  <tt class="classname">EntityStore</tt> objects also provide methods for retrieving
167                  information about the store, such as:
168          </p>
169        <div class="itemizedlist">
170          <ul type="disc">
171            <li>
172              <p>
173                                  the store's name. Use the
174                                  <tt class="methodname">EntityStore.getStoreName()</tt>
175                                  method to retrieve this.
176                          </p>
177            </li>
178            <li>
179              <p>
180                                  a handle to the environment in which the store is opened. Use the
181                                  <tt class="methodname">EntityStore.getEnvironment</tt>
182                                  method to retrieve this handle.
183                          </p>
184            </li>
185          </ul>
186        </div>
187        <p>
188                  You can also use the <tt class="classname">EntityStore</tt> to
189                  retrieve all the primary and secondary indexes related to a given type of entity
190                  object contained in the store. See <a href="persist_index.html">Working with Indices</a> for
191                  more information.
192          </p>
193        <div class="sect2" lang="en" xml:lang="en">
194          <div class="titlepage">
195            <div>
196              <div>
197                <h3 class="title"><a id="persist-open"></a>Opening and Closing Environments and Stores</h3>
198              </div>
199            </div>
200            <div></div>
201          </div>
202          <p>
203                  As described in
204                  
205                  <span>
206                          <a href="Env.html">Database Environments</a>, 
207                  </span>
208                          
209                  an
210                  <span class="emphasis"><em>environment</em></span> is a unit of
211                  encapsulation for DB databases. It also provides a
212                  handle by which activities common across the databases
213                  can be managed.
214          </p>
215          <p>
216                  To use an entity store, you must first open an environment and then provide that
217                  environment handle to the <tt class="classname">EntityStore</tt> constructor. 
218          </p>
219          <p>
220                  For example, the following code fragment configures both
221                  the environment and the entity store such that they can
222                  be created if they do not exist. Both the environment and
223                  the entity store are then opened.
224          </p>
225          <pre class="programlisting">package persist.gettingStarted;
226
227import java.io.File;
228import java.io.FileNotFoundException;
229
230import com.sleepycat.db.DatabaseException;
231import com.sleepycat.db.Environment;
232import com.sleepycat.db.EnvironmentConfig;
233
234import com.sleepycat.persist.EntityStore;
235import com.sleepycat.persist.StoreConfig;
236
237...
238
239private Environment myEnv;
240private EntityStore store;
241
242try {
243    EnvironmentConfig myEnvConfig = new EnvironmentConfig();
244    StoreConfig storeConfig = new StoreConfig();
245
246    myEnvConfig.setAllowCreate(!readOnly);
247    storeConfig.setAllowCreate(!readOnly);
248
249    try {
250        // Open the environment and entity store
251        myEnv = new Environment(envHome, myEnvConfig);
252        store = new EntityStore(myEnv, "EntityStore", storeConfig);
253    } catch (FileNotFoundException fnfe) {
254        System.err.println(fnfe.toString());
255        System.exit(-1);
256    }
257} catch(DatabaseException dbe) {
258    System.err.println("Error opening environment and store: " +
259                        dbe.toString());
260    System.exit(-1);
261} </pre>
262          <p>
263            As always, before you exit your program you should close both
264            your store and your environment. Be sure to close your store before you close your
265            environment. 
266    </p>
267          <pre class="programlisting">if (store != null) {
268    try {
269        store.close();
270    } catch(DatabaseException dbe) {
271        System.err.println("Error closing store: " +
272                            dbe.toString());
273        System.exit(-1);
274    }
275}
276
277if (myEnv != null) {
278    try {
279        // Finally, close environment.
280        myEnv.close();
281    } catch(DatabaseException dbe) {
282        System.err.println("Error closing MyDbEnv: " +
283                            dbe.toString());
284        System.exit(-1);
285    }
286} </pre>
287        </div>
288      </div>
289    </div>
290    <div class="navfooter">
291      <hr />
292      <table width="100%" summary="Navigation footer">
293        <tr>
294          <td width="40%" align="left"><a accesskey="p" href="dpl.html">Prev</a>��</td>
295          <td width="20%" align="center">
296            <a accesskey="u" href="dpl.html">Up</a>
297          </td>
298          <td width="40%" align="right">��<a accesskey="n" href="persistobject.html">Next</a></td>
299        </tr>
300        <tr>
301          <td width="40%" align="left" valign="top">Part��I.��Programming with the Direct Persistence Layer��</td>
302          <td width="20%" align="center">
303            <a accesskey="h" href="index.html">Home</a>
304          </td>
305          <td width="40%" align="right" valign="top">��Persistent Objects</td>
306        </tr>
307      </table>
308    </div>
309  </body>
310</html>
311