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