1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>DbEnv::open()</title> 7 <link rel="stylesheet" href="apiReference.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> 9 <link rel="start" href="index.html" title="Berkeley DB C++ API Reference" /> 10 <link rel="up" href="env.html" title="Chapter 5. The DbEnv Handle" /> 11 <link rel="prev" href="envlsn_reset.html" title="DbEnv::lsn_reset()" /> 12 <link rel="next" href="envremove.html" title="DbEnv::remove()" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">DbEnv::open()</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="envlsn_reset.html">Prev</a> </td> 22 <th width="60%" align="center">Chapter 5. 23 The DbEnv Handle 24 </th> 25 <td width="20%" align="right"> <a accesskey="n" href="envremove.html">Next</a></td> 26 </tr> 27 </table> 28 <hr /> 29 </div> 30 <div class="sect1" lang="en" xml:lang="en"> 31 <div class="titlepage"> 32 <div> 33 <div> 34 <h2 class="title" style="clear: both"><a id="envopen"></a>DbEnv::open()</h2> 35 </div> 36 </div> 37 </div> 38 <pre class="programlisting">#include <db_cxx.h> 39 40int 41DbEnv::open(const char *db_home, u_int32_t flags, int mode); </pre> 42 <p> 43 The <code class="methodname">DbEnv::open()</code> method opens a Berkeley DB environment. It 44 provides a structure for creating a consistent environment for 45 processes using one or more of the features of Berkeley DB. 46 </p> 47 <p> 48 The <code class="methodname">DbEnv::open()</code> method <span> 49 50 <span> 51 method either returns a non-zero error value or throws an 52 exception that encapsulates a non-zero error value on 53 failure, and returns 0 on success. 54 </span> 55 </span> 56 If <code class="methodname">DbEnv::open()</code> fails, the 57 <a class="xref" href="envclose.html" title="DbEnv::close()">DbEnv::close()</a> method must be 58 called to discard the <a class="link" href="env.html" title="Chapter 5. The DbEnv Handle">DbEnv</a> handle. 59 </p> 60 <div class="sect2" lang="en" xml:lang="en"> 61 <div class="titlepage"> 62 <div> 63 <div> 64 <h3 class="title"><a id="id1667117"></a>Parameters</h3> 65 </div> 66 </div> 67 </div> 68 <div class="sect3" lang="en" xml:lang="en"> 69 <div class="titlepage"> 70 <div> 71 <div> 72 <h4 class="title"><a id="id1667199"></a>db_home</h4> 73 </div> 74 </div> 75 </div> 76 <p> 77 The <span class="bold"><strong>db_home</strong></span> parameter is the database 78 environment's home directory. For more information on <span class="bold"><strong>db_home</strong></span>, and filename resolution in general, 79 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 80 home, as described in <a href="../../programmer_reference/env_naming.html" class="olink">Berkeley DB File Naming</a>. 81 </p> 82 <p> 83 When using a Unicode build on Windows (the default), the 84 <span class="bold"><strong>db_home</strong></span> argument 85 will be interpreted as a UTF-8 string, which is equivalent to ASCII for 86 Latin characters. 87 </p> 88 </div> 89 <div class="sect3" lang="en" xml:lang="en"> 90 <div class="titlepage"> 91 <div> 92 <div> 93 <h4 class="title"><a id="id1667288"></a>flags</h4> 94 </div> 95 </div> 96 </div> 97 <p> 98 The <span class="bold"><strong>flags</strong></span> parameter specifies the subsystems that are 99 initialized and how the application's environment affects Berkeley DB file naming, 100 among other things. The <span class="bold"><strong>flags</strong></span> parameter must be set to 0 or by 101 bitwise inclusively <span class="bold"><strong>OR</strong></span>'ing together one or more of the values 102 described in this section. 103 </p> 104 <p> 105 Because there are a large number of flags that can be specified, they have been grouped together by 106 functionality. The first group of flags indicates which of the Berkeley DB subsystems should be 107 initialized. 108 </p> 109 <p> 110 The choice of subsystems initialized for a Berkeley DB database environment is specified by the 111 thread of control initially creating the environment. Any subsequent thread of control joining 112 the environment will automatically be configured to use the same subsystems as were created in 113 the environment (unless the thread of control requests a subsystem not available in the 114 environment, which will fail). Applications joining an environment, able to adapt to whatever 115 subsystems have been configured in the environment, should open the environment without 116 specifying any subsystem flags. Applications joining an environment, requiring specific 117 subsystems from their environments, should open the environment specifying those specific 118 subsystem flags. 119 </p> 120 <div class="itemizedlist"> 121 <ul type="disc"> 122 <li> 123 <p><a id="envopen_DB_INIT_CDB"></a> 124 <code class="literal">DB_INIT_CDB</code> 125 </p> 126 <p> 127 Initialize locking for the 128 <a href="../../programmer_reference/cam.html#cam_intro" class="olink">Berkeley DB Concurrent Data Store</a> 129 product. In this mode, 130 Berkeley DB provides multiple reader/single writer access. The only other subsystem 131 that should be specified with the <code class="literal">DB_INIT_CDB</code> flag is 132 <code class="literal">DB_INIT_MPOOL</code>. 133 </p> 134 </li> 135 <li> 136 <p><a id="envopen_DB_INIT_LOCK"></a> 137 <code class="literal">DB_INIT_LOCK</code> 138 </p> 139 <p> 140 Initialize the locking subsystem. This subsystem should be used when multiple 141 processes or threads are going to be reading and writing a Berkeley DB database, 142 so that they do not interfere with each other. If all threads are accessing the 143 database(s) read-only, locking is unnecessary. When the DB_INIT_LOCK flag is 144 specified, it is usually necessary to run a deadlock detector, as well. See 145 <a class="link" href="db_deadlock.html" title="db_deadlock">db_deadlock</a> and 146 <a class="xref" href="lockdetect.html" title="DbEnv::lock_detect()">DbEnv::lock_detect()</a> 147 for more information. 148 </p> 149 </li> 150 <li> 151 <p><a id="envopen_DB_INIT_LOG"></a> 152 <code class="literal">DB_INIT_LOG</code> 153 </p> 154 <p> 155 Initialize the logging subsystem. This subsystem should be used when recovery 156 from application or system failure is necessary. If the log region is being 157 created and log files are already present, the log files are reviewed; 158 subsequent log writes are appended to the end of the log, rather than 159 overwriting current log entries. 160 </p> 161 </li> 162 <li> 163 <p><a id="envopen_DB_INIT_MPOOL"></a> 164 <code class="literal">DB_INIT_MPOOL</code> 165 </p> 166 <p> 167 Initialize the shared memory buffer pool subsystem. This subsystem should be 168 used whenever an application is using any Berkeley DB access method. 169 </p> 170 </li> 171 <li> 172 <p><a id="open_DB_INIT_REP"></a> 173 <code class="literal">DB_INIT_REP</code> 174 </p> 175 <p> 176 Initialize the replication subsystem. This subsystem should be used whenever an 177 application plans on using replication. The <code class="literal">DB_INIT_REP</code> 178 flag requires the 179 <code class="literal">DB_INIT_TXN</code> and <code class="literal">DB_INIT_LOCK</code> 180 flags also be configured. 181 </p> 182 </li> 183 <li> 184 <p><a id="envopen_DB_INIT_TXN"></a> 185 <code class="literal">DB_INIT_TXN</code> 186 </p> 187 <p> 188 Initialize the transaction subsystem. This subsystem should be used when 189 recovery and atomicity of multiple operations are important. The 190 <code class="literal">DB_INIT_TXN</code> flag implies the 191 <code class="literal">DB_INIT_LOG</code> flag. 192 </p> 193 </li> 194 </ul> 195 </div> 196 <p> 197 The second group of flags govern what recovery, if any, is performed when the environment is 198 initialized: 199 </p> 200 <div class="itemizedlist"> 201 <ul type="disc"> 202 <li> 203 <p><a id="envopen_DB_RECOVER"></a> 204 <code class="literal">DB_RECOVER</code> 205 </p> 206 <p> 207 Run normal recovery on this environment before opening it for normal use. If 208 this flag is set, the <code class="literal">DB_CREATE</code> and 209 <code class="literal">DB_INIT_TXN</code> flags must also be set, because 210 the regions will be removed and re-created, and transactions are required for 211 application recovery. 212 </p> 213 </li> 214 <li> 215 <p><a id="envopen_DB_RECOVER_FATAL"></a> 216 <code class="literal">DB_RECOVER_FATAL</code> 217 </p> 218 <p> 219 Run catastrophic recovery on this environment before opening it for normal use. 220 If this flag is set, the <code class="literal">DB_CREATE</code> and 221 <code class="literal">DB_INIT_TXN</code> flags must also be set, 222 because the regions will be removed and re-created, and transactions are 223 required for application recovery. 224 </p> 225 </li> 226 </ul> 227 </div> 228 <p> 229 A standard part of the recovery process is to remove the existing Berkeley DB environment and 230 create a new one in which to perform recovery. If the thread of control performing recovery does 231 not specify the correct region initialization information (for example, the correct memory pool 232 cache size), the result can be an application running in an environment with incorrect cache and 233 other subsystem sizes. For this reason, the thread of control performing recovery should specify 234 correct configuration information before calling the <code class="methodname">DbEnv::open()</code> method; or it should remove 235 the environment after recovery is completed, leaving creation of the correctly sized environment 236 to a subsequent call to the <code class="methodname">DbEnv::open()</code> method. 237 </p> 238 <p> 239 All Berkeley DB recovery processing must be single-threaded; that is, only a single thread of 240 control may perform recovery or access a Berkeley DB environment while recovery is being 241 performed. Because it is not an error to specify <code class="literal">DB_RECOVER</code> for an 242 environment for which no recovery is required, it is reasonable programming practice for 243 the thread of control responsible for performing recovery and creating the environment to 244 always specify the <code class="literal">DB_CREATE</code> and <code class="literal">DB_RECOVER</code> flags 245 during startup. 246 </p> 247 <p> 248 The third group of flags govern file-naming extensions in the environment: 249 </p> 250 <div class="itemizedlist"> 251 <ul type="disc"> 252 <li> 253 <p><a id="envopen_DB_USE_ENVIRON"></a> 254 <code class="literal">DB_USE_ENVIRON</code> 255 </p> 256 <p> 257 The Berkeley DB process' environment may be permitted to specify information to 258 be used when naming files; see 259 <a href="../../programmer_reference/env_naming.html" class="olink">Berkeley DB File Naming</a>. 260 Because permitting users to specify which files are used can create security 261 problems, environment information will be used in file naming for all users 262 only if the <code class="literal">DB_USE_ENVIRON</code> flag is set. 263 </p> 264 </li> 265 <li> 266 <p><a id="envopen_DB_USE_ENVIRON_ROOT"></a> 267 <code class="literal">DB_USE_ENVIRON_ROOT</code> 268 </p> 269 <p> 270 The Berkeley DB process' environment may be permitted to specify information to 271 be used when naming files; see 272 <a href="../../programmer_reference/env_naming.html" class="olink">Berkeley DB File Naming</a>. 273 Because permitting users to specify which files are used can create security 274 problems, if the <code class="literal">DB_USE_ENVIRON_ROOT</code> flag is set, environment 275 information will be used in file naming only for users with appropriate 276 permissions (for example, users with a user-ID of 0 on <code class="literal">UNIX</code> 277 systems). 278 </p> 279 </li> 280 </ul> 281 </div> 282 <p> 283 Finally, there are a few additional unrelated flags: 284 </p> 285 <div class="itemizedlist"> 286 <ul type="disc"> 287 <li> 288 <p><a id="open_DB_CREATE"></a> 289 <code class="literal">DB_CREATE</code> 290 </p> 291 <p> 292 Cause Berkeley DB subsystems to create any underlying files, as necessary. 293 </p> 294 </li> 295 <li> 296 <p><a id="open_DB_LOCKDOWN"></a> 297 <code class="literal">DB_LOCKDOWN</code> 298 </p> 299 <p> 300 Lock shared Berkeley DB environment files and memory-mapped databases into 301 memory. 302 </p> 303 </li> 304 <li> 305 <p><a id="envopen_DB_FAILCHK"></a> 306 <code class="literal">DB_FAILCHK</code> 307 </p> 308 <p> 309 Internally call the <a class="xref" href="envfailchk.html" title="DbEnv::failchk()">DbEnv::failchk()</a> 310 method as part of opening the environment. When <code class="literal">DB_FAILCHK</code> is 311 specified, a check is made to ensure all <code class="methodname">DbEnv::failchk()</code> 312 prerequisites are meet. 313 </p> 314 <p> 315 If the <code class="literal">DB_FAILCHK</code> flag is used in conjunction with the 316 <code class="literal">DB_REGISTER</code> flag, then a check will be made to see if 317 the environment needs recovery. If recovery is needed, a call 318 will be made to the <code class="methodname">DbEnv::failchk()</code> method 319 to release any database reads locks held by the thread of control that 320 exited and, if needed, to abort the unresolved transaction. If 321 <code class="methodname">DbEnv::failchk()</code> determines environment recovery 322 is still 323 required, the recovery actions for <code class="literal">DB_REGISTER</code> will be 324 followed. 325 </p> 326 <p> 327 If the <code class="literal">DB_FAILCHK</code> flag is not used in conjunction with the 328 <code class="literal">DB_REGISTER</code> flag, then make an internal call to 329 <code class="methodname">DbEnv::failchk()</code> as the last step of opening the environment. 330 If <code class="methodname">DbEnv::failchk()</code> determines database environment recovery is 331 required, <a href="../../programmer_reference/program_errorret.html#program_errorret.DB_RUNRECOVERY" class="olink">DB_RUNRECOVERY</a> 332 will be returned. 333 334 </p> 335 </li> 336 <li> 337 <p><a id="open_DB_PRIVATE"></a> 338 <code class="literal">DB_PRIVATE</code> 339 </p> 340 <p> 341 Allocate region memory from the heap instead of from memory backed by the 342 filesystem or system shared memory. 343 </p> 344 <p> 345 This flag implies the environment will only be accessed by a single process 346 (although that process may be multithreaded). This flag has two effects on the 347 Berkeley DB environment. First, all underlying data structures are allocated 348 from per-process memory instead of from shared memory that is accessible to more 349 than a single process. Second, mutexes are only configured to work between 350 threads. 351 </p> 352 <p> 353 This flag should not be specified if more than a single process is accessing the 354 environment because it is likely to cause database corruption and unpredictable 355 behavior. For example, if both a server application and Berkeley DB utilities 356 (for example, 357 <a class="link" href="db_archive.html" title="db_archive">db_archive</a>, 358 <a class="link" href="db_checkpoint.html" title="db_checkpoint">db_checkpoint</a> or 359 <a class="link" href="db_stat.html" title="db_stat">db_stat</a>) are expected to access the 360 environment, the <code class="literal">DB_PRIVATE</code> flag should not be specified. 361 </p> 362 <p> 363 See <a href="../../programmer_reference/env_region.html" class="olink">Shared Memory Regions</a> for more 364 information. 365 </p> 366 </li> 367 <li> 368 <p><a id="envopen_DB_REGISTER"></a> 369 <code class="literal">DB_REGISTER</code> 370 </p> 371 <p> 372 Check to see if recovery needs to be performed before opening the database 373 environment. (For this check to be accurate, all processes using the environment 374 must specify <code class="literal">DB_REGISTER</code> when opening the environment.) If 375 recovery needs to be performed for any reason (including the initial use of the 376 <code class="literal">DB_REGISTER</code> flag), and <code class="literal">DB_RECOVER</code> is also 377 specified, recovery will be performed and the open will 378 proceed normally. If recovery needs to be performed and 379 <code class="literal">DB_RECOVER</code> is not specified, 380 <a href="../../programmer_reference/program_errorret.html#program_errorret.DB_RUNRECOVERY" class="olink">DB_RUNRECOVERY</a> 381 will be returned. If recovery does not need to be performed, the 382 <code class="literal">DB_RECOVER</code> flag will be ignored. See 383 <a href="../../programmer_reference/transapp_app.html" class="olink">Architecting Transactional Data Store applications</a> for more information. 384 </p> 385 </li> 386 <li> 387 <p><a id="envopen_DB_SYSTEM_MEM"></a> 388 <code class="literal">DB_SYSTEM_MEM</code> 389 </p> 390 <p> 391 Allocate region memory from system shared memory instead of from heap memory or 392 memory backed by the filesystem. 393 </p> 394 <p> 395 See <a href="../../programmer_reference/env_region.html" class="olink">Shared Memory Regions</a> for more 396 information. 397 </p> 398 </li> 399 <li> 400 <p><a id="envopen_DB_THREAD"></a> 401 <code class="literal">DB_THREAD</code> 402 </p> 403 <p> 404 Cause the 405 <a class="link" href="env.html" title="Chapter 5. The DbEnv Handle">DbEnv</a> 406 handle returned by <code class="methodname">DbEnv::open()</code> to be <span class="emphasis"><em>free-threaded;</em></span> that is, 407 concurrently usable by multiple threads in the address space. The 408 <code class="literal">DB_THREAD </code> flag 409 should be specified if the 410 <a class="link" href="env.html" title="Chapter 5. The DbEnv Handle">DbEnv</a> 411 handle will be concurrently used by more than 412 one thread in the process, or if any 413 <a class="link" href="db.html" title="Chapter 2. The Db Handle">Db</a> 414 handles opened in the scope of the 415 <a class="link" href="env.html" title="Chapter 5. The DbEnv Handle">DbEnv</a> 416 handle will be concurrently used by more than one thread in the process. 417 </p> 418 <p> 419 This flag is required when using the Replication Manager. 420 </p> 421 </li> 422 </ul> 423 </div> 424 </div> 425 <div class="sect3" lang="en" xml:lang="en"> 426 <div class="titlepage"> 427 <div> 428 <div> 429 <h4 class="title"><a id="id1667578"></a>mode</h4> 430 </div> 431 </div> 432 </div> 433 <p> 434 On Windows systems, the mode parameter is ignored. 435 </p> 436 <p> 437 On UNIX systems or in IEEE/ANSI Std 1003.1 (POSIX) environments, files 438 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 439 value at the time of creation (see <span class="bold"><strong>umask</strong></span>(2)). Created files are owned by the 440 process owner; the group ownership of created files is based on the 441 system and directory defaults, and is not further specified by 442 Berkeley DB. System shared memory segments created by Berkeley DB are 443 created with mode <span class="bold"><strong>mode</strong></span>, unmodified by 444 the process' umask value. If <span class="bold"><strong>mode</strong></span> is 445 0, Berkeley DB will use a default mode of readable and writable by 446 both owner and group. 447 </p> 448 </div> 449 </div> 450 <div class="sect2" lang="en" xml:lang="en"> 451 <div class="titlepage"> 452 <div> 453 <div> 454 <h3 class="title"><a id="id1667526"></a>Errors</h3> 455 </div> 456 </div> 457 </div> 458 <p> 459 The <code class="methodname">DbEnv::open()</code> <span> 460 461 <span> 462 method may fail and throw a <a class="link" href="dbexception.html" title="Chapter 6. The DbException Class">DbException</a> 463 exception, encapsulating one of the following non-zero errors, or return one 464 of the following non-zero errors: 465 </span> 466 </span> 467 </p> 468 <div class="sect3" lang="en" xml:lang="en"> 469 <div class="titlepage"> 470 <div> 471 <div> 472 <h4 class="title"><a id="id1667514"></a>DB_RUNRECOVERY</h4> 473 </div> 474 </div> 475 </div> 476 <p> 477 Either the <code class="literal">DB_REGISTER</code> flag was specified, a failure occurred, and 478 no recovery flag was specified, or the <code class="literal">DB_FAILCHK</code> flag was specified 479 and recovery was deemed necessary. 480 </p> 481 </div> 482 <div class="sect3" lang="en" xml:lang="en"> 483 <div class="titlepage"> 484 <div> 485 <div> 486 <h4 class="title"><a id="id1667797"></a>DB_VERSION_MISMATCH</h4> 487 </div> 488 </div> 489 </div> 490 <p> 491 The version of the Berkeley DB library doesn't match the version that 492 created the database environment. 493 </p> 494 </div> 495 <div class="sect3" lang="en" xml:lang="en"> 496 <div class="titlepage"> 497 <div> 498 <div> 499 <h4 class="title"><a id="id1667430"></a>EAGAIN</h4> 500 </div> 501 </div> 502 </div> 503 <p> 504 The shared memory region was locked and (repeatedly) unavailable. 505 </p> 506 </div> 507 <div class="sect3" lang="en" xml:lang="en"> 508 <div class="titlepage"> 509 <div> 510 <div> 511 <h4 class="title"><a id="id1667484"></a>EINVAL</h4> 512 </div> 513 </div> 514 </div> 515 <p> 516 If the <code class="literal">DB_THREAD</code> flag was specified and fast mutexes are not available 517 for this architecture; The <code class="literal">DB_HOME</code> or <code class="literal">TMPDIR</code> 518 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 519 invalid flag value or parameter was specified. 520 </p> 521 </div> 522 <div class="sect3" lang="en" xml:lang="en"> 523 <div class="titlepage"> 524 <div> 525 <div> 526 <h4 class="title"><a id="id1668418"></a>ENOSPC</h4> 527 </div> 528 </div> 529 </div> 530 <p> 531 HP-UX only: Due to the constraints of the PA-RISC memory architecture, 532 HP-UX does not allow a process to map a file into its address space 533 multiple times. For this reason, each Berkeley DB environment may be 534 opened only once by a process on HP-UX; that is, calls to 535 <code class="methodname">DbEnv::open()</code> will fail if the specified Berkeley DB environment has 536 been opened and not subsequently closed. 537 </p> 538 </div> 539 <div class="sect3" lang="en" xml:lang="en"> 540 <div class="titlepage"> 541 <div> 542 <div> 543 <h4 class="title"><a id="id1668428"></a>ENOENT</h4> 544 </div> 545 </div> 546 </div> 547 <p> 548 The file or directory does not exist. 549 </p> 550 </div> 551 </div> 552 <div class="sect2" lang="en" xml:lang="en"> 553 <div class="titlepage"> 554 <div> 555 <div> 556 <h3 class="title"><a id="id1668479"></a>Class</h3> 557 </div> 558 </div> 559 </div> 560 <p> 561 <a class="link" href="env.html" title="Chapter 5. The DbEnv Handle">DbEnv</a> 562 </p> 563 </div> 564 <div class="sect2" lang="en" xml:lang="en"> 565 <div class="titlepage"> 566 <div> 567 <div> 568 <h3 class="title"><a id="id1667536"></a>See Also</h3> 569 </div> 570 </div> 571 </div> 572 <p> 573 <a class="xref" href="env.html#envlist" title="Database Environments and Related Methods">Database Environments and Related Methods</a> 574 </p> 575 </div> 576 </div> 577 <div class="navfooter"> 578 <hr /> 579 <table width="100%" summary="Navigation footer"> 580 <tr> 581 <td width="40%" align="left"><a accesskey="p" href="envlsn_reset.html">Prev</a> </td> 582 <td width="20%" align="center"> 583 <a accesskey="u" href="env.html">Up</a> 584 </td> 585 <td width="40%" align="right"> <a accesskey="n" href="envremove.html">Next</a></td> 586 </tr> 587 <tr> 588 <td width="40%" align="left" valign="top">DbEnv::lsn_reset() </td> 589 <td width="20%" align="center"> 590 <a accesskey="h" href="index.html">Home</a> 591 </td> 592 <td width="40%" align="right" valign="top"> DbEnv::remove()</td> 593 </tr> 594 </table> 595 </div> 596 </body> 597</html> 598