| 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 Class Catalog</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="opendbenvironment.html" title="Opening and Closing the Database Environment" /> |
| 12 |
<link rel="next" href="opendatabases.html" title="Opening and Closing Databases" /> |
| 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 Class Catalog |
| 23 |
</th> |
| 24 |
</tr> |
| 25 |
<tr> |
| 26 |
<td width="20%" align="left"><a accesskey="p" href="opendbenvironment.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="opendatabases.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="openclasscatalog"></a> |
| 40 |
Opening and Closing the Class Catalog |
| 41 |
</h2> |
| 42 |
</div> |
| 43 |
</div> |
| 44 |
</div> |
| 45 |
<p> |
| 46 |
This section describes how to open and close the Java class |
| 47 |
catalog. The class catalog is a specialized database store that |
| 48 |
contains the Java class descriptions of the serialized objects that |
| 49 |
are stored in the database. The class descriptions are stored in |
| 50 |
the catalog rather than storing them redundantly in each database |
| 51 |
record. A single class catalog per environment must be opened |
| 52 |
whenever serialized objects will be stored in the database. |
| 53 |
</p> |
| 54 |
<p> |
| 55 |
The <code class="classname">SampleDatabase</code> class is extended to open and close |
| 56 |
the class catalog. The following additional imports and class |
| 57 |
members are needed. |
| 58 |
</p> |
| 59 |
<a id="cb_java_sampledatabase1"></a> |
| 60 |
<pre class="programlisting"><strong class="userinput"><code>import com.sleepycat.bind.serial.StoredClassCatalog; |
| 61 |
import com.sleepycat.bind.serial.ClassCatalog; |
| 62 |
import com.sleepycat.db.Database; |
| 63 |
import com.sleepycat.db.DatabaseConfig; |
| 64 |
import com.sleepycat.db.DatabaseType;</code></strong> |
| 65 |
import com.sleepycat.db.DatabaseException; |
| 66 |
import com.sleepycat.db.Environment; |
| 67 |
import com.sleepycat.db.EnvironmentConfig; |
| 68 |
import java.io.File; |
| 69 |
import java.io.FileNotFoundException; |
| 70 |
|
| 71 |
... |
| 72 |
|
| 73 |
public class SampleDatabase |
| 74 |
{ |
| 75 |
private Environment env; |
| 76 |
<strong class="userinput"><code> private static final String CLASS_CATALOG = "java_class_catalog";</code></strong> |
| 77 |
... |
| 78 |
<strong class="userinput"><code> private StoredClassCatalog javaCatalog;</code></strong> |
| 79 |
... |
| 80 |
} </pre> |
| 81 |
<p> |
| 82 |
While the class catalog is itself a database, it contains |
| 83 |
metadata for other databases and is therefore treated specially by |
| 84 |
the DB Java Collections API. The |
| 85 |
<a class="ulink" href="../../java/com/sleepycat/bind/serial/StoredClassCatalog.html" target="_top">StoredClassCatalog</a> |
| 86 |
|
| 87 |
class encapsulates the catalog store and implements this special |
| 88 |
behavior. |
| 89 |
</p> |
| 90 |
<p> |
| 91 |
The following statements open the class catalog by creating a |
| 92 |
<code class="classname">Database</code> and a <code class="classname">StoredClassCatalog</code> object. The catalog |
| 93 |
database is created if it does not already exist. |
| 94 |
</p> |
| 95 |
<a id="cb_java_sampledatabase2"></a> |
| 96 |
<pre class="programlisting"> public SampleDatabase(String homeDirectory) |
| 97 |
throws DatabaseException, FileNotFoundException |
| 98 |
{ |
| 99 |
... |
| 100 |
<strong class="userinput"><code> DatabaseConfig dbConfig = new DatabaseConfig(); |
| 101 |
dbConfig.setTransactional(true); |
| 102 |
dbConfig.setAllowCreate(true); |
| 103 |
dbConfig.setType(DatabaseType.BTREE); |
| 104 |
|
| 105 |
Database catalogDb = env.openDatabase(null, CLASS_CATALOG, null, |
| 106 |
dbConfig); |
| 107 |
|
| 108 |
javaCatalog = new StoredClassCatalog(catalogDb);</code></strong> |
| 109 |
... |
| 110 |
} |
| 111 |
... |
| 112 |
<strong class="userinput"><code> public final StoredClassCatalog getClassCatalog() { |
| 113 |
return javaCatalog; |
| 114 |
}</code></strong> </pre> |
| 115 |
<p> |
| 116 |
The |
| 117 |
|
| 118 |
<a class="ulink" href="../../java/com/sleepycat/db/DatabaseConfig.html" target="_top">DatabaseConfig</a> |
| 119 |
|
| 120 |
class is used to specify configuration parameters when opening a |
| 121 |
database. The first configuration option specified — |
| 122 |
<code class="methodname">setTransactional()</code> — is set to true to create a transactional |
| 123 |
database. While non-transactional databases can also be created, |
| 124 |
the examples in this tutorial use transactional databases. |
| 125 |
</p> |
| 126 |
<p> |
| 127 |
<code class="methodname">setAllowCreate()</code> is set to true to specify |
| 128 |
that the database will be created if it does not already exist. If |
| 129 |
this parameter is not specified, an exception will be thrown if the |
| 130 |
database does not already exist. |
| 131 |
</p> |
| 132 |
<p> |
| 133 |
<code class="methodname">setDatabaseType()</code> identifies the database storage |
| 134 |
type or access method. For opening a catalog database, the |
| 135 |
<code class="literal">BTREE</code> type is required. <code class="literal">BTREE</code> is the |
| 136 |
most commonly used database type and in this tutorial is used for all |
| 137 |
databases. |
| 138 |
</p> |
| 139 |
<p> |
| 140 |
The first parameter of the <code class="methodname">openDatabase()</code> method is an |
| 141 |
optional transaction that is used for creating a new database. If |
| 142 |
null is passed, auto-commit is used when creating a database. |
| 143 |
</p> |
| 144 |
<p> |
| 145 |
The second and third parameters of <code class="methodname">openDatabase()</code> |
| 146 |
specify the filename and database (sub-file) name of the database. The |
| 147 |
database name is optional and is <code class="literal">null</code> in this example. |
| 148 |
</p> |
| 149 |
<p> |
| 150 |
The last parameter of <code class="methodname">openDatabase()</code> specifies the database |
| 151 |
configuration object. |
| 152 |
</p> |
| 153 |
<p> |
| 154 |
Lastly, the <code class="classname">StoredClassCatalog</code> object is created to manage the |
| 155 |
information in the class catalog database. The |
| 156 |
<code class="classname">StoredClassCatalog</code> object will be used in the sections |
| 157 |
following for creating serial bindings. |
| 158 |
</p> |
| 159 |
<p> |
| 160 |
The <code class="methodname">getClassCatalog</code> method returns the catalog object for |
| 161 |
use by other classes in the example program. |
| 162 |
</p> |
| 163 |
<p> |
| 164 |
When the environment is closed, the class catalog is closed |
| 165 |
also. |
| 166 |
</p> |
| 167 |
<a id="cb_close1"></a> |
| 168 |
<pre class="programlisting"> public void close() |
| 169 |
throws DatabaseException |
| 170 |
{ |
| 171 |
<strong class="userinput"><code> javaCatalog.close();</code></strong> |
| 172 |
env.close(); |
| 173 |
} </pre> |
| 174 |
<p> |
| 175 |
The <code class="methodname">StoredClassCatalog.close()</code> method simply closes the |
| 176 |
underlying class catalog database and in fact the |
| 177 |
|
| 178 |
<a class="ulink" href="../../java/com/sleepycat/db/Database.html#close()" target="_top">Database.close()</a> |
| 179 |
|
| 180 |
method may be called instead, if desired. It is recommended that you close the catalog database and |
| 181 |
all other databases, before closing the |
| 182 |
environment. |
| 183 |
</p> |
| 184 |
</div> |
| 185 |
<div class="navfooter"> |
| 186 |
<hr /> |
| 187 |
<table width="100%" summary="Navigation footer"> |
| 188 |
<tr> |
| 189 |
<td width="40%" align="left"><a accesskey="p" href="opendbenvironment.html">Prev</a> </td> |
| 190 |
<td width="20%" align="center"> |
| 191 |
<a accesskey="u" href="BasicProgram.html">Up</a> |
| 192 |
</td> |
| 193 |
<td width="40%" align="right"> <a accesskey="n" href="opendatabases.html">Next</a></td> |
| 194 |
</tr> |
| 195 |
<tr> |
| 196 |
<td width="40%" align="left" valign="top"> |
| 197 |
Opening and Closing the Database Environment |
| 198 |
</td> |
| 199 |
<td width="20%" align="center"> |
| 200 |
<a accesskey="h" href="index.html">Home</a> |
| 201 |
</td> |
| 202 |
<td width="40%" align="right" valign="top"> |
| 203 |
Opening and Closing Databases |
| 204 |
</td> |
| 205 |
</tr> |
| 206 |
</table> |
| 207 |
</div> |
| 208 |
</body> |
| 209 |
</html> |