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