Database Example

In Database Environment Management Example we created a class that manages an Environment. We now extend that class to allow it to open and manage multiple databases. Again, remember that you can find this class in:

JE_HOME/je/gettingStarted/MyDbEnv.java

where JE_HOME is the location where you placed your JE distribution.

Example 7.1 Database Management with MyDbEnv

First, we need to import a few additional classes, and setup some global variables to support databases. The databases that we are configuring and creating here are used by applications developed in examples later in this guide.

// File MyDbEnv.java

package je.gettingStarted;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.Database; 
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.Environment;

import java.io.File;

public class MyDbEnv {
    
    private Environment myEnv;
    private Database vendorDb;
    private Database inventoryDb;

    public MyDbEnv() {} 

Next we need to update the MyDbEnv.setup() method to instantiate a DatabaseConfig object. We also need to set some properties on that object. These property values are determined by the value of the readOnly parameter. We want our databases to be read-only if the environment is also read-only. We also want to allow our databases to be created if the databases are not read-only.

    public void setup(File envHome, boolean readOnly)
            throws DatabaseException {

        // Instantiate an environment and database configuration object
        EnvironmentConfig myEnvConfig = new EnvironmentConfig();
        DatabaseConfig myDbConfig = new DatabaseConfig();
        // Configure the environment and databases for the read-only
        // state as identified by the readOnly parameter on this 
        // method call.
        myEnvConfig.setReadOnly(readOnly);
        myDbConfig.setReadOnly(readOnly);
        // If the environment is opened for write, then we want to be
        // able to create the environment and databases if 
        // they do not exist.
        myEnvConfig.setAllowCreate(!readOnly);
        myDbConfig.setAllowCreate(!readOnly);

        // Instantiate the Environment. This opens it and also possibly
        // creates it.
        myEnv = new Environment(envHome, myEnvConfig);

        // Now create and open our databases.
        vendorDb = myEnv.openDatabase(null,
                                       "VendorDB",
                                       myDbConfig); 

        inventoryDb = myEnv.openDatabase(null,
                                         "InventoryDB",
                                         myDbConfig);
    } 

Next we need some additional getter methods used to return our database handles.

     // Getter methods
    public Environment getEnvironment() {
        return myEnv;
    }

    public Database getVendorDB() {
        return vendorDb;
    }

    public Database getInventoryDB() {
        return inventoryDb;
    } 

Finally, we need to update the MyDbEnv.close() method to close our databases.

    // Close the environment
    public void close() {
        if (myEnv != null) {
            try {
                vendorDb.close();
                inventoryDb.close();
                myEnv.close();
            } catch(DatabaseException dbe) {
                System.err.println("Error closing MyDbEnv: " + 
                                    dbe.toString());
                System.exit(-1);
            }
        }
    }
}

We can now use MyDbEnv to open and close both database environments and databases from the appropriate place in our application. For example:

package je.gettingStarted;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Database;

import java.io.File;

...

MyDbEnv exampleDbEnv = new MyDbEnv();

try {
    exampleDbEnv.setup(new File("/directory/currently/exists"), true);
    Database vendorDb = exampleDbEnv.getVendorDB();
    Database inventoryDB = exampleDbEnv.getInventoryDB();

    ...

} catch(DatabaseException dbe) {
    // Error code goes here
} finally {
    exampleDbEnv.close();
}