Berkeley DB Reference Version 5.3.33

DB_ENV->open()

#include <db.h>

int
DB_ENV->open(DB_ENV *dbenv, char *db_home, u_int32_t flags, int mode);  

The DB_ENV->open() method opens a Berkeley DB environment. It provides a structure for creating a consistent environment for processes using one or more of the features of Berkeley DB.

The DB_ENV->open() method method returns a non-zero error value on failure and 0 on success. If DB_ENV->open() fails, the DB_ENV->close() method must be called to discard the DB_ENV handle.

Warning

Using environments with some journaling filesystems might result in log file corruption. This can occur if the operating system experiences an unclean shutdown when a log file is being created. Please see Using Recovery on Journaling Filesystems in the Berkeley DB Programmer's Reference Guide for more information.

Parameters

db_home

The db_home parameter is the database environment's home directory. For more information on db_home, and filename resolution in general, see Berkeley DB File Naming. The environment variable DB_HOME may be used as the path of the database home, as described in Berkeley DB File Naming.

When using a Unicode build on Windows (the default), the db_home argument will be interpreted as a UTF-8 string, which is equivalent to ASCII for Latin characters.

flags

The flags parameter specifies the subsystems that are initialized and how the application's environment affects Berkeley DB file naming, among other things. The flags parameter must be set to 0 or by bitwise inclusively OR'ing together one or more of the values described in this section.

Because there are a large number of flags that can be specified, they have been grouped together by functionality. The first group of flags indicates which of the Berkeley DB subsystems should be initialized.

The choice of subsystems initialized for a Berkeley DB database environment is specified by the thread of control initially creating the environment. Any subsequent thread of control joining the environment will automatically be configured to use the same subsystems as were created in the environment (unless the thread of control requests a subsystem not available in the environment, which will fail). Applications joining an environment, able to adapt to whatever subsystems have been configured in the environment, should open the environment without specifying any subsystem flags. Applications joining an environment, requiring specific subsystems from their environments, should open the environment specifying those specific subsystem flags.

The second group of flags govern what recovery, if any, is performed when the environment is initialized:

A standard part of the recovery process is to remove the existing Berkeley DB environment and create a new one in which to perform recovery. If the thread of control performing recovery does not specify the correct region initialization information (for example, the correct memory pool cache size), the result can be an application running in an environment with incorrect cache and other subsystem sizes. For this reason, the thread of control performing recovery should specify correct configuration information before calling the DB_ENV->open() method; or it should remove the environment after recovery is completed, leaving creation of the correctly sized environment to a subsequent call to the DB_ENV->open() method.

All Berkeley DB recovery processing must be single-threaded; that is, only a single thread of control may perform recovery or access a Berkeley DB environment while recovery is being performed. Because it is not an error to specify DB_RECOVER for an environment for which no recovery is required, it is reasonable programming practice for the thread of control responsible for performing recovery and creating the environment to always specify the DB_CREATE and DB_RECOVER flags during startup.

The third group of flags govern file-naming extensions in the environment:

Finally, there are a few additional unrelated flags:

mode

On Windows systems, the mode parameter is ignored.

On UNIX systems or in IEEE/ANSI Std 1003.1 (POSIX) environments, files created by Berkeley DB are created with mode mode (as described in chmod(2)) and modified by the process' umask value at the time of creation (see umask(2)). Created files are owned by the process owner; the group ownership of created files is based on the system and directory defaults, and is not further specified by Berkeley DB. System shared memory segments created by Berkeley DB are created with mode mode, unmodified by the process' umask value. If mode is 0, Berkeley DB will use a default mode of readable and writable by both owner and group.

Errors

The DB_ENV->open() method may fail and return one of the following non-zero errors:

DB_RUNRECOVERY

Either the DB_REGISTER flag was specified, a failure occurred, and no recovery flag was specified, or the DB_FAILCHK flag was specified and recovery was deemed necessary.

DB_VERSION_MISMATCH

The version of the Berkeley DB library doesn't match the version that created the database environment.

EAGAIN

The shared memory region was locked and (repeatedly) unavailable.

EINVAL

If the DB_THREAD flag was specified and fast mutexes are not available for this architecture; The DB_HOME or TMPDIR environment variables were set, but empty; An incorrectly formatted NAME VALUE entry or line was found; or if an invalid flag value or parameter was specified.

ENOENT

The file or directory does not exist.

Class

DB_ENV

See Also

Database Environments and Related Methods