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