Debian Codegraph @

docs/gsg_txn/JAVA/isolation.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>Isolation</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="txnconcurrency.html" title="Chapter 4. Concurrency" />
11 <link rel="prev" href="lockingsubsystem.html" title="The Locking Subsystem" />
12 <link rel="next" href="txn_ccursor.html" title="Transactional Cursors and Concurrent Applications" />
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">Isolation</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="lockingsubsystem.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 4. Concurrency</th>
26 <td width="20%" align="right"> <a accesskey="n" href="txn_ccursor.html">Next</a></td>
27 </tr>
28 </table>
29 <hr />
30 </div>
31 <div class="sect1" lang="en" xml:lang="en">
32 <div class="titlepage">
33 <div>
34 <div>
35 <h2 class="title" style="clear: both"><a id="isolation"></a>Isolation</h2>
36 </div>
37 </div>
38 </div>
39 <div class="toc">
40 <dl>
41 <dt>
42 <span class="sect2">
43 <a href="isolation.html#degreesofisolation">Supported Degrees of Isolation</a>
44 </span>
45 </dt>
46 <dt>
47 <span class="sect2">
48 <a href="isolation.html#dirtyreads">Reading Uncommitted Data</a>
49 </span>
50 </dt>
51 <dt>
52 <span class="sect2">
53 <a href="isolation.html#readcommitted">Committed Reads</a>
54 </span>
55 </dt>
56 <dt>
57 <span class="sect2">
58 <a href="isolation.html#snapshot_isolation">Using Snapshot Isolation</a>
59 </span>
60 </dt>
61 </dl>
62 </div>
63 <p>
64 Isolation guarantees are an important aspect of transactional
65 protection. Transactions
66 ensure the data your transaction is working with will not be changed by some other transaction.
67 Moreover, the modifications made by a transaction will never be viewable outside of that transaction until
68 the changes have been committed.
69 </p>
70 <p>
71 That said, there are different degrees of isolation, and you can choose to relax your isolation
72 guarantees to one degree or another depending on your application's requirements. The primary reason why
73 you might want to do this is because of performance; the more isolation you ask your transactions to
74 provide, the more locking that your application must do. With more locking comes a greater chance of
75 blocking, which in turn causes your threads to pause while waiting for a lock. Therefore, by relaxing
76 your isolation guarantees, you can <span class="emphasis"><em>potentially</em></span> improve your application's throughput.
77 Whether you actually see any improvement depends, of course, on
78 the nature of your application's data and transactions.
79 </p>
80 <div class="sect2" lang="en" xml:lang="en">
81 <div class="titlepage">
82 <div>
83 <div>
84 <h3 class="title"><a id="degreesofisolation"></a>Supported Degrees of Isolation</h3>
85 </div>
86 </div>
87 </div>
88 <p>
89 DB supports the following levels of isolation:
90 </p>
91 <div class="informaltable">
92 <table border="1" width="80%">
93 <colgroup>
94 <col />
95 <col />
96 <col />
97 </colgroup>
98 <thead>
99 <tr>
100 <th>Degree</th>
101 <th>ANSI Term</th>
102 <th>Definition</th>
103 </tr>
104 </thead>
105 <tbody>
106 <tr>
107 <td>1</td>
108 <td>READ UNCOMMITTED</td>
109 <td>
110 Uncommitted reads means that one transaction will never
111 overwrite another transaction's dirty data. Dirty data is
112 data that a transaction has modified but not yet committed
113 to the underlying data store. However, uncommitted reads allows a
114 transaction to see data dirtied by another
115 transaction. In addition, a transaction may read data
116 dirtied by another transaction, but which subsequently
117 is aborted by that other transaction. In this latter
118 case, the reading transaction may be reading data that
119 never really existed in the database.
120 </td>
121 </tr>
122 <tr>
123 <td>2</td>
124 <td>READ COMMITTED</td>
125 <td>
126 <p>
127 Committed read isolation means that degree 1 is observed, except that dirty data is never read.
128 </p>
129 <p>
130 In addition, this isolation level guarantees that data will never change so long as
131 it is addressed by the cursor, but the data may change before the reading cursor is closed.
132 In the case of a transaction, data at the current
133 cursor position will not change, but once the cursor
134 moves, the previous referenced data can change. This
135 means that readers release read locks before the cursor
136 is closed, and therefore, before the transaction
137 completes. Note that this level of isolation causes the
138 cursor to operate in exactly the same way as it does in
139 the absence of a transaction.
140 </p>
141 </td>
142 </tr>
143 <tr>
144 <td>3</td>
145 <td>SERIALIZABLE</td>
146 <td>
147 <p>
148 Committed read is observed, plus the data read by a transaction, T,
149 will never be dirtied by another transaction before T completes.
150 This means that both read and write locks are not
151 released until the transaction completes.
152 </p>
153 <p>
154 <span>
155 In addition,
156 </span>
157
158
159
160 no transactions will see phantoms. Phantoms are records
161 returned as a result of a search, but which were not seen by
162 the same transaction when the identical
163 search criteria was previously used.
164 </p>
165 <p>
166 This is DB's default isolation guarantee.
167 </p>
168 </td>
169 </tr>
170 </tbody>
171 </table>
172 </div>
173 <p>
174
175 By default, DB transactions and transactional cursors offer
176 <span>
177 serializable isolation.
178 </span>
179
180
181 You can optionally reduce your isolation level by configuring DB to use
182 uncommitted read isolation. See
183 <a class="xref" href="isolation.html#dirtyreads" title="Reading Uncommitted Data">Reading Uncommitted Data</a>
184 for more information.
185
186 You can also configure DB to use committed read isolation. See
187 <a class="xref" href="isolation.html#readcommitted" title="Committed Reads">Committed Reads</a>
188 for more information.
189
190 </p>
191 <p>
192 Finally, in addition to DB's normal degrees of isolation, you
193 can also use <span class="emphasis"><em>snapshot isolation</em></span>. This allows
194 you to avoid the read locks that serializable isolation requires. See
195 <a class="xref" href="isolation.html#snapshot_isolation" title="Using Snapshot Isolation">Using Snapshot Isolation</a>
196 for details.
197 </p>
198 </div>
199 <div class="sect2" lang="en" xml:lang="en">
200 <div class="titlepage">
201 <div>
202 <div>
203 <h3 class="title"><a id="dirtyreads"></a>Reading Uncommitted Data</h3>
204 </div>
205 </div>
206 </div>
207 <p>
208 Berkeley DB allows you to configure your application to read data that has been modified but not yet
209 committed by another transaction; that is, dirty data. When you do this, you
210 may see a performance benefit by allowing your
211 application to not have to block waiting for write locks. On the other hand, the data that your
212 application is reading may change before the transaction has completed.
213 </p>
214 <p>
215 When used with transactions, uncommitted reads means that one transaction can see data
216 modified but not yet committed by another transaction. When
217 used with transactional cursors, uncommitted reads means
218 that any database reader can see data modified by the
219 cursor before the cursor's transaction has committed.
220 </p>
221 <p>
222 Because of this, uncommitted reads allow a transaction to read data
223 that may subsequently be aborted by another transaction. In
224 this case, the reading transaction will have read data that
225 never really existed in the database.
226 </p>
227 <p>
228 To configure your application to read uncommitted data:
229 </p>
230 <div class="orderedlist">
231 <ol type="1">
232 <li>
233 <p>
234 Open your database such that it will allow uncommitted reads. You do this by
235
236 <span>
237 specifying <code class="literal">true</code> to
238
239 <span>
240 <code class="methodname">DatabaseConfig.setReadUncommitted()</code>.
241 (If you are using the DPL, you must
242 provide this
243 <code class="classname">DatabaseConfig</code>
244 object to the entity store using the
245 <code class="methodname">EntityStore.setPrimaryConfig()</code>
246 method.)
247 </span>
248 </span>
249 </p>
250 </li>
251 <li>
252 <p>
253
254 <span>
255 Specify that you want to use uncommitted reads when you
256 create a transaction or open the cursor.
257 To do this, you use the <code class="methodname">setReadUncommitted()</code>
258 method on the relevant configuration object
259 (<code class="classname">TransactionConfig</code> or
260 <code class="classname">CursorConfig</code>).
261 </span>
262
263 </p>
264 </li>
265 </ol>
266 </div>
267 <p>
268 For example, the following opens the database such that it supports uncommitted reads, and then creates a
269 transaction that causes all reads performed within it to use uncommitted reads. Remember that simply opening
270 the database to support uncommitted reads is not enough; you must also declare your read operations to be
271 performed using uncommitted reads.
272 </p>
273 <pre class="programlisting">package db.txn;
274
275 import com.sleepycat.db.Database;
276 import com.sleepycat.db.DatabaseConfig;
277 import com.sleepycat.db.DatabaseEntry;
278 import com.sleepycat.db.DatabaseException;
279 import com.sleepycat.db.DatabaseType;
280 import com.sleepycat.db.Environment;
281 import com.sleepycat.db.EnvironmentConfig;
282 import com.sleepycat.db.Transaction;
283 import com.sleepycat.db.TransactionConfig;
284
285 import java.io.File;
286
287 ...
288
289 Database myDatabase = null;
290 Environment myEnv = null;
291 try {
292 EnvironmentConfig myEnvConfig = new EnvironmentConfig();
293 myEnvConfig.setTransactional(true);
294 myEnvConfig.setInitializeCache(true);
295 myEnvConfig.setInitializeLocking(true);
296 myEnvConfig.setInitializeLogging(true);
297
298 myEnv = new Environment(new File("/my/env/home"),
299 myEnvConfig);
300
301 // Open the database.
302 DatabaseConfig dbConfig = new DatabaseConfig();
303 dbConfig.setTransactional(true);
304 dbConfig.setType(DatabaseType.BTREE);
305 dbConfig.setAllowCreate(true);
306 dbConfig.setReadUncommitted(true); // Enable uncommitted reads.
307 myDatabase = myEnv.openDatabase(null, // txn handle
308 "sampleDatabase", // db file name
309 null, // db name
310 dbConfig);
311 TransactionConfig txnConfig = new TransactionConfig();
312 txnConfig.setReadUncommitted(true); // Use uncommitted reads
313 // for this transaction.
314 Transaction txn = myEnv.beginTransaction(null, txnConfig);
315
316 // From here, you perform your database reads and writes as normal,
317 // committing and aborting the transactions as is necessary, and
318 // testing for deadlock exceptions as normal (omitted for brevity).
319
320 ...</pre>
321 <p>
322 If you are using the DPL:
323 </p>
324 <pre class="programlisting">package persist.txn;
325
326 import com.sleepycat.db.DatabaseConfig;
327 import com.sleepycat.db.DatabaseEntry;
328 import com.sleepycat.db.DatabaseException;
329 import com.sleepycat.db.DatabaseType;
330 import com.sleepycat.db.Environment;
331 import com.sleepycat.db.EnvironmentConfig;
332 import com.sleepycat.db.Transaction;
333 import com.sleepycat.db.TransactionConfig;
334
335 import com.sleepycat.persist.EntityStore;
336 import com.sleepycat.persist.StoreConfig;
337
338 import java.io.File;
339
340 ...
341
342 EntityStore myStore = null;
343 Environment myEnv = null;
344 try {
345 EnvironmentConfig myEnvConfig = new EnvironmentConfig();
346 myEnvConfig.setTransactional(true);
347 myEnvConfig.setInitializeCache(true);
348 myEnvConfig.setInitializeLocking(true);
349 myEnvConfig.setInitializeLogging(true);
350
351 myEnv = new Environment(new File("/my/env/home"),
352 myEnvConfig);
353
354 // Open the store.
355 StoreConfig myStoreConfig = new StoreConfig();
356 myStoreConfig.setAllowCreate(true);
357 myStoreConfig.setTransactional(true);
358
359 // You must set all these fields if you are going to use
360 // a DatabaseConfig object with your new entity store.
361 DatabaseConfig dbConfig = new DatabaseConfig();
362 dbConfig.setTransactional(true);
363 dbConfig.setAllowCreate(true);
364 dbConfig.setType(DatabaseType.BTREE);
365 dbConfig.setReadUncommitted(true); // Enable uncommitted reads.
366
367 myStore = new EntityStore(myEnv, "store_name", myStoreConfig);
368
369 // Set the DatabaseConfig object, so that the underlying
370 // database is configured for uncommitted reads.
371 myStore.setPrimaryConfig(SomeEntityClass.class, dbConfig);
372
373 TransactionConfig txnConfig = new TransactionConfig();
374 txnConfig.setReadUncommitted(true); // Use uncommitted reads
375 // for this transaction.
376 Transaction txn = myEnv.beginTransaction(null, txnConfig);
377
378 // From here, you perform your store reads and writes as normal,
379 // committing and aborting the transactions as is necessary, and
380 // testing for deadlock exceptions as normal (omitted for brevity).
381
382 ...</pre>
383 <p>
384 You can also configure uncommitted read isolation on a read-by-read basis
385 by specifying <code class="literal">LockMode.READ_UNCOMMITTED</code>:
386 </p>
387 <pre class="programlisting">package db.txn;
388
389 import com.sleepycat.db.Database;
390 import com.sleepycat.db.DatabaseEntry;
391 import com.sleepycat.db.Environment;
392 import com.sleepycat.db.LockMode;
393 import com.sleepycat.db.Transaction;
394
395 ...
396
397 Database myDb = null;
398 Environment myEnv = null;
399 Transaction txn = null;
400
401 try {
402
403 // Environment and database open omitted
404
405 ...
406
407 txn = myEnv.beginTransaction(null, null);
408
409 DatabaseEntry theKey =
410 new DatabaseEntry((new String("theKey")).getBytes("UTF-8"));
411 DatabaseEntry theData = new DatabaseEntry();
412
413 myDb.get(txn, theKey, theData, LockMode.READ_UNCOMMITTED);
414 } catch (Exception e) {
415 // Exception handling goes here
416 } </pre>
417 <p>
418 Using the DPL:
419 </p>
420 <pre class="programlisting">package persist.txn;
421
422 import com.sleepycat.db.Environment;
423 import com.sleepycat.db.LockMode;
424 import com.sleepycat.db.Transaction;
425
426 import com.sleepycat.persist.PrimaryIndex;
427 ...
428
429 Environment myEnv = null;
430 Transaction txn = null;
431
432 try {
433
434 // Environment and database open omitted
435
436 ...
437
438 txn = myEnv.beginTransaction(null, null);
439
440 AnEntityClass aec = aPrimaryIndex.get(txn, "pKeya",
441 LockMode.READ_UNCOMMITTED);
442 } catch (Exception e) {
443 // Exception handling goes here
444 } </pre>
445 </div>
446 <div class="sect2" lang="en" xml:lang="en">
447 <div class="titlepage">
448 <div>
449 <div>
450 <h3 class="title"><a id="readcommitted"></a>Committed Reads</h3>
451 </div>
452 </div>
453 </div>
454 <p>
455 You can configure your transaction so that the data being
456 read by a transactional cursor is consistent so long as it
457 is being addressed by the cursor. However, once the cursor is done reading the
458
459
460 <span>
461 object or record (that is, reading records from the page that it currently has locked),
462 </span>
463 the cursor releases its lock on that
464
465
466 <span>
467 object, record or page.
468 </span>
469 This means that the data the cursor has read and released
470 may change before the cursor's transaction has completed.
471 </p>
472 <p>
473 For example,
474 suppose you have two transactions, <code class="literal">Ta</code> and <code class="literal">Tb</code>. Suppose further that
475 <code class="literal">Ta</code> has a cursor that reads <code class="literal">record R</code>, but does not modify it. Normally,
476 <code class="literal">Tb</code> would then be unable to write <code class="literal">record R</code> because
477 <code class="literal">Ta</code> would be holding a read lock on it. But when you configure your transaction for
478 committed reads, <code class="literal">Tb</code> <span class="emphasis"><em>can</em></span> modify <code class="literal">record
479 R</code> before <code class="literal">Ta</code> completes, so long as the reading cursor is no longer
480 addressing the
481
482
483 <span>
484 object, record or page.
485 </span>
486 </p>
487 <p>
488 When you configure your application for this level of isolation, you may see better performance
489 throughput because there are fewer read locks being held by your transactions.
490 Read committed isolation is most useful when you have a cursor that is reading and/or writing records in
491 a single direction, and that does not ever have to go back to re-read those same records. In this case,
492 you can allow DB to release read locks as it goes, rather than hold them for the life of the
493 transaction.
494 </p>
495 <p>
496 To configure your application to use committed reads, do one of the following:
497 </p>
498 <div class="itemizedlist">
499 <ul type="disc">
500 <li>
501 <p>
502 Create your transaction such that it allows committed reads. You do this by
503
504 <span>
505 specifying <code class="literal">true</code> to
506 <code class="methodname">TransactionConfig.setReadCommitted()</code>.
507 </span>
508 </p>
509 </li>
510 <li>
511 <p>
512
513 <span>
514 Specify <code class="literal">true</code> to
515 <code class="methodname">CursorConfig.setReadCommitted()</code>.
516 </span>
517 </p>
518 </li>
519 </ul>
520 </div>
521 <p>
522 For example, the following creates a transaction that allows committed reads:
523 </p>
524 <pre class="programlisting">package db.txn;
525
526 import com.sleepycat.db.Database;
527 import com.sleepycat.db.DatabaseConfig;
528 import com.sleepycat.db.DatabaseEntry;
529 import com.sleepycat.db.DatabaseException;
530 import com.sleepycat.db.Environment;
531 import com.sleepycat.db.EnvironmentConfig;
532 import com.sleepycat.db.Transaction;
533 import com.sleepycat.db.TransactionConfig;
534
535 import java.io.File;
536
537 ...
538
539 Database myDatabase = null;
540 Environment myEnv = null;
541 try {
542 EnvironmentConfig myEnvConfig = new EnvironmentConfig();
543 myEnvConfig.setTransactional(true);
544 myEnvConfig.setInitializeCache(true);
545 myEnvConfig.setInitializeLocking(true);
546 myEnvConfig.setInitializeLogging(true);
547
548 myEnv = new Environment(new File("/my/env/home"),
549 myEnvConfig);
550
551 // Open the database.
552 // Notice that we do not have to specify any properties to the
553 // database to allow committed reads (this is as opposed to
554 // uncommitted reads where we DO have to specify a property on
555 // the database open.
556 DatabaseConfig dbConfig = new DatabaseConfig();
557 dbConfig.setTransactional(true);
558 dbConfig.setType(DatabaseType.BTREE);
559
560 myDatabase = myEnv.openDatabase(null, // txn handle
561 "sampleDatabase", // db file name
562 null, // db name
563 dbConfig);
564 String keyString = "thekey";
565 String dataString = "thedata";
566 DatabaseEntry key =
567 new DatabaseEntry(keyString.getBytes("UTF-8"));
568 DatabaseEntry data =
569 new DatabaseEntry(dataString.getBytes("UTF-8"));
570
571 TransactionConfig txnConfig = new TransactionConfig();
572
573 // Open the transaction and enable committed reads. All cursors open
574 // with this transaction handle will use read committed isolation.
575 txnConfig.setReadCommitted(true);
576 Transaction txn = myEnv.beginTransaction(null, txnConfig);
577
578 // From here, you perform your database reads and writes as normal,
579 // committing and aborting the transactions as is necessary, and
580 // testing for deadlock exceptions as normal (omitted for brevity).
581
582 // Using transactional cursors with concurrent applications is
583 // described in more detail in the following section.
584
585 ...</pre>
586 <p>
587 Using the DPL:
588 </p>
589 <pre class="programlisting">package persist.txn;
590
591 import com.sleepycat.db.Environment;
592 import com.sleepycat.db.EnvironmentConfig;
593 import com.sleepycat.db.Transaction;
594 import com.sleepycat.db.TransactionConfig;
595
596 import com.sleepycat.persist.EntityStore;
597 import com.sleepycat.persist.StoreConfig;
598
599 import java.io.File;
600
601 ...
602
603 EntityStore myStore = null;
604 Environment myEnv = null;
605 try {
606 EnvironmentConfig myEnvConfig = new EnvironmentConfig();
607 myEnvConfig.setTransactional(true);
608 myEnvConfig.setInitializeCache(true);
609 myEnvConfig.setInitializeLocking(true);
610 myEnvConfig.setInitializeLogging(true);
611
612 myEnv = new Environment(new File("/my/env/home"),
613 myEnvConfig);
614
615 // Instantiate the store.
616 StoreConfig myStoreConfig = new StoreConfig();
617 myStoreConfig.setAllowCreate(true);
618 myStoreConfig.setTransactional(true);
619
620 TransactionConfig txnConfig = new TransactionConfig();
621
622 // Open the transaction and enable committed reads. All cursors open
623 // with this transaction handle will use read committed isolation.
624 txnConfig.setReadCommitted(true);
625 Transaction txn = myEnv.beginTransaction(null, txnConfig);
626
627 // From here, you perform your store reads and writes as normal,
628 // committing and aborting the transactions as is necessary, and
629 // testing for deadlock exceptions as normal (omitted for brevity).
630
631 // Using transactional cursors with concurrent applications is
632 // described in more detail in the following section.
633
634 ...</pre>
635 <p>
636 You can also configure read committed isolation on a read-by-read basis
637 by specifying <code class="literal">LockMode.READ_COMMITTED</code>:
638 </p>
639 <pre class="programlisting">package db.txn;
640
641 import com.sleepycat.db.Database;
642 import com.sleepycat.db.DatabaseEntry;
643 import com.sleepycat.db.Environment;
644 import com.sleepycat.db.LockMode;
645 import com.sleepycat.db.Transaction;
646
647 ...
648
649 Database myDb = null;
650 Environment myEnv = null;
651 Transaction txn = null;
652
653 try {
654
655 // Environment and database open omitted
656
657 ...
658
659 txn = myEnv.beginTransaction(null, null);
660
661 DatabaseEntry theKey =
662 new DatabaseEntry((new String("theKey")).getBytes("UTF-8"));
663 DatabaseEntry theData = new DatabaseEntry();
664
665 myDb.get(txn, theKey, theData, LockMode.READ_COMMITTED);
666 } catch (Exception e) {
667 // Exception handling goes here
668 } </pre>
669 <p>
670 Using the DPL:
671 </p>
672 <pre class="programlisting">package persist.txn;
673
674 import com.sleepycat.db.Environment;
675 import com.sleepycat.db.LockMode;
676 import com.sleepycat.db.Transaction;
677
678 import com.sleepycat.persist.PrimaryIndex;
679 ...
680
681 Environment myEnv = null;
682 Transaction txn = null;
683
684 try {
685
686 // Environment and database open omitted
687
688 ...
689
690 txn = myEnv.beginTransaction(null, null);
691
692 // Primary index creation omitted
693 ...
694
695 AnEntityClass aec = aPrimaryIndex.get(txn, "pKeya",
696 LockMode.READ_COMMITTED);
697 } catch (Exception e) {
698 // Exception handling goes here
699 } </pre>
700 </div>
701 <div class="sect2" lang="en" xml:lang="en">
702 <div class="titlepage">
703 <div>
704 <div>
705 <h3 class="title"><a id="snapshot_isolation"></a>Using Snapshot Isolation</h3>
706 </div>
707 </div>
708 </div>
709 <p>
710 By default DB uses serializable isolation. An
711 important side effect of this isolation level is that
712 read operations obtain read locks on database pages,
713 and then hold those locks until the read operation is
714 completed.
715
716 <span>
717 When you are using transactional cursors, this
718 means that read locks are held until the transaction commits or
719 aborts. In that case, over time a transactional cursor
720 can gradually block all other transactions from writing
721 to the database.
722 </span>
723 </p>
724 <p>
725 You can avoid this by using snapshot isolation.
726 Snapshot isolation uses <span class="emphasis"><em>multiversion
727 concurrency control</em></span> to guarantee
728 repeatable reads. What this means is that every time a
729 writer would take a read lock on a page, instead a copy of
730 the page is made and the writer operates on that page
731 copy. This frees other writers from blocking due to a
732 read lock held on the page.
733 </p>
734 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
735 <h3 class="title">Note</h3>
736 <p>
737 Snapshot isolation is strongly recommended for read-only threads when writer
738 threads are also running, as this will eliminate read-write contention and
739 greatly improve transaction throughput for your writer threads. However, in
740 order for snapshot isolation to work for your reader-only threads, you must
741 of course use transactions for your DB reads.
742 </p>
743 </div>
744 <div class="sect3" lang="en" xml:lang="en">
745 <div class="titlepage">
746 <div>
747 <div>
748 <h4 class="title"><a id="sisolation_cost"></a>Snapshot Isolation Cost</h4>
749 </div>
750 </div>
751 </div>
752 <p>
753 Snapshot isolation does not come without a cost.
754 Because pages are being duplicated before being
755 operated upon, the cache will fill up faster. This
756 means that you might need a larger cache in order to
757 hold the entire working set in memory.
758 </p>
759 <p>
760 If the cache becomes full of page copies before old
761 copies can be discarded, additional I/O will occur as
762 pages are written to temporary "freezer" files on disk.
763 This can substantially reduce throughput, and should be
764 avoided if possible by configuring a large cache and
765 keeping snapshot isolation transactions short.
766 </p>
767 <p>
768 You can estimate how large your cache should be by
769 taking a checkpoint, followed by a call to the
770
771
772 <code class="methodname">Environment.getArchiveLogFiles()</code>
773 method. The amount of cache required is approximately
774 double the size of the remaining log files (that is,
775 the log files that cannot be archived).
776 </p>
777 </div>
778 <div class="sect3" lang="en" xml:lang="en">
779 <div class="titlepage">
780 <div>
781 <div>
782 <h4 class="title"><a id="sisolation_maxtxn"></a>Snapshot Isolation Transactional Requirements</h4>
783 </div>
784 </div>
785 </div>
786 <p>
787 In addition to an increased cache size, you may also
788 need to increase the number of transactions
789 that your application supports. (See
790 <a class="xref" href="maxtxns.html" title="Configuring the Transaction Subsystem">Configuring the Transaction Subsystem</a>
791 for details on how to set this.)
792 In the worst case scenario, you might need to configure your application for one
793 more transaction for every page in the cache. This is
794 because transactions are retained until the last page
795 they created is evicted from the cache.
796 </p>
797 </div>
798 <div class="sect3" lang="en" xml:lang="en">
799 <div class="titlepage">
800 <div>
801 <div>
802 <h4 class="title"><a id="sisolation_whenuse"></a>When to Use Snapshot Isolation</h4>
803 </div>
804 </div>
805 </div>
806 <p>
807 Snapshot isolation is best used when all or most
808 of the following conditions are true:
809 </p>
810 <div class="itemizedlist">
811 <ul type="disc">
812 <li>
813 <p>
814 You can have a large cache relative to your working data set size.
815 </p>
816 </li>
817 <li>
818 <p>
819 You require repeatable reads.
820 </p>
821 </li>
822 <li>
823 <p>
824 You will be using transactions that routinely work on
825 the entire database, or more commonly,
826 there is data in your database that will be very
827 frequently written by more than one transaction.
828 </p>
829 </li>
830 <li>
831 <p>
832 Read/write contention is
833 limiting your application's
834 throughput, or the application
835 is all or mostly read-only and
836 contention for the lock manager
837 mutex is limiting throughput.
838 </p>
839 </li>
840 </ul>
841 </div>
842 </div>
843 <div class="sect3" lang="en" xml:lang="en">
844 <div class="titlepage">
845 <div>
846 <div>
847 <h4 class="title"><a id="sisolation_howuse"></a>How to use Snapshot Isolation</h4>
848 </div>
849 </div>
850 </div>
851 <p>
852 You use snapshot isolation by:
853 </p>
854 <div class="itemizedlist">
855 <ul type="disc">
856 <li>
857 <p>
858 Opening the database <span>or store</span> with
859 multiversion support. You can
860 configure this either when you
861 open your environment or when
862 you open your
863
864
865 <span>
866 database or store.
867 </span>
868
869
870
871
872
873 <span>
874 Use either the
875 <code class="methodname">EnvironmentConfig.setMultiversion()</code>
876 or the
877 <code class="methodname">DatabaseConfig.setMultiversion()</code>
878
879 option to configure
880 this support.
881 </span>
882 </p>
883 </li>
884 <li>
885 <p>
886 Configure your <span>cursor or</span>
887 transaction to use snapshot isolation.
888 </p>
889 <p>
890 To do this,
891
892
893
894 <span>
895 specify the
896 <code class="methodname">TransactionConfig.setSnapshot()</code>
897 option when you configure your transaction.
898 </span>
899 </p>
900 </li>
901 </ul>
902 </div>
903 <p>
904 The simplest way to take advantage of snapshot
905 isolation is for queries: keep update
906 transactions using full read/write locking and
907 use snapshot isolation on read-only transactions or
908 cursors. This should minimize blocking of
909 snapshot isolation transactions and will avoid
910 deadlock errors.
911 </p>
912 <p>
913 If the application has update transactions which
914 read many items and only update a small set (for
915 example, scanning until a desired record is
916 found, then modifying it), throughput may be
917 improved by running some updates at snapshot
918 isolation as well. But doing this means that
919 you must manage deadlock errors.
920 See
921 <a class="xref" href="lockingsubsystem.html#deadlockresolve" title="Resolving Deadlocks">Resolving Deadlocks</a>
922 for details.
923 </p>
924 <p>
925 The following code fragment turns
926 on snapshot isolation for a transaction:
927 </p>
928 <pre class="programlisting">package db.txn;
929
930 import com.sleepycat.db.Database;
931 import com.sleepycat.db.DatabaseType;
932 import com.sleepycat.db.DatabaseConfig;
933 import com.sleepycat.db.DatabaseException;
934 import com.sleepycat.db.Environment;
935 import com.sleepycat.db.EnvironmentConfig;
936
937 import java.io.File;
938 import java.io.FileNotFoundException;
939
940 ...
941
942 Database myDatabase = null;
943 Environment myEnv = null;
944 try {
945 EnvironmentConfig myEnvConfig = new EnvironmentConfig();
946 myEnvConfig.setInitializeCache(true);
947 myEnvConfig.setInitializeLocking(true);
948 myEnvConfig.setInitializeLogging(true);
949 myEnvConfig.setTransactional(true);
950 <strong class="userinput"><code>myEnvConfig.setMultiversion(true);</code></strong>
951
952 myEnv = new Environment(new File("/my/env/home"),
953 myEnvConfig);
954
955 // Open the database.
956 DatabaseConfig dbConfig = new DatabaseConfig();
957 dbConfig.setTransactional(true);
958 dbConfig.setType(DatabaseType.BTREE);
959 myDatabase = myEnv.openDatabase(null, // txn handle
960 "sampleDatabase", // db file name
961 null, // db name
962 dbConfig);
963
964 ...
965
966 <strong class="userinput"><code>TransactionConfig txnConfig = new TransactionConfig();
967 txnConfig.setSnapshot(true);</code></strong>
968 txn = myEnv.beginTransaction(null, <strong class="userinput"><code>txnConfig</code></strong>);
969
970 ...
971
972 } catch (DatabaseException de) {
973 // Exception handling goes here
974 } catch (FileNotFoundException fnfe) {
975 // Exception handling goes here
976 }</pre>
977 <p>
978 When using the DPL:
979 </p>
980 <pre class="programlisting">package persist.txn;
981
982 import com.sleepycat.db.DatabaseException;
983 import com.sleepycat.db.Environment;
984 import com.sleepycat.db.EnvironmentConfig;
985
986 import com.sleepycat.persist.EntityStore;
987 import com.sleepycat.persist.StoreConfig;
988
989 import java.io.File;
990 import java.io.FileNotFoundException;
991
992 ...
993
994 EntityStore myStore = null;
995 Environment myEnv = null;
996 try {
997 EnvironmentConfig myEnvConfig = new EnvironmentConfig();
998 myEnvConfig.setInitializeCache(true);
999 myEnvConfig.setInitializeLocking(true);
1000 myEnvConfig.setInitializeLogging(true);
1001 myEnvConfig.setTransactional(true);
1002 <strong class="userinput"><code>myEnvConfig.setMultiversion(true);</code></strong>
1003
1004 myEnv = new Environment(new File("/my/env/home"),
1005 myEnvConfig);
1006
1007 // Instantiate the store
1008 StoreConfig myStoreConfig = new StoreConfig();
1009 myStoreConfig.setAllowCreate(true);
1010 myStoreConfig.setTransactional(true);
1011
1012 myStore = new EntityStore(myEnv, storeName, myStoreConfig);
1013
1014 ...
1015
1016 <strong class="userinput"><code>TransactionConfig txnConfig = new TransactionConfig();
1017 txnConfig.setSnapshot(true);</code></strong>
1018 txn = myEnv.beginTransaction(null, <strong class="userinput"><code>txnConfig</code></strong>);
1019
1020 ...
1021
1022 } catch (DatabaseException de) {
1023 // Exception handling goes here
1024 } catch (FileNotFoundException fnfe) {
1025 // Exception handling goes here
1026 }</pre>
1027 </div>
1028 </div>
1029 </div>
1030 <div class="navfooter">
1031 <hr />
1032 <table width="100%" summary="Navigation footer">
1033 <tr>
1034 <td width="40%" align="left"><a accesskey="p" href="lockingsubsystem.html">Prev</a> </td>
1035 <td width="20%" align="center">
1036 <a accesskey="u" href="txnconcurrency.html">Up</a>
1037 </td>
1038 <td width="40%" align="right"> <a accesskey="n" href="txn_ccursor.html">Next</a></td>
1039 </tr>
1040 <tr>
1041 <td width="40%" align="left" valign="top">The Locking Subsystem </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"> Transactional Cursors and Concurrent Applications</td>
1046 </tr>
1047 </table>
1048 </div>
1049 </body>
1050 </html>