Debian Codegraph @

docs/gsg_txn/CXX/enabletxn.html

raw
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>Chapter 2. Enabling Transactions</title>
7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
10 <link rel="up" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
11 <link rel="prev" href="perftune-intro.html" title="Performance Tuning" />
12 <link rel="next" href="envopen.html" title="Opening a Transactional Environment and Database" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">Chapter 2. Enabling Transactions</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="perftune-intro.html">Prev</a> </td>
25 <th width="60%" align="center"> </th>
26 <td width="20%" align="right"> <a accesskey="n" href="envopen.html">Next</a></td>
27 </tr>
28 </table>
29 <hr />
30 </div>
31 <div class="chapter" lang="en" xml:lang="en">
32 <div class="titlepage">
33 <div>
34 <div>
35 <h2 class="title"><a id="enabletxn"></a>Chapter 2. Enabling Transactions</h2>
36 </div>
37 </div>
38 </div>
39 <div class="toc">
40 <p>
41 <b>Table of Contents</b>
42 </p>
43 <dl>
44 <dt>
45 <span class="sect1">
46 <a href="enabletxn.html#environments">Environments</a>
47 </span>
48 </dt>
49 <dd>
50 <dl>
51 <dt>
52 <span class="sect2">
53 <a href="enabletxn.html#filenaming">File Naming</a>
54 </span>
55 </dt>
56 <dt>
57 <span class="sect2">
58 <a href="enabletxn.html#errorsupport">Error Support</a>
59 </span>
60 </dt>
61 <dt>
62 <span class="sect2">
63 <a href="enabletxn.html#sharedmemory">Shared Memory Regions</a>
64 </span>
65 </dt>
66 <dt>
67 <span class="sect2">
68 <a href="enabletxn.html#security">Security Considerations</a>
69 </span>
70 </dt>
71 </dl>
72 </dd>
73 <dt>
74 <span class="sect1">
75 <a href="envopen.html">Opening a Transactional Environment and
76 <span>Database</span>
77
78
79 </a>
80 </span>
81 </dt>
82 </dl>
83 </div>
84 <p>
85 In order to use transactions with your application, you must turn them
86 on. To do this you must:
87 </p>
88 <div class="itemizedlist">
89 <ul type="disc">
90 <li>
91 <p>
92 Use an
93 environment (see <a class="xref" href="enabletxn.html#environments" title="Environments">Environments</a> for details).
94 </p>
95 </li>
96 <li>
97 <p>
98 Turn on transactions for your environment.
99
100
101
102 <span>
103 You do this by providing the <code class="literal">DB_INIT_TXN</code>
104 flag to the
105 <code class="methodname">DbEnv::open()</code>
106
107 method.
108 </span>
109
110 <span>
111 Note that initializing the transactional subsystem implies that
112 the logging subsystem is also initialized. Also, note that
113 if you do not initialize transactions when you first create
114 your environment, then you cannot use transactions for that
115 environment after that. This is because DB
116 allocates certain structures needed for transactional
117 locking that are not available if the environment is
118 created without transactional support.
119 </span>
120 </p>
121 </li>
122 <li>
123 <p>
124 Initialize the in-memory cache by
125 <span>
126 passing the <code class="literal">DB_INIT_MPOOL</code>
127 flag to the
128 <code class="methodname">DbEnv::open()</code>
129
130 method.
131 </span>
132
133
134 </p>
135 </li>
136 <li>
137 <p>
138 Initialize the locking subsystem. This is what provides locking for concurrent applications. It also is used
139 to perform deadlock detection. See <a class="xref" href="txnconcurrency.html" title="Chapter 4. Concurrency">Concurrency</a>
140 for more information.
141 </p>
142 <p>
143 You initialize the locking subsystem by
144 <span>
145 passing the <code class="literal">DB_INIT_LOCK</code>
146 flag to the
147 <code class="methodname">DbEnv::open()</code>
148
149 method.
150 </span>
151
152
153 </p>
154 </li>
155 <li>
156 <p>
157 Initialize the logging subsystem. While this is enabled by
158 default for transactional applications, we suggest that
159 you explicitly initialize it anyway for the purposes of code readability. The logging
160 subsystem is what provides your transactional application its durability guarantee, and it is required for
161 recoverability purposes. See <a class="xref" href="filemanagement.html" title="Chapter 5. Managing DB Files">Managing DB Files</a>
162 for more information.
163 </p>
164 <p>
165 You initialize the logging subsystem by
166 <span>
167 passing the <code class="literal">DB_INIT_LOG</code>
168 flag to the
169 <code class="methodname">DbEnv::open()</code>
170
171 method.
172 </span>
173
174
175 </p>
176 </li>
177 <li>
178 <p>
179 <span>
180 Transaction-enable your databases.
181 </span>
182 <span>
183 If you are using the base API, transaction-enable your databases.
184 </span>
185 You do this by
186
187
188 <span>
189 encapsulating the database open in a transaction.
190 </span>
191
192
193 <span>
194 Note that the common practice is for auto commit to be used to
195 transaction-protect the database open. To use auto-commit, you
196 must still enable transactions as described here, but you do
197 not have to explicitly use a transaction when you open your
198 database. An example of this is given in the next section.
199 </span>
200 </p>
201 </li>
202 </ul>
203 </div>
204 <div class="sect1" lang="en" xml:lang="en">
205 <div class="titlepage">
206 <div>
207 <div>
208 <h2 class="title" style="clear: both"><a id="environments"></a>Environments</h2>
209 </div>
210 </div>
211 </div>
212 <div class="toc">
213 <dl>
214 <dt>
215 <span class="sect2">
216 <a href="enabletxn.html#filenaming">File Naming</a>
217 </span>
218 </dt>
219 <dt>
220 <span class="sect2">
221 <a href="enabletxn.html#errorsupport">Error Support</a>
222 </span>
223 </dt>
224 <dt>
225 <span class="sect2">
226 <a href="enabletxn.html#sharedmemory">Shared Memory Regions</a>
227 </span>
228 </dt>
229 <dt>
230 <span class="sect2">
231 <a href="enabletxn.html#security">Security Considerations</a>
232 </span>
233 </dt>
234 </dl>
235 </div>
236 <p>
237 For simple DB applications, environments are optional. However, in
238 order to transaction protect your database operations, you must use an
239 environment.
240 </p>
241 <p>
242 An <span class="emphasis"><em>environment</em></span>, represents an
243 encapsulation of one or more databases and any associated log and
244 region files. They are used to support multi-threaded
245 and multi-process applications by allowing different threads of
246 control to share the in-memory cache, the locking tables, the
247 logging subsystem, and the file namespace. By sharing these things,
248 your concurrent application is more efficient than if each thread
249 of control had to manage these resources on its own.
250 </p>
251 <p>
252 By default all DB databases are backed by files on disk. In
253 addition to these files, transactional DB applications create
254 logs that are also by default stored on disk (they can optionally
255 be backed using shared memory). Finally, transactional
256 DB applications also create and use shared-memory regions that
257 are also typically backed by the filesystem. But like databases and
258 logs, the regions can be maintained strictly in-memory if your
259 application requires it. For an example of an application that
260 manages all environment files in-memory, see
261 <span><a class="xref" href="inmem_txnexample_c.html" title="In-Memory Transaction Example">In-Memory Transaction Example</a>.</span>
262
263
264
265 </p>
266 <div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;">
267 <h3 class="title">Warning</h3>
268 <p>
269 Using environments with some journaling filesystems might
270 result in log file corruption. This can occur if the operating
271 system experiences an unclean shutdown when a log file is being
272 created. Please see
273 <a href="../../programmer_reference/transapp_journal.html" class="olink">Using
274 Recovery on Journaling Filesystems</a>
275
276 in the <em class="citetitle">Berkeley DB Programmer's Reference Guide</em> for more information.
277 </p>
278 </div>
279 <div class="sect2" lang="en" xml:lang="en">
280 <div class="titlepage">
281 <div>
282 <div>
283 <h3 class="title"><a id="filenaming"></a>File Naming</h3>
284 </div>
285 </div>
286 </div>
287 <p>
288 In order to operate, your DB application must be able to
289 locate its database files, log files, and region files. If these
290 are stored in the filesystem, then you must tell DB where
291 they are located (a number of mechanisms exist that allow you to
292 identify the location of these files – see below). Otherwise,
293 by default they are located in the current working directory.
294 </p>
295 <div class="sect3" lang="en" xml:lang="en">
296 <div class="titlepage">
297 <div>
298 <div>
299 <h4 class="title"><a id="envhome"></a>Specifying the Environment Home Directory</h4>
300 </div>
301 </div>
302 </div>
303 <p>
304 The environment home directory is used to determine where
305 DB files are located. Its location
306 is identified using one of the following mechanisms, in the
307 following order of priority:
308 </p>
309 <div class="itemizedlist">
310 <ul type="disc">
311 <li>
312 <p>
313 If no information is given as to where to put the
314 environment home, then the current working
315 directory is used.
316 </p>
317 </li>
318 <li>
319 <p>
320 If a home directory is specified on the
321
322 <code class="methodname">DbEnv::open()</code>
323
324 <span>method,</span>
325
326 then that location is always used for the environment
327 home.
328 </p>
329 </li>
330 <li>
331 <p>
332 If a home directory is not supplied to
333
334 <span><code class="methodname">DbEnv::open()</code>, </span>
335
336 then the directory identified by the <code class="literal">DB_HOME</code> environment variable
337 is used <span class="emphasis"><em>if</em></span> you specify
338 <span>
339 either the <code class="literal">DB_USE_ENVIRON</code> or
340 <code class="literal">DB_USE_ENVIRON_ROOT</code> flags to the
341
342 <code class="methodname">DbEnv::open()</code>
343 method. Both flags allow you to identify the
344 path to the environment's home directory
345 using the <code class="literal">DB_HOME</code> environment variable. However,
346 <code class="literal">DB_USE_ENVIRON_ROOT</code> is honored only if the
347 process is run with root or administrative privileges.
348 </span>
349
350
351 </p>
352 </li>
353 </ul>
354 </div>
355 </div>
356 <div class="sect3" lang="en" xml:lang="en">
357 <div class="titlepage">
358 <div>
359 <div>
360 <h4 class="title"><a id="filelocation"></a>Specifying File Locations</h4>
361 </div>
362 </div>
363 </div>
364 <p>
365 By default, all DB files are created relative to the environment
366 home directory. For example, suppose your environment home is in
367 <code class="literal">/export/myAppHome</code>. Also suppose you name your database
368 <span><code class="literal">data/myDatabase.db</code>.</span>
369
370 Then in this case, the database is placed in:
371 <span><code class="literal">/export/myAppHome/data/myDatabase.db</code>.</span>
372
373 </p>
374 <p>
375 That said, DB always defers to absolute pathnames.
376 This means that if you provide an absolute filename when you
377 name your database, then that file is <span class="emphasis"><em>not</em></span>
378 placed relative to the environment home directory. Instead, it
379 is placed in the exact location that you specified for the
380 filename.
381 </p>
382 <p>
383 On UNIX systems, an absolute pathname is a name that begins with a
384 forward slash ('/'). On Windows systems, an absolute pathname is a
385 name that begins with one of the following:
386 </p>
387 <div class="itemizedlist">
388 <ul type="disc">
389 <li>
390 <p>
391 A backslash ('\').
392 </p>
393 </li>
394 <li>
395 <p>
396 Any alphabetic letter, followed by a colon (':'), followed
397 by a backslash ('\').
398 </p>
399 </li>
400 </ul>
401 </div>
402 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
403 <h3 class="title">Note</h3>
404 <p>
405 Try not to use absolute path names for your environment's
406 files. Under certain recovery scenarios, absolute path
407 names can render your environment unrecoverable. This
408 occurs if you are attempting to recover your environment on
409 a system that does not support the absolute path name that
410 you used.
411 </p>
412 </div>
413 </div>
414 <div class="sect3" lang="en" xml:lang="en">
415 <div class="titlepage">
416 <div>
417 <div>
418 <h4 class="title"><a id="splittingdata"></a>Identifying Specific File Locations</h4>
419 </div>
420 </div>
421 </div>
422 <p>
423 As described in the previous sections, DB will place all its
424 files in or relative to the environment home directory.
425 You can also cause a
426 specific database file to be placed in a particular location by
427 using an absolute path name for its name. In this
428 situation, the environment's home directory is not
429 considered when naming the file.
430 </p>
431 <p>
432 It is frequently desirable to place database, log, and region files on separate
433 disk drives. By spreading I/O across multiple drives, you
434 can increase parallelism and improve throughput.
435 Additionally, by placing log files and database files on
436 separate drives, you improve your application's
437 reliability by providing your application with a greater
438 chance of surviving a disk failure.
439 </p>
440 <p>
441 You can cause DB's files to be placed in specific
442 locations using the following mechanisms:
443 </p>
444 <div class="informaltable">
445 <table border="1" width="80%">
446 <colgroup>
447 <col />
448 <col />
449 </colgroup>
450 <thead>
451 <tr>
452 <th>File Type</th>
453 <th>To Override</th>
454 </tr>
455 </thead>
456 <tbody>
457 <tr>
458 <td>database files</td>
459 <td>
460 <p>
461 You can cause database files to be created
462 in a directory other than the
463 environment home by using the
464
465 <code class="methodname">DbEnv::add_data_dir()</code>
466
467 method. The directory identified here must
468 exist. If a relative path is provided, then
469 the directory location is resolved relative
470 to the environment's home directory.
471 </p>
472
473 <p>
474 This method modifies the directory
475 used for database files created and managed by
476 a single environment handle; it does not
477 configure the entire environment.
478 <span>This
479 method may not be called after the
480 environment has been opened.
481 </span>
482 </p>
483
484 <p>
485 You can also set a default data location that is used by
486 the entire environment by using the
487 <code class="literal">add_data_dir</code> parameter
488 in the environment's <code class="literal">DB_CONFIG</code> file.
489 Note that the <code class="literal">add_data_dir</code>
490 parameter overrides any value set by the
491
492 <code class="methodname">DbEnv::set_data_dir()</code>
493
494 method.
495 </p>
496 </td>
497 </tr>
498 <tr>
499 <td>Log files</td>
500 <td>
501 <p>
502 You can cause log files to be created
503 in a directory other than the environment home
504 directory by using the
505
506 <code class="methodname">DbEnv::set_lg_dir()</code>
507
508 method. The directory identified
509 here must exist. If a relative path is
510 provided, then the directory location is
511 resolved relative to the environment's home
512 directory.
513 </p>
514
515 <p>
516 This method modifies the directory
517 used for database files created and managed by
518 a single environment handle; it does not
519 configure the entire environment.
520 <span>This
521 method may not be called after the
522 environment has been opened.
523 </span>
524 </p>
525
526 <p>
527 You can also set a default log file location that is used by
528 the entire environment by using the
529 <code class="literal">set_lg_dir</code> parameter
530 in the environment's <code class="literal">DB_CONFIG</code> file.
531 Note that the <code class="literal">set_lg_dir</code>
532 parameter overrides any value set by the
533
534 <code class="methodname">DbEnv::set_lg_dir()</code>
535
536 method.
537 </p>
538 </td>
539 </tr>
540 <tr>
541 <td>Temporary files</td>
542 <td>
543 <p>
544 You can cause temporary files required by the
545 environment to be created
546 in a directory other than the environment home
547 directory by using the
548
549 <code class="methodname">DbEnv::set_tmp_dir()</code>
550
551 method. The directory identified
552 here must exist. If a relative path is
553 provided, then the directory location is
554 resolved relative to the environment's home
555 directory.
556 </p>
557
558 <p>
559 You can also set a temporary file location
560 by using the
561 <code class="literal">set_tmp_dir</code> parameter
562 in the environment's <code class="literal">DB_CONFIG</code> file.
563 Note that the <code class="literal">set_tmp_dir</code>
564 parameter overrides any value set by the
565
566 <code class="methodname">DbEnv::set_tmp_dir()</code>
567
568 method.
569 </p>
570 </td>
571 </tr>
572 <tr>
573 <td>Metadata files</td>
574 <td>
575 <p>
576 You can cause persistent metadata files required by the
577 replicated applications to be created
578 in a directory other than the environment home
579 directory by using the
580
581 <code class="methodname">DbEnv::set_metadata_dir()</code>
582
583 method. The directory identified
584 here must exist. If a relative path is
585 provided, then the directory location is
586 resolved relative to the environment's home
587 directory.
588 </p>
589
590 <p>
591 You can also set a metadata directory location
592 by using the
593 <code class="literal">set_metadata_dir</code> parameter
594 in the environment's <code class="literal">DB_CONFIG</code> file.
595 Note that the <code class="literal">set_metadata_dir</code>
596 parameter overrides any value set by the
597
598 <code class="methodname">DbEnv::set_metadata_dir()</code>
599
600 method.
601 </p>
602 </td>
603 </tr>
604 <tr>
605 <td>Region files</td>
606 <td>
607 If backed by the filesystem, region
608 files are always placed in the environment home
609 directory.
610 </td>
611 </tr>
612 </tbody>
613 </table>
614 </div>
615 <p>
616 Note that the <code class="literal">DB_CONFIG</code> must reside in the
617 environment home directory. Parameters are specified in it one
618 parameter to a line. Each parameter is followed by a space,
619 which is followed by the parameter value. For example:
620 </p>
621 <pre class="programlisting"> add_data_dir /export1/db/env_data_files </pre>
622 </div>
623 </div>
624 <div class="sect2" lang="en" xml:lang="en">
625 <div class="titlepage">
626 <div>
627 <div>
628 <h3 class="title"><a id="errorsupport"></a>Error Support</h3>
629 </div>
630 </div>
631 </div>
632 <p>
633 To simplify error handling and to aid in application debugging, environments offer several useful
634 methods.
635
636 <span>Note that many of these
637 methods are identical to the error handling methods available for the
638
639 <span>Db</span>
640
641
642 <span>class.</span>
643
644 </span>
645
646 They are:
647 </p>
648 <div class="itemizedlist">
649 <ul type="disc">
650 <li>
651 <p>
652 <code class="methodname">set_error_stream()</code>
653
654 </p>
655 <p>
656 Sets the
657 <span>C++ <code class="classname">ostream</code></span>
658
659 to be used for displaying error messages issued by the DB library.
660 </p>
661 </li>
662 <li>
663 <p>
664 <code class="methodname">set_errcall()</code>
665
666 </p>
667 <p>
668 Defines the function that is called when an error message is
669 issued by DB. The error prefix and message are passed to
670 this callback. It is up to the application to display this
671 information correctly.
672 </p>
673 <p>
674 This is the recommended way to get error messages from
675 DB.
676 </p>
677 </li>
678 <li>
679 <p>
680 <code class="methodname">set_errfile()</code>
681 </p>
682 <p>
683 Sets the C library <code class="literal">FILE *</code> to be used for
684 displaying error messages issued by the DB library.
685 </p>
686 </li>
687 <li>
688 <p>
689 <code class="methodname">set_errpfx()</code>
690
691 </p>
692 <p>
693 Sets the prefix used to for any error messages issued by the
694 DB library.
695 </p>
696 </li>
697 <li>
698 <p>
699 <code class="methodname">err()</code>
700 </p>
701 <p>
702 Issues an error message based upon a DB error code a message text that you supply.
703 The error message is sent to the
704 callback function as defined by <code class="methodname">set_errcall()</code>.
705 If that method has not been used, then the error message is sent to the
706 file defined by
707
708 <span>
709 <code class="methodname">set_errfile()</code> or <code class="methodname">set_error_stream()</code>.
710 </span>
711 If none of these methods have been used, then the error message is sent to
712 standard error.
713 </p>
714 <p>
715 The error message consists of the prefix string
716 (as defined by <code class="methodname">set_errprefix()</code>),
717 an optional <code class="literal">printf</code>-style formatted message,
718 the DB error message associated with the supplied error code,
719 and a trailing newline.
720 </p>
721 </li>
722 <li>
723 <p>
724 <code class="methodname">errx()</code>
725 </p>
726 <p>
727 Behaves identically to <code class="methodname">err()</code> except
728 that you do not provide the DB error code and so
729 the DB message text is not displayed.
730 </p>
731 </li>
732 </ul>
733 </div>
734 <p>
735 In addition, you can use the <code class="function">db_strerror()</code>
736 function to directly return the error string that corresponds to a
737 particular error number. For more information on the
738 <code class="function">db_strerror()</code> function, see the <code class="literal">Error Returns</code>
739 section of the <em class="citetitle">Getting Started with Berkeley DB</em> guide.
740 </p>
741 </div>
742 <div class="sect2" lang="en" xml:lang="en">
743 <div class="titlepage">
744 <div>
745 <div>
746 <h3 class="title"><a id="sharedmemory"></a>Shared Memory Regions</h3>
747 </div>
748 </div>
749 </div>
750 <p>
751 The subsystems that you enable for an environment (in our case,
752 transaction, logging, locking, and the memory pool)
753 are described by one or more regions. The regions contain all of the
754 state information that needs to be shared among threads and/or
755 processes using the environment.
756 </p>
757 <p>
758 Regions may be backed by the file system, by heap memory, or by
759 system shared memory.
760 </p>
761 <div class="sect3" lang="en" xml:lang="en">
762 <div class="titlepage">
763 <div>
764 <div>
765 <h4 class="title"><a id="filebackedregions"></a>Regions Backed by Files</h4>
766 </div>
767 </div>
768 </div>
769 <p>
770 By default, shared memory regions are created as files in the environment's
771 home directory (<span class="emphasis"><em>not</em></span> the environment's data
772 directory). If it is available, the POSIX <code class="literal">mmap</code>
773 interface is used to map these files into your application's
774 address space. If <code class="literal">mmap</code>
775 is not available, then the UNIX <code class="literal">shmget</code> interfaces
776 are used instead (again, if they are available).
777 </p>
778 <p>
779 In this default case, the region files are named
780 <code class="literal">__db.###</code>
781 (for example, <code class="literal">__db.001</code>, <code class="literal">__db.002</code>,
782 and so on).
783 </p>
784 </div>
785 <div class="sect3" lang="en" xml:lang="en">
786 <div class="titlepage">
787 <div>
788 <div>
789 <h4 class="title"><a id="heapbackedregions"></a>Regions Backed by Heap Memory</h4>
790 </div>
791 </div>
792 </div>
793 <p>
794 If heap memory is used to back your shared memory regions, then
795 you can only open a single handle for the environment. This
796 means that the environment cannot be accessed by multiple
797 processes. In this case, the regions are managed only in
798 memory, and they are not written to the filesystem. You
799 indicate that heap memory is to be used for the region files by
800 specifying
801 <span>
802 <code class="literal">DB_PRIVATE</code> to the
803
804 <code class="methodname">DbEnv::open()</code>
805 method.
806 </span>
807
808
809 </p>
810 <p>
811 Note that you can also set this flag by using the
812 <code class="literal">set_open_flags</code> parameter in the
813 <code class="literal">DB_CONFIG</code> file. See the
814 <em class="citetitle">Berkeley DB C API Reference Guide</em> for more information.
815 </p>
816 <p>
817 (For an example of an entirely in-memory transactional
818 application, see
819 <span>
820 <a class="xref" href="inmem_txnexample_c.html" title="In-Memory Transaction Example">In-Memory Transaction Example</a>.)
821 </span>
822
823
824
825 </p>
826 </div>
827 <div class="sect3" lang="en" xml:lang="en">
828 <div class="titlepage">
829 <div>
830 <div>
831 <h4 class="title"><a id="systembackedregions"></a>Regions Backed by System Memory</h4>
832 </div>
833 </div>
834 </div>
835 <p>
836 Finally, you can cause system memory to be used for your
837 regions instead of memory-mapped files. You do this by providing
838 <span>
839 <code class="literal">DB_SYSTEM_MEM</code> to the
840
841 <code class="methodname">DbEnv::open()</code>
842 method.
843 </span>
844
845
846 </p>
847 <p>
848 When region files are backed by system memory, DB creates a
849 single file in the environment's home directory. This file
850 contains information necessary to identify the system shared
851 memory in use by the environment. By creating this file, DB
852 enables multiple processes to share the environment.
853 </p>
854 <p>
855 The system memory that is used is architecture-dependent. For
856 example, on systems supporting X/Open-style shared memory
857 interfaces, such as UNIX systems, the <code class="literal">shmget(2)</code>
858 and related System V IPC interfaces are used.
859
860 <span>
861
862 Additionally, VxWorks systems use system memory. In these cases,
863 an initial segment ID must be specified by the application to
864 ensure that applications do not overwrite each other's
865 environments, so that the number of segments created does not
866 grow without bounds. See the
867
868
869 <code class="methodname">DbEnv::set_shm_key()</code>
870
871 method for more information.
872 </span>
873 </p>
874 <p>
875 On Windows platforms, the use of system memory for the region files
876 is problematic because the operating system uses reference counting
877 to clean up shared objects in the paging file automatically. In
878 addition, the default access permissions for shared objects are
879 different from files, which may cause problems when an environment
880 is accessed by multiple processes running as different users. See
881 <a class="ulink" href="" target="_top">Windows notes</a>
882 or more information.
883 </p>
884 </div>
885 </div>
886 <div class="sect2" lang="en" xml:lang="en">
887 <div class="titlepage">
888 <div>
889 <div>
890 <h3 class="title"><a id="security"></a>Security Considerations</h3>
891 </div>
892 </div>
893 </div>
894 <p>
895 When using environments, there are some security considerations to
896 keep in mind:
897 </p>
898 <div class="itemizedlist">
899 <ul type="disc">
900 <li>
901 <p>
902 Database environment permissions
903 </p>
904 <p>
905 The directory used for the environment
906 should have its permissions set to ensure that files in the
907 environment are not accessible to users without appropriate
908 permissions. Applications that add to the user's permissions
909 (for example, UNIX <code class="literal">setuid</code> or
910 <code class="literal">setgid</code> applications), must be
911 carefully checked to not permit illegal use of those
912 permissions such as general file access in the environment
913 directory.
914 </p>
915 </li>
916 <li>
917 <p>
918 Environment variables
919 </p>
920 <p>
921 Setting
922
923 <span>
924 the <code class="literal">DB_USE_ENVIRON</code> or
925 <code class="literal">DB_USE_ENVIRON_ROOT</code> flags
926 </span>
927
928
929 so that environment variables can be used during file naming
930 can be dangerous. Setting those flags in DB
931 applications with additional permissions (for example, UNIX
932 <code class="literal">setuid</code> or <code class="literal">setgid</code>
933 applications) could potentially allow users
934 to read and write databases to which they would not normally
935 have access.
936 </p>
937 <p>
938 For example, suppose you write a DB application
939 that runs <code class="literal">setuid</code>. This means that
940 when the application runs, it does so under a
941 userid different than that of the application's caller.
942 This is especially problematic if the application is
943 granting stronger privileges to a user than the user
944 might ordinarily have.
945 </p>
946 <p>
947 Now, if
948 <span>
949 the <code class="literal">DB_USE_ENVIRON</code> or
950 <code class="literal">DB_USE_ENVIRON_ROOT</code> flags
951 are set for the environment,
952 </span>
953
954
955
956 then the environment that the application is
957 using is modifiable using the
958 <code class="literal">DB_HOME</code> environment variable. In
959 this scenario, if the uid used by the application has
960 sufficiently broad privileges, then the application's caller
961 can read and/or write databases owned by another user
962 simply by setting his
963 <code class="literal">DB_HOME</code> environment variable to the
964 environment used by that other user.
965 </p>
966 <p>
967 Note that this scenario need not be malicious; the
968 wrong environment could be used by the application
969 simply by inadvertently specifying the wrong path to
970 <code class="literal">DB_HOME</code>.
971 </p>
972 <p>
973 As always, you should use <code class="literal">setuid</code>
974 sparingly, if at all. But if you do use
975 <code class="literal">setuid</code>, then you should refrain from
976 specifying
977 <span>
978 the <code class="literal">DB_USE_ENVIRON</code> or
979 <code class="literal">DB_USE_ENVIRON_ROOT</code> flags
980 </span>
981
982 for the environment open. And, of course, if you must
983 use <code class="literal">setuid</code>, then make sure you use
984 the weakest uid possible – preferably one that is
985 used only by the application itself.
986 </p>
987 </li>
988 <li>
989 <p>
990 File permissions
991 </p>
992 <p>
993 By default, DB always creates database and log files readable and
994 writable by the owner and the group (that is,
995 <code class="literal">S_IRUSR</code>,
996 <code class="literal">S_IWUSR</code>, <code class="literal">S_IRGRP</code> and
997 <code class="literal">S_IWGRP</code>; or octal mode 0660 on historic
998 UNIX systems). The group ownership of created files is based
999 on the system and directory defaults, and is not further
1000 specified by DB.
1001 </p>
1002 </li>
1003 <li>
1004 <p>
1005 Temporary backing files
1006 </p>
1007 <p>
1008 If an unnamed database is created and the cache is too small
1009 to hold the database in memory, Berkeley DB will create a
1010 temporary physical file to enable it to page the database to
1011 disk as needed. In this case, environment variables such as
1012 <code class="literal">TMPDIR</code> may be used to specify the
1013 location of that temporary file. Although temporary backing
1014 files are created readable and writable by the owner only
1015 (<code class="literal">S_IRUSR</code> and <code class="literal">S_IWUSR</code>,
1016 or octal mode 0600 on historic UNIX systems), some
1017 filesystems may not sufficiently protect temporary files
1018 created in random directories from improper access. To be
1019 absolutely safe, applications storing sensitive data in
1020 unnamed databases should use the
1021
1022 <code class="methodname">DbEnv::set_tmp_dir()</code>
1023
1024 method to specify a temporary directory with known permissions.
1025 </p>
1026 </li>
1027 </ul>
1028 </div>
1029 </div>
1030 </div>
1031 </div>
1032 <div class="navfooter">
1033 <hr />
1034 <table width="100%" summary="Navigation footer">
1035 <tr>
1036 <td width="40%" align="left"><a accesskey="p" href="perftune-intro.html">Prev</a> </td>
1037 <td width="20%" align="center"> </td>
1038 <td width="40%" align="right"> <a accesskey="n" href="envopen.html">Next</a></td>
1039 </tr>
1040 <tr>
1041 <td width="40%" align="left" valign="top">Performance Tuning </td>
1042 <td width="20%" align="center">
1043 <a accesskey="h" href="index.html">Home</a>
1044 </td>
1045 <td width="40%" align="right" valign="top"> Opening a Transactional Environment and
1046 <span>Database</span>
1047
1048
1049 </td>
1050 </tr>
1051 </table>
1052 </div>
1053 </body>
1054 </html>