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