Debian Codegraph @

docs/programmer_reference/transapp_read.html

raw symbols SCIP
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>Degrees of 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="Berkeley DB Programmer's Reference Guide" />
10 <link rel="up" href="transapp.html" title="Chapter 11.  Berkeley DB Transactional Data Store Applications" />
11 <link rel="prev" href="transapp_inc.html" title="Isolation" />
12 <link rel="next" href="transapp_cursor.html" title="Transactional cursors" />
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">Degrees of isolation</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="transapp_inc.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 11. 
26 Berkeley DB Transactional Data Store Applications
27 </th>
28 <td width="20%" align="right"> <a accesskey="n" href="transapp_cursor.html">Next</a></td>
29 </tr>
30 </table>
31 <hr />
32 </div>
33 <div class="sect1" lang="en" xml:lang="en">
34 <div class="titlepage">
35 <div>
36 <div>
37 <h2 class="title" style="clear: both"><a id="transapp_read"></a>Degrees of isolation</h2>
38 </div>
39 </div>
40 </div>
41 <div class="toc">
42 <dl>
43 <dt>
44 <span class="sect2">
45 <a href="transapp_read.html#snapshot_isolation">Snapshot Isolation</a>
46 </span>
47 </dt>
48 </dl>
49 </div>
50 <p>Transactions can be isolated from each other to different degrees.
51 <span class="emphasis"><em>Serializable</em></span> provides the most isolation, and means that, for
52 the life of the transaction, every time a thread of control reads a data
53 item, it will be unchanged from its previous value (assuming, of course,
54 the thread of control does not itself modify the item). By default,
55 Berkeley DB enforces serializability whenever database reads are wrapped in
56 transactions. This is also known as <span class="emphasis"><em>degree 3 isolation</em></span>.</p>
57 <p>Most applications do not need to enclose all reads in transactions, and
58 when possible, transactionally protected reads at serializable isolation
59 should be avoided as they can cause performance problems. For example,
60 a serializable cursor sequentially reading each key/data pair in a
61 database, will acquire a read lock on most of the pages in the database
62 and so will gradually block all write operations on the databases until
63 the transaction commits or aborts. Note, however, that if there are
64 update transactions present in the application, the read operations must
65 still use locking, and must be prepared to repeat any operation
66 (possibly closing and reopening a cursor) that fails with a return value
67 of <a class="link" href="program_errorret.html#program_errorret.DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a>.
68 Applications that need repeatable reads
69 are ones that require the ability to repeatedly access a data item
70 knowing that it will not have changed (for example, an operation
71 modifying a data item based on its existing value).</p>
72 <p><span class="emphasis"><em>Snapshot isolation</em></span> also guarantees repeatable reads, but
73 avoids read locks by using multiversion concurrency control (MVCC).
74 This makes update operations more expensive, because they have to
75 allocate space for new versions of pages in cache and make copies, but
76 avoiding read locks can significantly increase throughput for many
77 applications. Snapshot isolation is discussed in detail below.</p>
78 <p>A transaction may only require
79 <span class="emphasis"><em>cursor stability</em></span>, that is only
80 be guaranteed that cursors see committed data that does not change so
81 long as it is addressed by the cursor, but may change before the reading
82 transaction completes. This is also called <span class="emphasis"><em>degree 2
83 isolation</em></span>. Berkeley DB provides this level of isolation when a transaction
84 is started with the <a href="../api_reference/C/dbcget.html#dbcget_DB_READ_COMMITTED" class="olink">DB_READ_COMMITTED</a> flag. This flag may also
85 be specified when opening a cursor within a fully isolated
86 transaction.</p>
87 <p>Berkeley DB optionally supports reading uncommitted data; that is, read
88 operations may request data which has been modified but not yet
89 committed by another transaction. This is also called <span class="emphasis"><em>degree
90 1 isolation</em></span>. This is done by first specifying the
91 <a href="../api_reference/C/dbopen.html#dbopen_DB_READ_UNCOMMITTED" class="olink">DB_READ_UNCOMMITTED</a> flag when opening the underlying database,
92 and then specifying the <a href="../api_reference/C/dbopen.html#dbopen_DB_READ_UNCOMMITTED" class="olink">DB_READ_UNCOMMITTED</a> flag when beginning
93 a transaction, opening a cursor, or performing a read operation. The
94 advantage of using <a href="../api_reference/C/dbopen.html#dbopen_DB_READ_UNCOMMITTED" class="olink">DB_READ_UNCOMMITTED</a> is that read operations
95 will not block when another transaction holds a write lock on the
96 requested data; the disadvantage is that read operations may return data
97 that will disappear should the transaction holding the write lock
98 abort.</p>
99 <div class="sect2" lang="en" xml:lang="en">
100 <div class="titlepage">
101 <div>
102 <div>
103 <h3 class="title"><a id="snapshot_isolation"></a>Snapshot Isolation</h3>
104 </div>
105 </div>
106 </div>
107 <p>To make use of snapshot isolation, databases must first be configured
108 for multiversion access by calling <a href="../api_reference/C/dbopen.html" class="olink">DB-&gt;open()</a> with the
109 <a href="../api_reference/C/dbopen.html#dbopen_DB_MULTIVERSION" class="olink">DB_MULTIVERSION</a> flag. Then transactions or cursors must be
110 configured with the <a href="../api_reference/C/txnbegin.html#txnbegin_DB_TXN_SNAPSHOT" class="olink">DB_TXN_SNAPSHOT</a> flag.</p>
111 <p>When configuring an environment for snapshot isolation, it is important
112 to realize that having multiple versions of pages in cache means that
113 the working set will take up more of the cache. As a result, snapshot
114 isolation is best suited for use with larger cache sizes.</p>
115 <p>If the cache becomes full of page copies before the old copies can be
116 discarded, additional I/O will occur as pages are written to temporary
117 "freezer" files. This can substantially reduce throughput, and should
118 be avoided if possible by configuring a large cache and keeping snapshot
119 isolation transactions short. The amount of cache required to avoid
120 freezing buffers can be estimated by taking a checkpoint followed by a
121 call to <a href="../api_reference/C/logarchive.html" class="olink">DB_ENV-&gt;log_archive()</a>. The amount of cache required is
122 approximately double the size of logs that remains.</p>
123 <p>The environment should also be configured for sufficient transactions
124 using <a href="../api_reference/C/envset_tx_max.html" class="olink">DB_ENV-&gt;set_tx_max()</a>. The maximum number of transactions
125 needs to include all transactions executed concurrently by the
126 application plus all cursors configured for snapshot isolation.
127 Further, the transactions are retained until the last page they created
128 is evicted from cache, so in the extreme case, an additional transaction
129 may be needed for each page in the cache. Note that cache sizes under
130 500MB are increased by 25%, so the calculation of number of pages needs
131 to take this into account.</p>
132 <p>So when <span class="emphasis"><em>should</em></span> applications use snapshot isolation?</p>
133 <div class="itemizedlist">
134 <ul type="disc">
135 <li>There is a large cache relative to size of updates performed by
136 concurrent transactions; and</li>
137 <li>Read/write contention is limiting the throughput of the application;
138 or</li>
139 <li>The application is all or mostly read-only, and contention for the lock
140 manager mutex is limiting throughput.</li>
141 </ul>
142 </div>
143 <p>The simplest way to take advantage of snapshot isolation is for queries:
144 keep update transactions using full read/write locking and set
145 <a href="../api_reference/C/txnbegin.html#txnbegin_DB_TXN_SNAPSHOT" class="olink">DB_TXN_SNAPSHOT</a> on read-only transactions or cursors. This
146 should minimize blocking of snapshot isolation transactions and will
147 avoid introducing new
148 <a class="link" href="program_errorret.html#program_errorret.DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a>
149 errors.</p>
150 <p>If the application has update transactions which read many items and
151 only update a small set (for example, scanning until a desired record is
152 found, then modifying it), throughput may be improved by running some
153 updates at snapshot isolation as well.</p>
154 </div>
155 </div>
156 <div class="navfooter">
157 <hr />
158 <table width="100%" summary="Navigation footer">
159 <tr>
160 <td width="40%" align="left"><a accesskey="p" href="transapp_inc.html">Prev</a> </td>
161 <td width="20%" align="center">
162 <a accesskey="u" href="transapp.html">Up</a>
163 </td>
164 <td width="40%" align="right"> <a accesskey="n" href="transapp_cursor.html">Next</a></td>
165 </tr>
166 <tr>
167 <td width="40%" align="left" valign="top">Isolation </td>
168 <td width="20%" align="center">
169 <a accesskey="h" href="index.html">Home</a>
170 </td>
171 <td width="40%" align="right" valign="top"> Transactional cursors</td>
172 </tr>
173 </table>
174 </div>
175 </body>
176 </html>