Chapter 2. Database Environments
Prev |  | Next

## Chapter 2. Database Environments

The DPL requires you to use a database environment. The environment is used by the DPL to manage the databases that it maintains for you, and to manage transactions.

You also use the database environment for administrative and configuration activities related to your database log files and the in-memory cache. See the Getting Started with Berkeley DB Java Edition guide for more information.

## Opening Database Environments

You open a database environment by instantiating an `Environment` object. You must provide to the constructor the name of the on-disk directory where the environment is to reside. This directory location must exist or the open will fail.

By default, the environment is not created for you if it does not exist. Set the creation property to `true` if you want the environment to be created. For example:

    package persist.gettingStarted;

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

    import java.io.File;

    ...

    // Open the environment. Allow it to be created if it does not already exist.
    Environment myDbEnvironment = null;

    try {
        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setAllowCreate(true);
        myDbEnvironment = new Environment(new File("/export/dbEnv"), envConfig);
    } catch (DatabaseException dbe) {
        // Exception handling goes here
    }

Your application can open and use as many environments as you have disk and memory to manage, although most applications will use just one environment. Also, you can instantiate multiple `Environment` objects for the same physical environment.

Opening an environment usually causes some background threads to be started. JE uses these threads for log file cleaning and some administrative tasks. However, these threads will only be opened once per process, so if you open the same environment more than once from within the same process, there is no performance impact on your application. Also, if you open the environment as read-only, then the background threads (with the exception of the evictor thread) are not started.

Note that opening your environment causes normal recovery to be run. This causes the underlying databases used by the DPL to be brought into a consistent state relative to the changed data found in your log files. See the Getting Started with Berkeley DB Java Edition for more information on background threads and database log files.

Prev | Up | Next
Library Dependencies | Home | Closing Database Environments