Berkeley DB Reference Guide:
Java API Tutorial - Basic

PrevRefNext

Implementing the main program

The main program opens the database, stores and retrieves objects within a transaction, and closes the database. This section describes the main program shell, and the next section describes how to run transactions for storing and retrieving objects.


The Sample class contains the main program. The skeleton for the Sample class follows.

import com.sleepycat.db.DbException;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Sample { private SampleDatabase db; private SampleViews views;

public static void main(String args) { }

private Sample(String homeDir, boolean runRecovery) throws DbException, FileNotFoundException { }

private void close() throws DbException, IOException { }

private void run() throws Exception { } }

The main program uses the SampleDatabase and SampleViews classes that were described in the preceding sections. The main method will create an instance of the Sample class, and call its run and close methods.


The following statements parse the program's command line arguments.

    public static void main(String[] args)
    {
        System.out.println("\nRunning sample: " + Sample.class);
        boolean runRecovery = true;
        String homeDir = "./tmp";
        for (int i = 0; i < args.length; i += 1)
        {
            String arg = args[i];
            if (args[i].equals("-h") && i < args.length - 1)
            {
                i += 1;
                homeDir = args[i];
            }
            else if (args[i].equals("-multiprocess"))
            {
                runRecovery = false;
            }
            else
            {
                System.err.println("Usage:\n java " + Sample.class.getName() +
                                  "\n  [-h <home-directory>] [-multiprocess]");
                System.exit(2);
            }
        }
    }

The usage command is:

java com.sleepycat.examples.bdb.shipment.basic.Sample
     [-h <home-directory> ] [-multiprocess]

The -h command is used to set the homeDir variable, which will later be passed to the SampleDatabase constructor. Normally all Berkeley DB programs should provide a way to configure their database environment home directory.

The default for the home directory is ./tmp -- the tmp subdirectory of the current directory where the sample is run. The home directory must exist before running the sample. To re-create the sample database from scratch, delete all files in the home directory before running the sample.

The home directory was described previously in the Opening and closing the database environment section.

Also described in that section was the runRecovery parameter of the SampleDatabase constructor. The -multiprocess command is used here to set the runRecovery value. If the processing model is single-process, then Berkeley DB recovery is always run when the environment is opened. If a multiprocess model is needed, a monitor process should be used to handle recovery.

Of course, the command line arguments shown are only examples and a real-life application may use different techniques for configuring these options.


The following statements create an instance of the Sample class and call its run and close methods.

    public static void main(String args)
    {
        ...
        Sample sample = null;
        try
        {
            sample = new Sample(homeDir, runRecovery);
            sample.run();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (sample != null)
            {
                try
                {
                    sample.close();
                }
                catch (Exception e)
                {
                    System.err.println("Exception during database close:");
                    e.printStackTrace();
                }
            }
        }
    }

The Sample constructor will open the database and the run method will run transactions for storing and retrieving objects. If either of these throws an exception, then the program was unable to run and should normally terminate. (Transaction retries are handled at a lower level and will be described later.) The first catch statement handles such exceptions.

The finally statement is used to call the close method since an attempt should always be made to close the database cleanly. If an exception is thrown during close and a prior exception occurred above, then the exception during close is likely a side effect of the prior exception.


The Sample constructor creates the SampleDatabase and SampleViews objects.

    private Sample(String homeDir, boolean runRecovery)
        throws DbException, FileNotFoundException
    {
        db = new SampleDatabase(homeDir, runRecovery);
        views = new SampleViews(db);
    }

Recall that creating the SampleDatabase object will open the environment and all database stores.


To close the database the Sample.close method simply calls SampleDatabase.close.

    private void close()
        throws DbException, IOException
    {
        db.close();
    }

The run method is described in the next section.


PrevRefNext

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