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.62.4" />
9    <link rel="home" 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="previous" 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></div>
36      </div>
37      <p>
38            If you are retrieving 
39                
40                
41                <span>a record from the database or a class from the store</span>
42            for the purpose of modifying or deleting it, you should declare 
43            a read-modify-write cycle at the time that you read the 
44                    <span>record.</span>
45                    
46            Doing so causes DB to obtain write locks (instead of a read
47            locks) at the time of the read. This helps to prevent deadlocks by
48            preventing another transaction from acquiring a read lock on the same
49            record while the read-modify-write cycle is in progress.
50        </p>
51      <p>
52            Note that declaring a read-modify-write cycle may actually increase the amount of blocking that your
53            application sees, because readers immediately obtain write locks and write locks cannot be shared. For this
54            reason, you should use read-modify-write cycles only if you are seeing a large amount of deadlocking
55            occurring in your application.
56        </p>
57      <p>
58            In order to declare a read/modify/write cycle when you perform a
59            read operation, 
60
61            
62
63            
64
65            <span>
66                specify 
67                    <tt class="classname">com.sleepycat.db.LockMode.RMW</tt>
68                    
69                    to the database, cursor,
70                    <tt class="classname">PrimaryIndex</tt>, or
71                    <tt class="classname">SecondaryIndex</tt> get method.
72            </span>
73        </p>
74      <p>
75            For example:
76        </p>
77      <pre class="programlisting">// Begin the deadlock retry loop as is normal.
78while (retry_count &lt; MAX_DEADLOCK_RETRIES) {
79    try {
80        txn = myEnv.beginTransaction(null, null);
81
82        ...
83        // key and data are DatabaseEntry objects.
84        // Their usage is omitted for brevity.
85        ...
86
87        // Read the data. Declare the read/modify/write cycle here
88        myDatabase.get(txn, key, data, LockMode.RMW);
89
90
91        // Put the data. Note that you do not have to provide any 
92        // additional flags here due to the read/modify/write 
93        // cycle. Simply put the data and perform your deadlock 
94        // detection as normal.
95        myDatabase.put(txn, key, data);
96        txn.commit();
97        return 0;
98    } catch (DeadlockException de) {
99        // Deadlock detection and exception handling omitted
100        // for brevity
101        ... </pre>
102      <p>
103        Or, with the DPL:
104</p>
105      <pre class="programlisting">// Begin the deadlock retry loop as is normal
106        while (retry_count &lt; MAX_DEADLOCK_RETRIES) {
107    try {
108        txn = myEnv.beginTransaction(null, null);
109
110        ...
111        // 'store' is an EntityStore and 'Inventory' is an entity class
112        // Their usage and implementation is omitted for brevity.
113        ...
114
115        // Read the data, using the PrimaryIndex for the entity object
116        PrimaryIndex&lt;String,Inventory&gt; pi = 
117                store.getPrimaryIndex(String.class, Inventory.class);
118        Inventory iv = pi.get(txn, "somekey", LockMode.RMW);
119
120        // Do something to the retreived object
121
122
123        // Put the object. Note that you do not have to provide any 
124        // additional flags here due to the read/modify/write 
125        // cycle. Simply put the data and perform your deadlock 
126        // detection as normal.
127
128        pi.put(txn, iv);
129        txn.commit();
130        return 0;
131
132    } catch (DeadlockException de) {
133        // Deadlock detection and exception handling omitted
134        // for brevity
135        ... </pre>
136    </div>
137    <div class="navfooter">
138      <hr />
139      <table width="100%" summary="Navigation footer">
140        <tr>
141          <td width="40%" align="left"><a accesskey="p" href="txn_ccursor.html">Prev</a> </td>
142          <td width="20%" align="center">
143            <a accesskey="u" href="txnconcurrency.html">Up</a>
144          </td>
145          <td width="40%" align="right"> <a accesskey="n" href="txnnowait.html">Next</a></td>
146        </tr>
147        <tr>
148          <td width="40%" align="left" valign="top">Transactional Cursors and Concurrent Applications </td>
149          <td width="20%" align="center">
150            <a accesskey="h" href="index.html">Home</a>
151          </td>
152          <td width="40%" align="right" valign="top"> No Wait on Blocks</td>
153        </tr>
154      </table>
155    </div>
156  </body>
157</html>
158