| 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>Opening and Closing the Database Environment</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="Berkeley DB Collections Tutorial" /> |
| 10 |
<link rel="up" href="BasicProgram.html" title="Chapter 2. The Basic Program" /> |
| 11 |
<link rel="prev" href="BasicProgram.html" title="Chapter 2. The Basic Program" /> |
| 12 |
<link rel="next" href="openclasscatalog.html" title="Opening and Closing the Class Catalog" /> |
| 13 |
</head> |
| 14 |
<body> |
| 15 |
<div xmlns="" class="navheader"> |
| 16 |
<div class="libver"> |
| 17 |
<p>Library Version 11.2.5.3</p> |
| 18 |
</div> |
| 19 |
<table width="100%" summary="Navigation header"> |
| 20 |
<tr> |
| 21 |
<th colspan="3" align="center"> |
| 22 |
Opening and Closing the Database Environment |
| 23 |
</th> |
| 24 |
</tr> |
| 25 |
<tr> |
| 26 |
<td width="20%" align="left"><a accesskey="p" href="BasicProgram.html">Prev</a> </td> |
| 27 |
<th width="60%" align="center">Chapter 2. |
| 28 |
The Basic Program |
| 29 |
</th> |
| 30 |
<td width="20%" align="right"> <a accesskey="n" href="openclasscatalog.html">Next</a></td> |
| 31 |
</tr> |
| 32 |
</table> |
| 33 |
<hr /> |
| 34 |
</div> |
| 35 |
<div class="sect1" lang="en" xml:lang="en"> |
| 36 |
<div class="titlepage"> |
| 37 |
<div> |
| 38 |
<div> |
| 39 |
<h2 class="title" style="clear: both"><a id="opendbenvironment"></a> |
| 40 |
Opening and Closing the Database Environment |
| 41 |
</h2> |
| 42 |
</div> |
| 43 |
</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 <code class="classname">SampleDatabase</code> 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 <code class="classname">close()</code> method is used |
| 57 |
to close the environment. The skeleton for the |
| 58 |
<code class="classname">SampleDatabase</code> class follows. |
| 59 |
</p> |
| 60 |
<a id="cb_java_sampledatabase"></a> |
| 61 |
<pre class="programlisting"><strong class="userinput"><code>import com.sleepycat.db.DatabaseException; |
| 62 |
import com.sleepycat.db.Environment; |
| 63 |
import com.sleepycat.db.EnvironmentConfig; |
| 64 |
import java.io.File; |
| 65 |
import java.io.FileNotFoundException; |
| 66 |
|
| 67 |
public 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 |
}</code></strong> </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 class="ulink" 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 |
<strong class="userinput"><code> 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);</code></strong> |
| 119 |
} </pre> |
| 120 |
<p> |
| 121 |
The |
| 122 |
|
| 123 |
<a class="ulink" 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 — <code class="methodname">setTransactional()</code> — |
| 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 |
<code class="methodname">setAllowCreate()</code> 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 <code class="classname">Environment</code> 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 |
must be closed when database work is completed to free |
| 149 |
allocated resources and to avoid having to run recovery later. |
| 150 |
It is recommended that databases are closed before closing the |
| 151 |
environment. |
| 152 |
</p> |
| 153 |
<a id="cb_close"></a> |
| 154 |
<pre class="programlisting"> public void close() |
| 155 |
throws DatabaseException |
| 156 |
{ |
| 157 |
<strong class="userinput"><code> env.close();</code></strong> |
| 158 |
} </pre> |
| 159 |
<p> |
| 160 |
The following getter method returns the environment for use by |
| 161 |
other classes in the example program. The environment is used for |
| 162 |
opening databases and running transactions. |
| 163 |
</p> |
| 164 |
<a id="cb_getenvironment"></a> |
| 165 |
<pre class="programlisting">public class SampleDatabase |
| 166 |
{ |
| 167 |
... |
| 168 |
<strong class="userinput"><code> public final Environment getEnvironment() |
| 169 |
{ |
| 170 |
return env; |
| 171 |
}</code></strong> |
| 172 |
... |
| 173 |
} </pre> |
| 174 |
</div> |
| 175 |
<div class="navfooter"> |
| 176 |
<hr /> |
| 177 |
<table width="100%" summary="Navigation footer"> |
| 178 |
<tr> |
| 179 |
<td width="40%" align="left"><a accesskey="p" href="BasicProgram.html">Prev</a> </td> |
| 180 |
<td width="20%" align="center"> |
| 181 |
<a accesskey="u" href="BasicProgram.html">Up</a> |
| 182 |
</td> |
| 183 |
<td width="40%" align="right"> <a accesskey="n" href="openclasscatalog.html">Next</a></td> |
| 184 |
</tr> |
| 185 |
<tr> |
| 186 |
<td width="40%" align="left" valign="top">Chapter 2. |
| 187 |
The Basic Program |
| 188 |
</td> |
| 189 |
<td width="20%" align="center"> |
| 190 |
<a accesskey="h" href="index.html">Home</a> |
| 191 |
</td> |
| 192 |
<td width="40%" align="right" valign="top"> |
| 193 |
Opening and Closing the Class Catalog |
| 194 |
</td> |
| 195 |
</tr> |
| 196 |
</table> |
| 197 |
</div> |
| 198 |
</body> |
| 199 |
</html> |