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

## Closing Database Environments

You close your environment by calling the `Environment.close()` method. Note that if you are not using transactions, then you should run an environment sync before closing your environment. Without a sync, you are not guaranteed that your database will be written to disk. See Databases and Log Files for more information on environment syncs.

    import com.sleepycat.je.DatabaseException;

    import com.sleepycat.je.Environment;

    ...

    try {
        if (myDbEnvironment != null) {
            myDbEnvironment.sync(); // For non-transactional only
            myDbEnvironment.close();
        }
    } catch (DatabaseException dbe) {
        // Exception handling goes here
    }

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

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 databases, then JE will complain before closing them as well. At this time, any open cursors are also closed, and any on-going transactions are aborted.

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