| 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>Read/Modify/Write</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="exclusivelock.html" title="Exclusive Database Handles" /> |
| 12 |
<link rel="next" href="txnnowait.html" title="No Wait on Blocks" /> |
| 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">Read/Modify/Write</th> |
| 22 |
</tr> |
| 23 |
<tr> |
| 24 |
<td width="20%" align="left"><a accesskey="p" href="exclusivelock.html">Prev</a> </td> |
| 25 |
<th width="60%" align="center">Chapter 4. Concurrency</th> |
| 26 |
<td width="20%" align="right"> <a accesskey="n" href="txnnowait.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="readmodifywrite"></a>Read/Modify/Write</h2> |
| 36 |
</div> |
| 37 |
</div> |
| 38 |
</div> |
| 39 |
<p> |
| 40 |
If you are retrieving |
| 41 |
<span>a record from the database</span> |
| 42 |
|
| 43 |
|
| 44 |
for the purpose of modifying or deleting it, you should declare |
| 45 |
a read-modify-write cycle at the time that you read the |
| 46 |
<span>record.</span> |
| 47 |
|
| 48 |
Doing so causes DB to obtain write locks (instead of a read |
| 49 |
locks) at the time of the read. This helps to prevent deadlocks by |
| 50 |
preventing another transaction from acquiring a read lock on the same |
| 51 |
record while the read-modify-write cycle is in progress. |
| 52 |
</p> |
| 53 |
<p> |
| 54 |
Note that declaring a read-modify-write cycle may actually increase the amount of blocking that your |
| 55 |
application sees, because readers immediately obtain write locks and write locks cannot be shared. For this |
| 56 |
reason, you should use read-modify-write cycles only if you are seeing a large amount of deadlocking |
| 57 |
occurring in your application. |
| 58 |
</p> |
| 59 |
<p> |
| 60 |
In order to declare a read/modify/write cycle when you perform a |
| 61 |
read operation, |
| 62 |
|
| 63 |
<span> |
| 64 |
pass the <code class="literal">DB_RMW</code> flag |
| 65 |
<span> |
| 66 |
to the database or cursor get method. |
| 67 |
</span> |
| 68 |
|
| 69 |
|
| 70 |
</span> |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
</p> |
| 76 |
<p> |
| 77 |
For example: |
| 78 |
</p> |
| 79 |
<pre class="programlisting">retry: |
| 80 |
/* Get the transaction */ |
| 81 |
ret = envp->txn_begin(envp, NULL, &txn, 0); |
| 82 |
if (ret != 0) { |
| 83 |
envp->err(envp, ret, "txn_begin failed"); |
| 84 |
return (EXIT_FAILURE); |
| 85 |
} |
| 86 |
... |
| 87 |
/* key and data are Dbts. Their usage is omitted for brevity. */ |
| 88 |
... |
| 89 |
/* Get the data. Declare the read/modify/write cycle here */ |
| 90 |
ret = dbp->get(dbp, txn, &key, &data, DB_RMW); |
| 91 |
|
| 92 |
... |
| 93 |
/* Modify the key and data as is required (not shown here) */ |
| 94 |
... |
| 95 |
|
| 96 |
/* Put the data. Note that you do not have to provide any additional |
| 97 |
* flags here due to the read/modify/write cycle. Simply put the |
| 98 |
* data and perform your deadlock detection as normal. |
| 99 |
*/ |
| 100 |
ret = dbp->put(dbp, txn, &key, &data, 0); |
| 101 |
switch (ret) { |
| 102 |
/* Deadlock detection omitted for brevity */ |
| 103 |
.... </pre> |
| 104 |
</div> |
| 105 |
<div class="navfooter"> |
| 106 |
<hr /> |
| 107 |
<table width="100%" summary="Navigation footer"> |
| 108 |
<tr> |
| 109 |
<td width="40%" align="left"><a accesskey="p" href="exclusivelock.html">Prev</a> </td> |
| 110 |
<td width="20%" align="center"> |
| 111 |
<a accesskey="u" href="txnconcurrency.html">Up</a> |
| 112 |
</td> |
| 113 |
<td width="40%" align="right"> <a accesskey="n" href="txnnowait.html">Next</a></td> |
| 114 |
</tr> |
| 115 |
<tr> |
| 116 |
<td width="40%" align="left" valign="top">Exclusive Database Handles </td> |
| 117 |
<td width="20%" align="center"> |
| 118 |
<a accesskey="h" href="index.html">Home</a> |
| 119 |
</td> |
| 120 |
<td width="40%" align="right" valign="top"> No Wait on Blocks</td> |
| 121 |
</tr> |
| 122 |
</table> |
| 123 |
</div> |
| 124 |
</body> |
| 125 |
</html> |