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.bind.serial.StoredClassCatalog;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseType;
...
public class SampleDatabase
{
    private static final String CLASS_CATALOG = "java_class_catalog";
    ...
    private StoredClassCatalog javaCatalog;
    ...
} 

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 StoredClassCatalog class encapsulates the catalog store and implements this special behavior.

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

    public SampleDatabase(String homeDirectory)
        throws DatabaseException, FileNotFoundException
    {
        ...
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setTransactional(true);
        dbConfig.setAllowCreate(true);

        Database catalogDb = env.openDatabase(null, CLASS_CATALOG, dbConfig);

        javaCatalog = new StoredClassCatalog(catalogDb);
    }
    ...
    public final StoredClassCatalog getClassCatalog() {
        return javaCatalog;
    }
	

The DatabaseConfig class is used to specify configuration parameters when opening a database. The first configuration option specified — setTransactional() — 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.

setAllowCreate() 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.

The first parameter of the openDatabase() 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.

The second parameter of openDatabase() specifies the database name and must not be a null.

The last parameter of openDatabase() specifies the database configuration object.

Lastly, the StoredClassCatalog object is created to manage the information in the class catalog database. The StoredClassCatalog object will be used in the sections following for creating serial bindings.

The getClassCatalog method returns the catalog object for use by other classes in the example program.

When the environment is closed, the class catalog is closed also.

    public void close()
        throws DatabaseException
    {
        javaCatalog.close();
        env.close();
    } 

The StoredClassCatalog.close() method simply closes the underlying class catalog database and in fact the Database.close() method may be called instead, if desired. The catalog database, and all other databases, must be closed before closing the environment.