Debian Codegraph @

docs/gsg_txn/C/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 <span>
236 specifying <code class="literal">DB_READ_UNCOMMITTED</code> when you open your database.
237 </span>
238
239 </p>
240 </li>
241 <li>
242 <p>
243 <span>
244 Specify <code class="literal">DB_READ_UNCOMMITTED</code>
245 when you create the transaction,
246 open the cursor, or read a record from the database.
247 </span>
248
249
250 </p>
251 </li>
252 </ol>
253 </div>
254 <p>
255 For example, the following opens the database such that it supports uncommitted reads, and then creates a
256 transaction that causes all reads performed within it to use uncommitted reads. Remember that simply opening
257 the database to support uncommitted reads is not enough; you must also declare your read operations to be
258 performed using uncommitted reads.
259 </p>
260 <pre class="programlisting">#include &lt;stdio.h&gt;
261 #include &lt;stdlib.h&gt;
262
263 #include "db.h"
264
265 int
266 main(void)
267 {
268 int ret, ret_c;
269 u_int32_t db_flags, env_flags;
270 DB *dbp;
271 DB_ENV *envp;
272 DB_TXN *txn;
273 const char *db_home_dir = "/tmp/myEnvironment";
274 const char *file_name = "mydb.db";
275 const char *keystr ="thekey";
276 const char *datastr = "thedata";
277
278 dbp = NULL;
279 envp = NULL;
280
281 /* Open the environment */
282 ret = db_env_create(&amp;envp, 0);
283 if (ret != 0) {
284 fprintf(stderr, "Error creating environment handle: %s\n",
285 db_strerror(ret));
286 return (EXIT_FAILURE);
287 }
288
289 env_flags = DB_CREATE | /* If the environment does not
290 * exist, create it. */
291 DB_INIT_LOCK | /* Initialize locking */
292 DB_INIT_LOG | /* Initialize logging */
293 DB_INIT_MPOOL | /* Initialize the cache */
294 DB_THREAD | /* Free-thread the env handle. */
295 DB_INIT_TXN; /* Initialize transactions */
296
297 ret = envp-&gt;open(envp, db_home_dir, env_flags, 0);
298 if (ret != 0) {
299 fprintf(stderr, "Error opening environment: %s\n",
300 db_strerror(ret));
301 goto err;
302 }
303
304 /* Initialize the DB handle */
305 ret = db_create(&amp;dbp, envp, 0);
306 if (ret != 0) {
307 envp-&gt;err(envp, ret, "Database creation failed");
308 goto err;
309 }
310
311 db_flags = DB_CREATE | /* Create the db if it does not
312 * exist */
313 DB_AUTO_COMMIT | /* Enable auto commit */
314 DB_READ_UNCOMMITTED; /* Enable uncommitted reads */
315
316 ret = dbp-&gt;open(dbp, /* Pointer to the database */
317 NULL, /* Txn pointer */
318 file_name, /* File name */
319 NULL, /* Logical db name */
320 DB_BTREE, /* Database type (using btree) */
321 db_flags, /* Open flags */
322 0); /* File mode. Using defaults */
323 if (ret != 0) {
324 envp-&gt;err(envp, ret, "Database '%s' open failed",
325 file_name);
326 goto err;
327 }
328
329 /* Get the txn handle */
330 txn = NULL;
331 ret = envp-&gt;txn_begin(envp, NULL, &amp;txn, DB_READ_UNCOMMITTED);
332 if (ret != 0) {
333 envp-&gt;err(envp, ret, "Transaction begin failed.");
334 goto err;
335 }
336
337 /*
338 * From here, you perform your database reads and writes as normal,
339 * committing and aborting the transactions as is necessary, and
340 * testing for deadlock exceptions as normal (omitted for brevity).
341 */
342
343 ... </pre>
344 </div>
345 <div class="sect2" lang="en" xml:lang="en">
346 <div class="titlepage">
347 <div>
348 <div>
349 <h3 class="title"><a id="readcommitted"></a>Committed Reads</h3>
350 </div>
351 </div>
352 </div>
353 <p>
354 You can configure your transaction so that the data being
355 read by a transactional cursor is consistent so long as it
356 is being addressed by the cursor. However, once the cursor is done reading the
357
358 <span>
359 record (that is, reading records from the page that it currently has locked),
360 </span>
361
362 the cursor releases its lock on that
363
364 <span>
365 record or page.
366 </span>
367
368 This means that the data the cursor has read and released
369 may change before the cursor's transaction has completed.
370 </p>
371 <p>
372 For example,
373 suppose you have two transactions, <code class="literal">Ta</code> and <code class="literal">Tb</code>. Suppose further that
374 <code class="literal">Ta</code> has a cursor that reads <code class="literal">record R</code>, but does not modify it. Normally,
375 <code class="literal">Tb</code> would then be unable to write <code class="literal">record R</code> because
376 <code class="literal">Ta</code> would be holding a read lock on it. But when you configure your transaction for
377 committed reads, <code class="literal">Tb</code> <span class="emphasis"><em>can</em></span> modify <code class="literal">record
378 R</code> before <code class="literal">Ta</code> completes, so long as the reading cursor is no longer
379 addressing the
380
381 <span>
382 record or page.
383 </span>
384
385 </p>
386 <p>
387 When you configure your application for this level of isolation, you may see better performance
388 throughput because there are fewer read locks being held by your transactions.
389 Read committed isolation is most useful when you have a cursor that is reading and/or writing records in
390 a single direction, and that does not ever have to go back to re-read those same records. In this case,
391 you can allow DB to release read locks as it goes, rather than hold them for the life of the
392 transaction.
393 </p>
394 <p>
395 To configure your application to use committed reads, do one of the following:
396 </p>
397 <div class="itemizedlist">
398 <ul type="disc">
399 <li>
400 <p>
401 Create your transaction such that it allows committed reads. You do this by
402 <span>
403 specifying <code class="literal">DB_READ_COMMITTED</code> when you open the transaction.
404 </span>
405
406 </p>
407 </li>
408 <li>
409 <p>
410 <span>
411 Specify <code class="literal">DB_READ_COMMITTED</code>
412 when you open the cursor.
413 </span>
414
415 </p>
416 </li>
417 </ul>
418 </div>
419 <p>
420 For example, the following creates a transaction that allows committed reads:
421 </p>
422 <pre class="programlisting">#include &lt;stdio.h&gt;
423 #include &lt;stdlib.h&gt;
424
425 #include "db.h"
426
427 int
428 main(void)
429 {
430 int ret, ret_c;
431 u_int32_t db_flags, env_flags;
432 DB *dbp;
433 DB_ENV *envp;
434 DB_TXN *txn;
435 const char *db_home_dir = "/tmp/myEnvironment";
436 const char *file_name = "mydb.db";
437
438 dbp = NULL;
439 envp = NULL;
440
441 /* Open the environment */
442 ret = db_env_create(&amp;envp, 0);
443 if (ret != 0) {
444 fprintf(stderr, "Error creating environment handle: %s\n",
445 db_strerror(ret));
446 return (EXIT_FAILURE);
447 }
448
449 env_flags = DB_CREATE | /* If the environment does not
450 * exist, create it. */
451 DB_INIT_LOCK | /* Initialize locking */
452 DB_INIT_LOG | /* Initialize logging */
453 DB_INIT_MPOOL | /* Initialize the cache */
454 DB_THREAD | /* Free-thread the env handle. */
455 DB_INIT_TXN; /* Initialize transactions */
456
457 ret = envp-&gt;open(envp, db_home_dir, env_flags, 0);
458 if (ret != 0) {
459 fprintf(stderr, "Error opening environment: %s\n",
460 db_strerror(ret));
461 goto err;
462 }
463
464 /* Initialize the DB handle */
465 ret = db_create(&amp;dbp, envp, 0);
466 if (ret != 0) {
467 envp-&gt;err(envp, ret, "Database creation failed");
468 goto err;
469 }
470
471 /*
472 * Notice that we do not have to specify any flags to the database to
473 * allow committed reads (this is as opposed to uncommitted reads
474 * where we DO have to specify a flag on the database open.
475 */
476 db_flags = DB_CREATE | DB_AUTO_COMMIT;
477 ret = dbp-&gt;open(dbp, /* Pointer to the database */
478 NULL, /* Txn pointer */
479 file_name, /* File name */
480 NULL, /* Logical db name */
481 DB_BTREE, /* Database type (using btree) */
482 db_flags, /* Open flags */
483 0); /* File mode. Using defaults */
484 if (ret != 0) {
485 envp-&gt;err(envp, ret, "Database '%s' open failed",
486 file_name);
487 goto err;
488 }
489
490 /* Get the txn handle */
491 txn = NULL;
492 /*
493 * Open the transaction and enable committed reads. All cursors open
494 * with this transaction handle will use read committed isolation.
495 */
496 ret = envp-&gt;txn_begin(envp, NULL, &amp;txn, DB_READ_COMMITTED);
497 if (ret != 0) {
498 envp-&gt;err(envp, ret, "Transaction begin failed.");
499 goto err;
500 }
501
502 /*
503 * From here, you perform your database reads and writes as normal,
504 * committing and aborting the transactions as is necessary, and
505 * testing for deadlock exceptions as normal (omitted for brevity).
506 *
507 * Using transactional cursors with concurrent applications is
508 * described in more detail in the following section.
509 */
510
511 ... </pre>
512 </div>
513 <div class="sect2" lang="en" xml:lang="en">
514 <div class="titlepage">
515 <div>
516 <div>
517 <h3 class="title"><a id="snapshot_isolation"></a>Using Snapshot Isolation</h3>
518 </div>
519 </div>
520 </div>
521 <p>
522 By default DB uses serializable isolation. An
523 important side effect of this isolation level is that
524 read operations obtain read locks on database pages,
525 and then hold those locks until the read operation is
526 completed.
527
528 <span>
529 When you are using transactional cursors, this
530 means that read locks are held until the transaction commits or
531 aborts. In that case, over time a transactional cursor
532 can gradually block all other transactions from writing
533 to the database.
534 </span>
535 </p>
536 <p>
537 You can avoid this by using snapshot isolation.
538 Snapshot isolation uses <span class="emphasis"><em>multiversion
539 concurrency control</em></span> to guarantee
540 repeatable reads. What this means is that every time a
541 writer would take a read lock on a page, instead a copy of
542 the page is made and the writer operates on that page
543 copy. This frees other writers from blocking due to a
544 read lock held on the page.
545 </p>
546 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
547 <h3 class="title">Note</h3>
548 <p>
549 Snapshot isolation is strongly recommended for read-only threads when writer
550 threads are also running, as this will eliminate read-write contention and
551 greatly improve transaction throughput for your writer threads. However, in
552 order for snapshot isolation to work for your reader-only threads, you must
553 of course use transactions for your DB reads.
554 </p>
555 </div>
556 <div class="sect3" lang="en" xml:lang="en">
557 <div class="titlepage">
558 <div>
559 <div>
560 <h4 class="title"><a id="sisolation_cost"></a>Snapshot Isolation Cost</h4>
561 </div>
562 </div>
563 </div>
564 <p>
565 Snapshot isolation does not come without a cost.
566 Because pages are being duplicated before being
567 operated upon, the cache will fill up faster. This
568 means that you might need a larger cache in order to
569 hold the entire working set in memory.
570 </p>
571 <p>
572 If the cache becomes full of page copies before old
573 copies can be discarded, additional I/O will occur as
574 pages are written to temporary "freezer" files on disk.
575 This can substantially reduce throughput, and should be
576 avoided if possible by configuring a large cache and
577 keeping snapshot isolation transactions short.
578 </p>
579 <p>
580 You can estimate how large your cache should be by
581 taking a checkpoint, followed by a call to the
582 <code class="methodname">DB_ENV-&gt;log_archive()</code>
583
584
585 method. The amount of cache required is approximately
586 double the size of the remaining log files (that is,
587 the log files that cannot be archived).
588 </p>
589 </div>
590 <div class="sect3" lang="en" xml:lang="en">
591 <div class="titlepage">
592 <div>
593 <div>
594 <h4 class="title"><a id="sisolation_maxtxn"></a>Snapshot Isolation Transactional Requirements</h4>
595 </div>
596 </div>
597 </div>
598 <p>
599 In addition to an increased cache size, you may also
600 need to increase the number of transactions
601 that your application supports. (See
602 <a class="xref" href="maxtxns.html" title="Configuring the Transaction Subsystem">Configuring the Transaction Subsystem</a>
603 for details on how to set this.)
604 In the worst case scenario, you might need to configure your application for one
605 more transaction for every page in the cache. This is
606 because transactions are retained until the last page
607 they created is evicted from the cache.
608 </p>
609 </div>
610 <div class="sect3" lang="en" xml:lang="en">
611 <div class="titlepage">
612 <div>
613 <div>
614 <h4 class="title"><a id="sisolation_whenuse"></a>When to Use Snapshot Isolation</h4>
615 </div>
616 </div>
617 </div>
618 <p>
619 Snapshot isolation is best used when all or most
620 of the following conditions are true:
621 </p>
622 <div class="itemizedlist">
623 <ul type="disc">
624 <li>
625 <p>
626 You can have a large cache relative to your working data set size.
627 </p>
628 </li>
629 <li>
630 <p>
631 You require repeatable reads.
632 </p>
633 </li>
634 <li>
635 <p>
636 You will be using transactions that routinely work on
637 the entire database, or more commonly,
638 there is data in your database that will be very
639 frequently written by more than one transaction.
640 </p>
641 </li>
642 <li>
643 <p>
644 Read/write contention is
645 limiting your application's
646 throughput, or the application
647 is all or mostly read-only and
648 contention for the lock manager
649 mutex is limiting throughput.
650 </p>
651 </li>
652 </ul>
653 </div>
654 </div>
655 <div class="sect3" lang="en" xml:lang="en">
656 <div class="titlepage">
657 <div>
658 <div>
659 <h4 class="title"><a id="sisolation_howuse"></a>How to use Snapshot Isolation</h4>
660 </div>
661 </div>
662 </div>
663 <p>
664 You use snapshot isolation by:
665 </p>
666 <div class="itemizedlist">
667 <ul type="disc">
668 <li>
669 <p>
670 Opening the database with
671 multiversion support. You can
672 configure this either when you
673 open your environment or when
674 you open your
675
676 <span>
677 database.
678 </span>
679
680
681 <span>
682 Use the
683 <code class="literal">DB_MULTIVERSION</code>
684 flag to configure this support.
685 </span>
686
687
688
689
690 </p>
691 </li>
692 <li>
693 <p>
694 Configure your <span>cursor or</span>
695 transaction to use snapshot isolation.
696 </p>
697 <p>
698 To do this,
699
700 <span>
701 pass the <code class="literal">DB_TXN_SNAPSHOT</code> flag
702 when you
703
704 <span>
705 open the cursor or
706 </span>
707
708 create the transaction.
709
710 <span>
711 If configured for the transaction,
712 then this flag is not required
713 when the cursor is opened.
714 </span>
715
716 </span>
717
718
719 </p>
720 </li>
721 </ul>
722 </div>
723 <p>
724 The simplest way to take advantage of snapshot
725 isolation is for queries: keep update
726 transactions using full read/write locking and
727 use snapshot isolation on read-only transactions or
728 cursors. This should minimize blocking of
729 snapshot isolation transactions and will avoid
730 deadlock errors.
731 </p>
732 <p>
733 If the application has update transactions which
734 read many items and only update a small set (for
735 example, scanning until a desired record is
736 found, then modifying it), throughput may be
737 improved by running some updates at snapshot
738 isolation as well. But doing this means that
739 you must manage deadlock errors.
740 See
741 <a class="xref" href="lockingsubsystem.html#deadlockresolve" title="Resolving Deadlocks">Resolving Deadlocks</a>
742 for details.
743 </p>
744 <p>
745 The following code fragment turns
746 on snapshot isolation for a transaction:
747 </p>
748 <pre class="programlisting">#include &lt;stdio.h&gt;
749 #include &lt;stdlib.h&gt;
750
751 #include "db.h"
752
753 int
754 main(void)
755 {
756 int ret, ret_c;
757 u_int32_t db_flags, env_flags;
758 DB *dbp;
759 DB_ENV *envp;
760 const char *db_home_dir = "/tmp/myEnvironment";
761 const char *file_name = "mydb.db";
762
763 dbp = NULL;
764 envp = NULL;
765
766 /* Open the environment */
767 ret = db_env_create(&amp;envp, 0);
768 if (ret != 0) {
769 fprintf(stderr, "Error creating environment handle: %s\n",
770 db_strerror(ret));
771 return (EXIT_FAILURE);
772 }
773 <strong class="userinput"><code>/* Support snapshot isolation */
774 envp-&gt;set_flags(envp, DB_MULTIVERSION, 1);</code></strong>
775 env_flags = DB_CREATE | /* Create the environment if it does
776 * not already exist. */
777
778 DB_INIT_LOCK | /* Initialize locking. */
779 DB_INIT_LOG | /* Initialize logging */
780 DB_INIT_MPOOL| /* Initialize the in-memory cache. */
781 DB_INIT_TXN; /* Initialize transactions */
782
783 ret = envp-&gt;open(envp, db_home_dir, env_flags, 0);
784 if (ret != 0) {
785 fprintf(stderr, "Error opening environment: %s\n",
786 db_strerror(ret));
787 goto err;
788 }
789
790 /* Initialize the DB handle */
791 ret = db_create(&amp;dbp, envp, 0);
792 if (ret != 0) {
793 envp-&gt;err(envp, ret, "Database creation failed");
794 goto err;
795 }
796
797 /*
798 * Nothing needs to be supplied here to support snapshot isolation.
799 * The environment does, so its databases will too.
800 */
801 db_flags = DB_CREATE | DB_AUTO_COMMIT;
802 ret = dbp-&gt;open(dbp, /* Pointer to the database */
803 NULL, /* Txn pointer */
804 file_name, /* File name */
805 NULL, /* Logical db name */
806 DB_BTREE, /* Database type (using btree) */
807 db_flags, /* Open flags */
808 0); /* File mode. Using defaults */
809 if (ret != 0) {
810 envp-&gt;err(envp, ret, "Database '%s' open failed",
811 file_name);
812 goto err;
813 }
814
815
816 ....
817
818 ret = envp-&gt;txn_begin(envp, NULL, &amp;txn, <strong class="userinput"><code>DB_TXN_SNAPSHOT</code></strong>);
819
820 /* remainder of the program omitted for brevity */
821
822 </pre>
823 </div>
824 </div>
825 </div>
826 <div class="navfooter">
827 <hr />
828 <table width="100%" summary="Navigation footer">
829 <tr>
830 <td width="40%" align="left"><a accesskey="p" href="lockingsubsystem.html">Prev</a> </td>
831 <td width="20%" align="center">
832 <a accesskey="u" href="txnconcurrency.html">Up</a>
833 </td>
834 <td width="40%" align="right"> <a accesskey="n" href="txn_ccursor.html">Next</a></td>
835 </tr>
836 <tr>
837 <td width="40%" align="left" valign="top">The Locking Subsystem </td>
838 <td width="20%" align="center">
839 <a accesskey="h" href="index.html">Home</a>
840 </td>
841 <td width="40%" align="right" valign="top"> Transactional Cursors and Concurrent Applications</td>
842 </tr>
843 </table>
844 </div>
845 </body>
846 </html>