1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>
Opening and Closing the Class Catalog
</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
<link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" />
<link rel="up" href="BasicProgram.html" title="Chapter2. 		The Basic Program 	" />
<link rel="previous" href="opendbenvironment.html" title=" 		Opening and Closing the Database Environment 	" />
<link rel="next" href="opendatabases.html" title=" 		Opening and Closing Databases 	" />
</head>
<body>
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">
Opening and Closing the Class Catalog
</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="opendbenvironment.html">Prev</a></td>
<th width="60%" align="center">Chapter2.
The Basic Program
</th>
<td width="20%" align="right"><a accesskey="n" href="opendatabases.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="openclasscatalog"></a>
Opening and Closing the Class Catalog
</h2>
</div>
</div>
<div></div>
</div>
<p>
This section describes how to open and close the Java class
catalog. The class catalog is a specialized database store that
contains the Java class descriptions of the serialized objects that
are stored in the database. The class descriptions are stored in
the catalog rather than storing them redundantly in each database
record. A single class catalog per environment must be opened
whenever serialized objects will be stored in the database.
</p>
<p>
The <tt class="classname">SampleDatabase</tt> class is extended to open and close
the class catalog. The following additional imports and class
members are needed.
</p>
<a id="cb_java_sampledatabase1"></a>
<pre class="programlisting"><b class="userinput"><tt>import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.db.Database;
import com.sleepycat.db.DatabaseConfig;
import com.sleepycat.db.DatabaseType;</tt></b>
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import java.io.File;
import java.io.FileNotFoundException;
...
public class SampleDatabase
{
private Environment env;
<b class="userinput"><tt> private static final String CLASS_CATALOG = "java_class_catalog";</tt></b>
...
<b class="userinput"><tt> private StoredClassCatalog javaCatalog;</tt></b>
...
} </pre>
<p>
While the class catalog is itself a database, it contains
metadata for other databases and is therefore treated specially by
the Sleepycat Java Collections API. The
<a href="../../java/com/sleepycat/bind/serial/StoredClassCatalog.html" target="_top">StoredClassCatalog</a>
class encapsulates the catalog store and implements this special
behavior.
</p>
<p>
The following statements open the class catalog by creating a
<tt class="classname">Database</tt> and a <tt class="classname">StoredClassCatalog</tt> object. The catalog
database is created if it doesn't already exist.
</p>
<a id="cb_java_sampledatabase2"></a>
<pre class="programlisting"> public SampleDatabase(String homeDirectory)
throws DatabaseException, FileNotFoundException
{
...
<b class="userinput"><tt> DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
dbConfig.setAllowCreate(true);
dbConfig.setType(DatabaseType.BTREE);
Database catalogDb = env.openDatabase(null, CLASS_CATALOG, null,
dbConfig);
javaCatalog = new StoredClassCatalog(catalogDb);</tt></b>
...
}
...
<b class="userinput"><tt> public final StoredClassCatalog getClassCatalog() {
return javaCatalog;
}</tt></b> </pre>
<p>
The
<a href="../../java/com/sleepycat/db/DatabaseConfig.html" target="_top">DatabaseConfig</a>
class is used to specify configuration parameters when opening a
database. The first configuration option specified —
<tt class="methodname">setTransactional()</tt> — is set to true to create a transactional
database. While non-transactional databases can also be created,
the examples in this tutorial use transactional databases.
</p>
<p>
<tt class="methodname">setAllowCreate()</tt> is set to true to specify
that the database will be created if it doesn't already exist. If
this parameter is not specified, an exception will be thrown if the
database does not already exist.
</p>
<p>
<tt class="methodname">setDatabaseType()</tt> identifies the database storage
type or access method. For opening a catalog database, the
<tt class="literal">BTREE</tt> type is required. <tt class="literal">BTREE</tt> is the
most commonly used database type and in this tutorial is used for all
databases.
</p>
<p>
The first parameter of the <tt class="methodname">openDatabase()</tt> method is an
optional transaction that is used for creating a new database. If
null is passed, auto-commit is used when creating a database.
</p>
<p>
The second and third parameters of <tt class="methodname">openDatabase()</tt>
specify the filename and database (sub-file) name o fthe database. The
database name is optional and is <tt class="literal">null</tt> in this example.
</p>
<p>
The last parameter of <tt class="methodname">openDatabase()</tt> specifies the database
configuration object.
</p>
<p>
Lastly, the <tt class="classname">StoredClassCatalog</tt> object is created to manage the
information in the class catalog database. The
<tt class="classname">StoredClassCatalog</tt> object will be used in the sections
following for creating serial bindings.
</p>
<p>
The <tt class="methodname">getClassCatalog</tt> method returns the catalog object for
use by other classes in the example program.
</p>
<p>
When the environment is closed, the class catalog is closed
also.
</p>
<a id="cb_close1"></a>
<pre class="programlisting"> public void close()
throws DatabaseException
{
<b class="userinput"><tt> javaCatalog.close();</tt></b>
env.close();
} </pre>
<p>
The <tt class="methodname">StoredClassCatalog.close()</tt> method simply closes the
underlying class catalog database and in fact the
<a href="../../java/com/sleepycat/db/Database.html#close()" target="_top">Database.close()</a>
method may be called instead, if desired. The catalog database, and
all other databases, must be closed before closing the
environment.
</p>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="opendbenvironment.html">Prev</a></td>
<td width="20%" align="center">
<a accesskey="u" href="BasicProgram.html">Up</a>
</td>
<td width="40%" align="right"><a accesskey="n" href="opendatabases.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">
Opening and Closing the Database Environment
</td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top">
Opening and Closing Databases
</td>
</tr>
</table>
</div>
</body>
</html>
|