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