Berkeley DB Reference Guide: Opening and closing the class catalog
Description: Berkeley DB: An embedded database programmatic toolkit.
Keywords: embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++

### Berkeley DB Reference Guide: Java API Tutorial - Basic |

### Opening and closing the class catalog

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.

The SampleDatabase class is extended to open and close the class catalog. The following additional imports and class members are needed.

>     import com.sleepycat.bdb.StoredClassCatalog;
>     ...
>     public class SampleDatabase
>     {
>         private static final String CLASS_CATALOG = "java_class_catalog";
>         ...
>         private StoredClassCatalog javaCatalog;
>         ...
>     }

While the class catalog is a database store, it contains metadata for other stores and is therefore treated specially by the Java API. The StoredClassCatalog class encapsulates the catalog store and implements this special behavior.

The following statements open the class catalog by creating a StoredClassCatalog object. The catalog file is created if it doesn't already exist.

>         public SampleDatabase(String homeDirectory, boolean runRecovery)
>             throws DbException, FileNotFoundException
>         {
>             ...
>             int flags = Db.DB_CREATE | Db.DB_AUTO_COMMIT;
>             javaCatalog = new StoredClassCatalog(env, CLASS_CATALOG, null, flags);
>         }

The environment parameter is specified when opening any store, since a store always operates within a single environment.

The two String parameters are also used when opening any store. They specify the filename and database (sub-file) name, of the store. The database (sub-file) name is optional, and is null in the example.

The flags variable is initialized with Db.DB_CREATE to create the file if it doesn't exist and with Db.DB_AUTO_COMMIT to create the store within an implicit transaction. These flags are used for opening all stores in the example.

The following statement closes the class catalog.

>         public void close()
>             throws DbException, IOException
>         {
>             javaCatalog.close();
>             env.close(0);
>         }

The catalog store, and all other stores, must be closed before closing the environment.

Copyright (c) 1996-2003 Sleepycat Software, Inc. - All rights reserved.