Closing Database Environments
Prev | Chapter 2. Database Environments | Next

## Closing Database Environments

You close your environment by calling the `Environment.close()` method. This method performs a checkpoint, so it is not necessary to perform a sync or a checkpoint explicitly before calling it. For information on checkpoints, see the JE Transaction Processing Guide. For information on syncs, see the Berkeley DB Java Edition Getting Started Guide.

    import com.sleepycat.je.DatabaseException;

    import com.sleepycat.je.Environment;

    ...

    try {
        if (myDbEnvironment != null) {
            myDbEnvironment.close();
        }
    } catch (DatabaseException dbe) {
        // Exception handling goes here
    }

You should close your environment(s) only after all other store activities have completed and you have closed any stores currently opened in the environment.

### Note

It is possible for the environment to close before JE's cleaner thread has finished its work. This happens if you perform a large number of deletes immediately before shutting down your environment. The result is that your log files may be quit a lot larger than you expect them to be because the cleaner thread has not had a chance to finish its work.

If want to make sure that the cleaner has finished running before the environment is closed, call `Environment.cleanLog()` before calling `Environment.close()`:

    import com.sleepycat.je.DatabaseException;

    import com.sleepycat.je.Environment;

    ...

    try {
        if (myDbEnvironment != null) {
            myDbEnvironment.cleanLog(); // Clean the log before closing
            myDbEnvironment.close();
        }
    } catch (DatabaseException dbe) {
        // Exception handling goes here
    }

Closing the last environment handle in your application causes all internal data structures to be released and the background threads to be stopped. If there are any opened stores, then JE will complain before closing them as well.

Prev | Up | Next
Chapter 2. Database Environments | Home | Environment Properties