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> 7 Opening and Closing the Database Environment 8 </title> 9 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 10 <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> 11 <link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" /> 12 <link rel="up" href="BasicProgram.html" title="Chapter 2. 		The Basic Program 	" /> 13 <link rel="previous" href="BasicProgram.html" title="Chapter 2. 		The Basic Program 	" /> 14 <link rel="next" href="openclasscatalog.html" title=" 		Opening and Closing the Class Catalog 	" /> 15 </head> 16 <body> 17 <div class="navheader"> 18 <table width="100%" summary="Navigation header"> 19 <tr> 20 <th colspan="3" align="center"> 21 Opening and Closing the Database Environment 22 </th> 23 </tr> 24 <tr> 25 <td width="20%" align="left"><a accesskey="p" href="BasicProgram.html">Prev</a> </td> 26 <th width="60%" align="center">Chapter 2. 27 The Basic Program 28 </th> 29 <td width="20%" align="right"> <a accesskey="n" href="openclasscatalog.html">Next</a></td> 30 </tr> 31 </table> 32 <hr /> 33 </div> 34 <div class="sect1" lang="en" xml:lang="en"> 35 <div class="titlepage"> 36 <div> 37 <div> 38 <h2 class="title" style="clear: both"><a id="opendbenvironment"></a> 39 Opening and Closing the Database Environment 40 </h2> 41 </div> 42 </div> 43 <div></div> 44 </div> 45 <p> 46 This section of the tutorial describes how to open and close the 47 database environment. The database environment manages resources 48 (for example, memory, locks and transactions) for any number of 49 databases. A single environment instance is normally used for all 50 databases. 51</p> 52 <p> 53 The <tt class="classname">SampleDatabase</tt> class is used to open and close the 54 environment. It will also be used in following sections to open and 55 close the class catalog and other databases. Its constructor is 56 used to open the environment and its <tt class="classname">close()</tt> method is used 57 to close the environment. The skeleton for the 58 <tt class="classname">SampleDatabase</tt> class follows. 59</p> 60 <a id="cb_java_sampledatabase"></a> 61 <pre class="programlisting"><b class="userinput"><tt>import com.sleepycat.db.DatabaseException; 62import com.sleepycat.db.Environment; 63import com.sleepycat.db.EnvironmentConfig; 64import java.io.File; 65import java.io.FileNotFoundException; 66 67public class SampleDatabase 68{ 69 private Environment env; 70 71 public SampleDatabase(String homeDirectory) 72 throws DatabaseException, FileNotFoundException 73 { 74 } 75 76 public void close() 77 throws DatabaseException 78 { 79 } 80}</tt></b> </pre> 81 <p> 82 The first thing to notice is that the Environment class is in 83 the 84 85 <span>com.sleepycat.db</span> 86 package, not the com.sleepycat.collections 87 package. The 88 89 <span>com.sleepycat.db</span> 90 package contains all core Berkeley DB 91 functionality. The com.sleepycat.collections package contains 92 extended functionality that is based on the Java Collections API. 93 The collections package is layered on top of the 94 95 <span>com.sleepycat.db</span> 96 package. Both packages are needed to create a complete application 97 based on the DB Java Collections API. 98</p> 99 <p> 100 The following statements create an 101 102 <a href="/java/com/sleepycat/db/Environment.html" target="_top">Environment</a> 103 104 object. 105</p> 106 <a id="cb_java_sampledatabaseconstructor"></a> 107 <pre class="programlisting">public SampleDatabase(String homeDirectory) 108 throws DatabaseException, FileNotFoundException 109 { 110<b class="userinput"><tt> System.out.println("Opening environment in: " + homeDirectory); 111 112 EnvironmentConfig envConfig = new EnvironmentConfig(); 113 envConfig.setTransactional(true); 114 envConfig.setAllowCreate(true); 115 envConfig.setInitializeCache(true); 116 envConfig.setInitializeLocking(true); 117 118 env = new Environment(new File(homeDirectory), envConfig);</tt></b> 119 } </pre> 120 <p> 121 The 122 123 <a href="/java/com/sleepycat/db/EnvironmentConfig.html" target="_top">EnvironmentConfig</a> 124 125 class is used to specify environment configuration parameters. The 126 first configuration option specified — <tt class="methodname">setTransactional()</tt> — 127 is set to true to create an environment where transactional (and 128 non-transactional) databases may be opened. While non-transactional 129 environments can also be created, the examples in this tutorial use 130 a transactional environment. 131</p> 132 <p> 133 <tt class="methodname">setAllowCreate()</tt> is set to true to specify 134 that the environment's files will be created if they don't already 135 exist. If this parameter is not specified, an exception will be 136 thrown if the environment does not already exist. A similar 137 parameter will be used later to cause databases to be created if 138 they don't exist. 139</p> 140 <p> 141 When an <tt class="classname">Environment</tt> object is constructed, a home 142 directory and the environment configuration object are specified. 143 The home directory is the location of the environment's log files 144 that store all database information. 145</p> 146 <p> 147 The following statement closes the environment. The environment 148 should always be closed when database work is completed to free 149 allocated resources and to avoid having to run recovery later. 150 Closing the environment does not automatically close databases, so 151 databases should be closed explicitly before closing the 152 environment. 153</p> 154 <a id="cb_close"></a> 155 <pre class="programlisting"> public void close() 156 throws DatabaseException 157 { 158<b class="userinput"><tt> env.close();</tt></b> 159 } </pre> 160 <p> 161 The following getter method returns the environment for use by 162 other classes in the example program. The environment is used for 163 opening databases and running transactions. 164</p> 165 <a id="cb_getenvironment"></a> 166 <pre class="programlisting">public class SampleDatabase 167{ 168 ... 169<b class="userinput"><tt> public final Environment getEnvironment() 170 { 171 return env; 172 }</tt></b> 173 ... 174} </pre> 175 </div> 176 <div class="navfooter"> 177 <hr /> 178 <table width="100%" summary="Navigation footer"> 179 <tr> 180 <td width="40%" align="left"><a accesskey="p" href="BasicProgram.html">Prev</a> </td> 181 <td width="20%" align="center"> 182 <a accesskey="u" href="BasicProgram.html">Up</a> 183 </td> 184 <td width="40%" align="right"> <a accesskey="n" href="openclasscatalog.html">Next</a></td> 185 </tr> 186 <tr> 187 <td width="40%" align="left" valign="top">Chapter 2. 188 The Basic Program 189 </td> 190 <td width="20%" align="center"> 191 <a accesskey="h" href="index.html">Home</a> 192 </td> 193 <td width="40%" align="right" valign="top"> 194 Opening and Closing the Class Catalog 195 </td> 196 </tr> 197 </table> 198 </div> 199 </body> 200</html> 201