1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>
Using the Sleepycat Java Collections API
</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
<link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" />
<link rel="up" href="collectionOverview.html" title="AppendixA. API Notes and Details " />
<link rel="previous" href="collectionOverview.html" title="AppendixA. API Notes and Details " />
<link rel="next" href="UsingStoredCollections.html" title=" Using Stored Collections " />
</head>
<body>
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">
Using the Sleepycat Java Collections API
</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="collectionOverview.html">Prev</a></td>
<th width="60%" align="center">AppendixA.
API Notes and Details
</th>
<td width="20%" align="right"><a accesskey="n" href="UsingStoredCollections.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="UsingCollectionsAPI"></a>
Using the Sleepycat Java Collections API
</h2>
</div>
</div>
<div></div>
</div>
<p>
An
<a href="../../java/com/sleepycat/db/Environment.html" target="_top">Environment</a>
manages the resources for one or more data stores. A
<a href="../../java/com/sleepycat/db/Database.html" target="_top">Database</a>
object
represents a single database and is created via a method on the
environment object.
<a href="../../java/com/sleepycat/db/SecondaryDatabase.html" target="_top">SecondaryDatabase</a>
objects represent an index associated with a primary database.
<span>
An access method must be chosen for each database and secondary
database.
</span>
Primary and secondary databases are then used to create stored
collection objects, as described in
<a href="UsingStoredCollections.html">
Using Stored Collections
</a>.
</p>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="UsingTransactions"></a>
Using Transactions
</h3>
</div>
</div>
<div></div>
</div>
<p>
Once you have an environment, one or more databases, and one or
more stored collections, you are ready to access (read and write)
stored data. For a transactional environment, a transaction must be
started before accessing data, and must be committed or aborted
after access is complete. The Sleepycat Java Collections API provides several
ways of managing transactions.
</p>
<p>
The recommended technique is to use the
<a href="../../java/com/sleepycat/collections/TransactionRunner.html" target="_top">TransactionRunner</a>
class along with your own implementation of the
<a href="../../java/com/sleepycat/collections/TransactionWorker.html" target="_top">TransactionWorker</a>
interface.
<a href="../../java/com/sleepycat/collections/TransactionRunner.html" target="_top">TransactionRunner</a>
will call your
<a href="../../java/com/sleepycat/collections/TransactionWorker.html" target="_top">TransactionWorker</a>
implementation class to perform the data access or work of the
transaction. This technique has the following benefits:
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
Transaction exceptions will be handled transparently and
retries will be performed when deadlocks are detected.
</p>
</li>
<li>
<p>
The transaction will automatically be committed if your
<a href="../../java/com/sleepycat/collections/TransactionWorker.html#doWork()" target="_top">TransactionWorker.doWork()</a>
method returns normally, or will be
aborted if <tt class="methodname">doWork()</tt> throws an exception.
</p>
</li>
<li>
<p>
<tt class="classname">TransactionRunner</tt> can be used for non-transactional
environments as well, allowing you to write your application
independently of the environment.
</p>
</li>
</ul>
</div>
<p>
If you don't want to use
<a href="../../java/com/sleepycat/collections/TransactionRunner.html" target="_top">TransactionRunner</a>,
the alternative is to use the
<a href="../../java/com/sleepycat/collections/CurrentTransaction.html" target="_top">CurrentTransaction</a>
class.
</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>
Obtain a CurrentTransaction instance by calling the
<a href="../../java/com/sleepycat/collections/CurrentTransaction.html#getInstance(com.sleepycat.db.Environment)" target="_top">CurrentTransaction.getInstance</a>
method. The instance returned
can be used by all threads in a program.
</p>
</li>
<li>
<p>
Use
<a href="../../java/com/sleepycat/collections/CurrentTransaction.html#beginTransaction(com.sleepycat.db.TransactionConfig)" target="_top">CurrentTransaction.beginTransaction()</a>,
<a href="../../java/com/sleepycat/collections/CurrentTransaction.html#commitTransaction()" target="_top">CurrentTransaction.commitTransaction()</a>
and
<a href="../../java/com/sleepycat/collections/CurrentTransaction.html#abortTransaction()" target="_top">CurrentTransaction.abortTransaction()</a>
to directly begin, commit and abort transactions.
</p>
</li>
</ol>
</div>
<p>
If you choose to use CurrentTransaction directly you must handle
the
<a href="../../java/com/sleepycat/db/DeadlockException.html" target="_top">DeadlockException</a>
exception and perform retries yourself. Also note that
CurrentTransaction may only be used in a transactional
environment.
</p>
<p>
The Sleepycat Java Collections API supports nested transactions. If
<a href="../../java/com/sleepycat/collections/TransactionRunner.html#run(com.sleepycat.collections.TransactionWorker)" target="_top">TransactionRunner.run(com.sleepycat.collections.TransactionWorker)</a>
or
<a href="../../java/com/sleepycat/collections/CurrentTransaction.html#beginTransaction(com.sleepycat.db.TransactionConfig)" target="_top">CurrentTransaction.beginTransaction()</a>
,
is called while another transaction is active, a child transaction
is created. When
<a href="../../java/com/sleepycat/collections/TransactionRunner.html#run(com.sleepycat.collections.TransactionWorker)" target="_top">TransactionRunner.run(com.sleepycat.collections.TransactionWorker)</a>
returns, or when
<a href="../../java/com/sleepycat/collections/CurrentTransaction.html#commitTransaction()" target="_top">CurrentTransaction.commitTransaction()</a>
or
<a href="../../java/com/sleepycat/collections/CurrentTransaction.html#abortTransaction()" target="_top">CurrentTransaction.abortTransaction()</a>
is called, the parent transaction becomes active again. Note that
because only one transaction is active per-thread, it is impossible
to accidentally use a parent transaction while a child transaction
is active.
</p>
<p>
The Sleepycat Java Collections API supports transaction auto-commit.
If no transaction is active and a write operation is requested for
a transactional database, auto-commit is used automatically.
</p>
<p>
The Sleepycat Java Collections API also supports transaction
dirty-read via the
<a href="../../java/com/sleepycat/collections/StoredCollections.html" target="_top">StoredCollections</a>
class. When dirty-read is enabled for a collection, data will be
read that has been modified by another transaction but not
committed. Using dirty-read can improve concurrency since reading
will not wait for other transactions to complete. For a
non-transactional container, dirty-read has no effect. See
<a href="../../java/com/sleepycat/collections/StoredCollections.html" target="_top">StoredCollections</a>
for how to create a dirty-read collection.
</p>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="TransactionRollback"></a>
Transaction Rollback
</h3>
</div>
</div>
<div></div>
</div>
<p>
When a transaction is aborted (or rolled back) the application
is responsible for discarding references to any data objects that
were modified during the transaction. Since the Sleepycat Java Collections
API treats data by value, not by reference, neither the data
objects nor the Sleepycat Java Collections API objects contain status
information indicating whether the data objects are 1- in sync with
the database, 2- dirty (contain changes that have not been written
to the database), 3- stale (were read previously but have become
out of sync with changes made to the database), or 4- contain
changes that cannot be committed because of an aborted
transaction.
</p>
<p>
For example, a given data object will reflect the current state
of the database after reading it within a transaction. If the
object is then modified it will be out of sync with the database.
When the modified object is written to the database it will then be
in sync again. But if the transaction is aborted the object will
then be out of sync with the database. References to objects for aborted
transactions
should no longer be used. When these objects are needed later they
should be read fresh from the database.
</p>
<p>
When an existing stored object is to be updated, special care
should be taken to read the data, then modify it, and then write it
to the database, all within a single transaction. If a stale data
object (an object that was read previously but has since been
changed in the database) is modified and then written to the
database, database changes may be overwritten unintentionally.
</p>
<p>
When an application enforces rules about concurrent access to
specific data objects or all data objects, the rules described here
can be relaxed. For example, if the application knows that a
certain object is only modified in one place, it may be able to
reliably keep a current copy of that object. In that case, it is
not necessary to reread the object before updating it. That said,
if arbitrary concurrent access is to be supported, the safest
approach is to always read data before modifying it within a single
transaction.
</p>
<p>
Similar concerns apply to using data that may have become stale.
If the application depends on current data, it should be read fresh
from the database just before it is used.
</p>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="SelectingAccessMethods"></a>Selecting Access Methods</h3>
</div>
</div>
<div></div>
</div>
<p>
For each data store and secondary index, you must choose from one of the
access methods in the table below.
The access method determines not only whether sorted keys or duplicate
keys are supported, but also what types of collection views may be used
and what restrictions are imposed on the collection views.
</p>
<div class="informaltable">
<table border="1" width="80%">
<colgroup>
<col />
<col />
<col />
<col />
<col />
<col />
</colgroup>
<thead>
<tr>
<th>Access Method</th>
<th>Ordered</th>
<th>Duplicates</th>
<th>Record Numbers</th>
<th>Database Type</th>
<th><tt class="classname">DatabaseConfig</tt> Method</th>
</tr>
</thead>
<tbody>
<tr>
<td>
BTREE-UNIQUE
</td>
<td>
Yes
</td>
<td>
No
</td>
<td>
No
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseType.html#BTREE" target="_top">BTREE</a>
</td>
<td>
None
</td>
</tr>
<tr>
<td>
BTREE-DUP
</td>
<td>
Yes
</td>
<td>
Yes, Unsorted
</td>
<td>
No
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseType.html#BTREE" target="_top">BTREE</a>
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseConfig.html#setUnsortedDuplicates(boolean)" target="_top">setUnsortedDuplicates</a>
</td>
</tr>
<tr>
<td>
BTREE-DUPSORT
</td>
<td>
Yes
</td>
<td>
Yes, Sorted
</td>
<td>
No
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseType.html#BTREE" target="_top">BTREE</a>
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseConfig.html#setSortedDuplicates(boolean)" target="_top">setSortedDuplicates</a>
</td>
</tr>
<tr>
<td>
BTREE-RECNUM
</td>
<td>
Yes
</td>
<td>
No
</td>
<td>
Yes, Renumbered
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseType.html#BTREE" target="_top">BTREE</a>
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseConfig.html#setBtreeRecordNumbers(boolean)" target="_top">setBtreeRecordNumbers</a>
</td>
</tr>
<tr>
<td>
HASH-UNIQUE
</td>
<td>
No
</td>
<td>
No
</td>
<td>
No
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseType.html#HASH" target="_top">HASH</a>
</td>
<td>
None
</td>
</tr>
<tr>
<td>
HASH-DUP
</td>
<td>
No
</td>
<td>
Yes, Unsorted
</td>
<td>
No
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseType.html#HASH" target="_top">HASH</a>
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseConfig.html#setUnsortedDuplicates(boolean)" target="_top">setUnsortedDuplicates</a>
</td>
</tr>
<tr>
<td>
HASH-DUPSORT
</td>
<td>
No
</td>
<td>
Yes, Sorted
</td>
<td>
No
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseType.html#HASH" target="_top">HASH</a>
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseConfig.html#setSortedDuplicates(boolean)" target="_top">setSortedDuplicates</a>
</td>
</tr>
<tr>
<td>
QUEUE
</td>
<td>
Yes
</td>
<td>
No
</td>
<td>
Yes, Fixed
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseType.html#QUEUE" target="_top">QUEUE</a>
</td>
<td>
None
</td>
</tr>
<tr>
<td>
RECNO
</td>
<td>
Yes
</td>
<td>
No
</td>
<td>
Yes, Fixed
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseType.html#RECNO" target="_top">RECNO</a>
</td>
<td>
None
</td>
</tr>
<tr>
<td>
RECNO-RENUMBER
</td>
<td>
Yes
</td>
<td>
No
</td>
<td>
Yes, Renumbered
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseType.html#RECNO" target="_top">RECNO</a>
</td>
<td>
<a href="../../java/com/sleepycat/db/DatabaseConfig.html#setRenumbering(boolean)" target="_top">setRenumbering</a>
</td>
</tr>
</tbody>
</table>
</div>
<p>
Please see
<span class="html"><a href="../../ref/am_conf/intro.html" target="_top">Available Access Methods</a> in</span>
the <i class="citetitle">Berkeley DB Programmer's Reference Guide</i>
for more information on access method configuration.
</p>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="AccessMethodRestrictions"></a>
Access Method Restrictions
</h3>
</div>
</div>
<div></div>
</div>
<p>
The restrictions imposed by the access method on the database
model are:
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
If keys are ordered then data may be enumerated in key order and
key ranges may be used to form subsets of a data store. The
<tt class="classname">SortedMap</tt> and <tt class="classname">SortedSet</tt>
interfaces are supported for collections with ordered keys.
</p>
</li>
<li>
<p>
If duplicates are allowed then more than one value may be
associated with the same key. This means that the data store cannot
be strictly considered a map — it is really a multi-map. See
<a href="UsingStoredCollections.html">
Using Stored Collections
</a>
for implications on the use of the collection interfaces.
</p>
</li>
<li>
<p>
If duplicate keys are allowed for a data store then the data
store may not have secondary indices.
</p>
</li>
<li>
<p>
For secondary indices with duplicates, the duplicates must be
sorted. This restriction is imposed by the Sleepycat Java
Collections API.
</p>
</li>
<li>
<p>
With sorted duplicates, all values for the same key must be
distinct.
</p>
</li>
<li>
<p>
If duplicates are unsorted, then values for the same key must be
distinct.
</p>
</li>
<li>
<p>
If record number keys are used, the the number of records is
limited to the maximum value of an unsigned 32-bit integer.
</p>
</li>
<li>
<p>
If reocrd number keys are renumbered, then standard List
add/remove behavior is supported but concurrency/performance is
reduced.
</p>
</li>
</ul>
</div>
<p>
See
<a href="UsingStoredCollections.html">
Using Stored Collections
</a>
for more information on how access methods impact the use of stored
collections.
</p>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="collectionOverview.html">Prev</a></td>
<td width="20%" align="center">
<a accesskey="u" href="collectionOverview.html">Up</a>
</td>
<td width="40%" align="right"><a accesskey="n" href="UsingStoredCollections.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">AppendixA.
API Notes and Details
</td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top">
Using Stored Collections
</td>
</tr>
</table>
</div>
</body>
</html>
|