<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>DbEnv::open()</title> <link rel="stylesheet" href="apiReference.css" type="text/css" /> <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> <link rel="start" href="index.html" title="Berkeley DB C++ API Reference" /> <link rel="up" href="env.html" title="Chapter 5. The DbEnv Handle" /> <link rel="prev" href="envlsn_reset.html" title="DbEnv::lsn_reset()" /> <link rel="next" href="envremove.html" title="DbEnv::remove()" /> </head> <body> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr> <th colspan="3" align="center">DbEnv::open()</th> </tr> <tr> <td width="20%" align="left"><a accesskey="p" href="envlsn_reset.html">Prev</a> </td> <th width="60%" align="center">Chapter 5. The DbEnv Handle </th> <td width="20%" align="right"> <a accesskey="n" href="envremove.html">Next</a></td> </tr> </table> <hr /> </div> <div class="sect1" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h2 class="title" style="clear: both"><a id="envopen"></a>DbEnv::open()</h2> </div> </div> </div> <pre class="programlisting">#include <db_cxx.h> int DbEnv::open(const char *db_home, u_int32_t flags, int mode); </pre> <p> The <code class="methodname">DbEnv::open()</code> 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. </p> <p> The <code class="methodname">DbEnv::open()</code> method <span> <span> method either returns a non-zero error value or throws an exception that encapsulates a non-zero error value on failure, and returns 0 on success. </span> </span> If <code class="methodname">DbEnv::open()</code> fails, the <a class="xref" href="envclose.html" title="DbEnv::close()">DbEnv::close()</a> method must be called to discard the <a class="link" href="env.html" title="Chapter 5. The DbEnv Handle">DbEnv</a> handle. </p> <div class="sect2" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h3 class="title"><a id="id1667117"></a>Parameters</h3> </div> </div> </div> <div class="sect3" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h4 class="title"><a id="id1667199"></a>db_home</h4> </div> </div> </div> <p> The <span class="bold"><strong>db_home</strong></span> parameter is the database environment's home directory. For more information on <span class="bold"><strong>db_home</strong></span>, and filename resolution in general, see <a href="../../programmer_reference/env_naming.html" class="olink">Berkeley DB File Naming</a>. The environment variable <span class="bold"><strong>DB_HOME</strong></span> may be used as the path of the database home, as described in <a href="../../programmer_reference/env_naming.html" class="olink">Berkeley DB File Naming</a>. </p> <p> When using a Unicode build on Windows (the default), the <span class="bold"><strong>db_home</strong></span> argument will be interpreted as a UTF-8 string, which is equivalent to ASCII for Latin characters. </p> </div> <div class="sect3" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h4 class="title"><a id="id1667288"></a>flags</h4> </div> </div> </div> <p> The <span class="bold"><strong>flags</strong></span> parameter specifies the subsystems that are initialized and how the application's environment affects Berkeley DB file naming, among other things. The <span class="bold"><strong>flags</strong></span> parameter must be set to 0 or by bitwise inclusively <span class="bold"><strong>OR</strong></span>'ing together one or more of the values described in this section. </p> <p> 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. </p> <p> 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. </p> <div class="itemizedlist"> <ul type="disc"> <li> <p><a id="envopen_DB_INIT_CDB"></a> <code class="literal">DB_INIT_CDB</code> </p> <p> Initialize locking for the <a href="../../programmer_reference/cam.html#cam_intro" class="olink">Berkeley DB Concurrent Data Store</a> product. In this mode, Berkeley DB provides multiple reader/single writer access. The only other subsystem that should be specified with the <code class="literal">DB_INIT_CDB</code> flag is <code class="literal">DB_INIT_MPOOL</code>. </p> </li> <li> <p><a id="envopen_DB_INIT_LOCK"></a> <code class="literal">DB_INIT_LOCK</code> </p> <p> Initialize the locking subsystem. This subsystem should be used when multiple processes or threads are going to be reading and writing a Berkeley DB database, so that they do not interfere with each other. If all threads are accessing the database(s) read-only, locking is unnecessary. When the DB_INIT_LOCK flag is specified, it is usually necessary to run a deadlock detector, as well. See <a class="link" href="db_deadlock.html" title="db_deadlock">db_deadlock</a> and <a class="xref" href="lockdetect.html" title="DbEnv::lock_detect()">DbEnv::lock_detect()</a> for more information. </p> </li> <li> <p><a id="envopen_DB_INIT_LOG"></a> <code class="literal">DB_INIT_LOG</code> </p> <p> Initialize the logging subsystem. This subsystem should be used when recovery from application or system failure is necessary. If the log region is being created and log files are already present, the log files are reviewed; subsequent log writes are appended to the end of the log, rather than overwriting current log entries. </p> </li> <li> <p><a id="envopen_DB_INIT_MPOOL"></a> <code class="literal">DB_INIT_MPOOL</code> </p> <p> Initialize the shared memory buffer pool subsystem. This subsystem should be used whenever an application is using any Berkeley DB access method. </p> </li> <li> <p><a id="open_DB_INIT_REP"></a> <code class="literal">DB_INIT_REP</code> </p> <p> Initialize the replication subsystem. This subsystem should be used whenever an application plans on using replication. The <code class="literal">DB_INIT_REP</code> flag requires the <code class="literal">DB_INIT_TXN</code> and <code class="literal">DB_INIT_LOCK</code> flags also be configured. </p> </li> <li> <p><a id="envopen_DB_INIT_TXN"></a> <code class="literal">DB_INIT_TXN</code> </p> <p> Initialize the transaction subsystem. This subsystem should be used when recovery and atomicity of multiple operations are important. The <code class="literal">DB_INIT_TXN</code> flag implies the <code class="literal">DB_INIT_LOG</code> flag. </p> </li> </ul> </div> <p> The second group of flags govern what recovery, if any, is performed when the environment is initialized: </p> <div class="itemizedlist"> <ul type="disc"> <li> <p><a id="envopen_DB_RECOVER"></a> <code class="literal">DB_RECOVER</code> </p> <p> Run normal recovery on this environment before opening it for normal use. If this flag is set, the <code class="literal">DB_CREATE</code> and <code class="literal">DB_INIT_TXN</code> flags must also be set, because the regions will be removed and re-created, and transactions are required for application recovery. </p> </li> <li> <p><a id="envopen_DB_RECOVER_FATAL"></a> <code class="literal">DB_RECOVER_FATAL</code> </p> <p> Run catastrophic recovery on this environment before opening it for normal use. If this flag is set, the <code class="literal">DB_CREATE</code> and <code class="literal">DB_INIT_TXN</code> flags must also be set, because the regions will be removed and re-created, and transactions are required for application recovery. </p> </li> </ul> </div> <p> 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 <code class="methodname">DbEnv::open()</code> method; or it should remove the environment after recovery is completed, leaving creation of the correctly sized environment to a subsequent call to the <code class="methodname">DbEnv::open()</code> method. </p> <p> 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 <code class="literal">DB_RECOVER</code> 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 <code class="literal">DB_CREATE</code> and <code class="literal">DB_RECOVER</code> flags during startup. </p> <p> The third group of flags govern file-naming extensions in the environment: </p> <div class="itemizedlist"> <ul type="disc"> <li> <p><a id="envopen_DB_USE_ENVIRON"></a> <code class="literal">DB_USE_ENVIRON</code> </p> <p> The Berkeley DB process' environment may be permitted to specify information to be used when naming files; see <a href="../../programmer_reference/env_naming.html" class="olink">Berkeley DB File Naming</a>. Because permitting users to specify which files are used can create security problems, environment information will be used in file naming for all users only if the <code class="literal">DB_USE_ENVIRON</code> flag is set. </p> </li> <li> <p><a id="envopen_DB_USE_ENVIRON_ROOT"></a> <code class="literal">DB_USE_ENVIRON_ROOT</code> </p> <p> The Berkeley DB process' environment may be permitted to specify information to be used when naming files; see <a href="../../programmer_reference/env_naming.html" class="olink">Berkeley DB File Naming</a>. Because permitting users to specify which files are used can create security problems, if the <code class="literal">DB_USE_ENVIRON_ROOT</code> flag is set, environment information will be used in file naming only for users with appropriate permissions (for example, users with a user-ID of 0 on <code class="literal">UNIX</code> systems). </p> </li> </ul> </div> <p> Finally, there are a few additional unrelated flags: </p> <div class="itemizedlist"> <ul type="disc"> <li> <p><a id="open_DB_CREATE"></a> <code class="literal">DB_CREATE</code> </p> <p> Cause Berkeley DB subsystems to create any underlying files, as necessary. </p> </li> <li> <p><a id="open_DB_LOCKDOWN"></a> <code class="literal">DB_LOCKDOWN</code> </p> <p> Lock shared Berkeley DB environment files and memory-mapped databases into memory. </p> </li> <li> <p><a id="envopen_DB_FAILCHK"></a> <code class="literal">DB_FAILCHK</code> </p> <p> Internally call the <a class="xref" href="envfailchk.html" title="DbEnv::failchk()">DbEnv::failchk()</a> method as part of opening the environment. When <code class="literal">DB_FAILCHK</code> is specified, a check is made to ensure all <code class="methodname">DbEnv::failchk()</code> prerequisites are meet. </p> <p> If the <code class="literal">DB_FAILCHK</code> flag is used in conjunction with the <code class="literal">DB_REGISTER</code> flag, then a check will be made to see if the environment needs recovery. If recovery is needed, a call will be made to the <code class="methodname">DbEnv::failchk()</code> method to release any database reads locks held by the thread of control that exited and, if needed, to abort the unresolved transaction. If <code class="methodname">DbEnv::failchk()</code> determines environment recovery is still required, the recovery actions for <code class="literal">DB_REGISTER</code> will be followed. </p> <p> If the <code class="literal">DB_FAILCHK</code> flag is not used in conjunction with the <code class="literal">DB_REGISTER</code> flag, then make an internal call to <code class="methodname">DbEnv::failchk()</code> as the last step of opening the environment. If <code class="methodname">DbEnv::failchk()</code> determines database environment recovery is required, <a href="../../programmer_reference/program_errorret.html#program_errorret.DB_RUNRECOVERY" class="olink">DB_RUNRECOVERY</a> will be returned. </p> </li> <li> <p><a id="open_DB_PRIVATE"></a> <code class="literal">DB_PRIVATE</code> </p> <p> Allocate region memory from the heap instead of from memory backed by the filesystem or system shared memory. </p> <p> This flag implies the environment will only be accessed by a single process (although that process may be multithreaded). This flag has two effects on the Berkeley DB environment. First, all underlying data structures are allocated from per-process memory instead of from shared memory that is accessible to more than a single process. Second, mutexes are only configured to work between threads. </p> <p> This flag should not be specified if more than a single process is accessing the environment because it is likely to cause database corruption and unpredictable behavior. For example, if both a server application and Berkeley DB utilities (for example, <a class="link" href="db_archive.html" title="db_archive">db_archive</a>, <a class="link" href="db_checkpoint.html" title="db_checkpoint">db_checkpoint</a> or <a class="link" href="db_stat.html" title="db_stat">db_stat</a>) are expected to access the environment, the <code class="literal">DB_PRIVATE</code> flag should not be specified. </p> <p> See <a href="../../programmer_reference/env_region.html" class="olink">Shared Memory Regions</a> for more information. </p> </li> <li> <p><a id="envopen_DB_REGISTER"></a> <code class="literal">DB_REGISTER</code> </p> <p> Check to see if recovery needs to be performed before opening the database environment. (For this check to be accurate, all processes using the environment must specify <code class="literal">DB_REGISTER</code> when opening the environment.) If recovery needs to be performed for any reason (including the initial use of the <code class="literal">DB_REGISTER</code> flag), and <code class="literal">DB_RECOVER</code> is also specified, recovery will be performed and the open will proceed normally. If recovery needs to be performed and <code class="literal">DB_RECOVER</code> is not specified, <a href="../../programmer_reference/program_errorret.html#program_errorret.DB_RUNRECOVERY" class="olink">DB_RUNRECOVERY</a> will be returned. If recovery does not need to be performed, the <code class="literal">DB_RECOVER</code> flag will be ignored. See <a href="../../programmer_reference/transapp_app.html" class="olink">Architecting Transactional Data Store applications</a> for more information. </p> </li> <li> <p><a id="envopen_DB_SYSTEM_MEM"></a> <code class="literal">DB_SYSTEM_MEM</code> </p> <p> Allocate region memory from system shared memory instead of from heap memory or memory backed by the filesystem. </p> <p> See <a href="../../programmer_reference/env_region.html" class="olink">Shared Memory Regions</a> for more information. </p> </li> <li> <p><a id="envopen_DB_THREAD"></a> <code class="literal">DB_THREAD</code> </p> <p> Cause the <a class="link" href="env.html" title="Chapter 5. The DbEnv Handle">DbEnv</a> handle returned by <code class="methodname">DbEnv::open()</code> to be <span class="emphasis"><em>free-threaded;</em></span> that is, concurrently usable by multiple threads in the address space. The <code class="literal">DB_THREAD </code> flag should be specified if the <a class="link" href="env.html" title="Chapter 5. The DbEnv Handle">DbEnv</a> handle will be concurrently used by more than one thread in the process, or if any <a class="link" href="db.html" title="Chapter 2. The Db Handle">Db</a> handles opened in the scope of the <a class="link" href="env.html" title="Chapter 5. The DbEnv Handle">DbEnv</a> handle will be concurrently used by more than one thread in the process. </p> <p> This flag is required when using the Replication Manager. </p> </li> </ul> </div> </div> <div class="sect3" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h4 class="title"><a id="id1667578"></a>mode</h4> </div> </div> </div> <p> On Windows systems, the mode parameter is ignored. </p> <p> On UNIX systems or in IEEE/ANSI Std 1003.1 (POSIX) environments, files created by Berkeley DB are created with mode <span class="bold"><strong>mode</strong></span> (as described in <span class="bold"><strong>chmod</strong></span>(2)) and modified by the process' umask value at the time of creation (see <span class="bold"><strong>umask</strong></span>(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 <span class="bold"><strong>mode</strong></span>, unmodified by the process' umask value. If <span class="bold"><strong>mode</strong></span> is 0, Berkeley DB will use a default mode of readable and writable by both owner and group. </p> </div> </div> <div class="sect2" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h3 class="title"><a id="id1667526"></a>Errors</h3> </div> </div> </div> <p> The <code class="methodname">DbEnv::open()</code> <span> <span> method may fail and throw a <a class="link" href="dbexception.html" title="Chapter 6. The DbException Class">DbException</a> exception, encapsulating one of the following non-zero errors, or return one of the following non-zero errors: </span> </span> </p> <div class="sect3" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h4 class="title"><a id="id1667514"></a>DB_RUNRECOVERY</h4> </div> </div> </div> <p> Either the <code class="literal">DB_REGISTER</code> flag was specified, a failure occurred, and no recovery flag was specified, or the <code class="literal">DB_FAILCHK</code> flag was specified and recovery was deemed necessary. </p> </div> <div class="sect3" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h4 class="title"><a id="id1667797"></a>DB_VERSION_MISMATCH</h4> </div> </div> </div> <p> The version of the Berkeley DB library doesn't match the version that created the database environment. </p> </div> <div class="sect3" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h4 class="title"><a id="id1667430"></a>EAGAIN</h4> </div> </div> </div> <p> The shared memory region was locked and (repeatedly) unavailable. </p> </div> <div class="sect3" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h4 class="title"><a id="id1667484"></a>EINVAL</h4> </div> </div> </div> <p> If the <code class="literal">DB_THREAD</code> flag was specified and fast mutexes are not available for this architecture; The <code class="literal">DB_HOME</code> or <code class="literal">TMPDIR</code> environment variables were set, but empty; An incorrectly formatted <span class="bold"><strong>NAME VALUE</strong></span> entry or line was found; or if an invalid flag value or parameter was specified. </p> </div> <div class="sect3" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h4 class="title"><a id="id1668418"></a>ENOSPC</h4> </div> </div> </div> <p> HP-UX only: Due to the constraints of the PA-RISC memory architecture, HP-UX does not allow a process to map a file into its address space multiple times. For this reason, each Berkeley DB environment may be opened only once by a process on HP-UX; that is, calls to <code class="methodname">DbEnv::open()</code> will fail if the specified Berkeley DB environment has been opened and not subsequently closed. </p> </div> <div class="sect3" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h4 class="title"><a id="id1668428"></a>ENOENT</h4> </div> </div> </div> <p> The file or directory does not exist. </p> </div> </div> <div class="sect2" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h3 class="title"><a id="id1668479"></a>Class</h3> </div> </div> </div> <p> <a class="link" href="env.html" title="Chapter 5. The DbEnv Handle">DbEnv</a> </p> </div> <div class="sect2" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h3 class="title"><a id="id1667536"></a>See Also</h3> </div> </div> </div> <p> <a class="xref" href="env.html#envlist" title="Database Environments and Related Methods">Database Environments and Related Methods</a> </p> </div> </div> <div class="navfooter"> <hr /> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"><a accesskey="p" href="envlsn_reset.html">Prev</a> </td> <td width="20%" align="center"> <a accesskey="u" href="env.html">Up</a> </td> <td width="40%" align="right"> <a accesskey="n" href="envremove.html">Next</a></td> </tr> <tr> <td width="40%" align="left" valign="top">DbEnv::lsn_reset() </td> <td width="20%" align="center"> <a accesskey="h" href="index.html">Home</a> </td> <td width="40%" align="right" valign="top"> DbEnv::remove()</td> </tr> </table> </div> </body> </html>