1 package org.apache.ojb.odmg.locking;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import org.apache.ojb.odmg.TransactionImpl;
19
20 import java.util.Collection;
21
22
23
24
25
26
27
28
29
30
31
32 public class RepeatableReadStrategy extends AbstractLockStrategy
33 {
34
35
36
37
38
39
40
41 public boolean readLock(TransactionImpl tx, Object obj)
42 {
43 LockEntry writer = getWriter(obj);
44 if (writer == null)
45 {
46 if (addReader(tx, obj))
47 return true;
48 else
49 return readLock(tx, obj);
50 }
51 if (writer.isOwnedBy(tx))
52 {
53 return true;
54 }
55 else
56 return false;
57
58 }
59
60
61
62
63
64
65
66
67 public boolean writeLock(TransactionImpl tx, Object obj)
68 {
69 LockEntry writer = getWriter(obj);
70 Collection readers = getReaders(obj);
71 if (writer == null)
72 {
73 if (readers.size() == 0)
74 {
75 if (setWriter(tx, obj))
76 return true;
77 else
78 return writeLock(tx, obj);
79 }
80
81 else if (readers.size() == 1)
82 {
83 if (((LockEntry) readers.iterator().next()).isOwnedBy(tx))
84 return upgradeLock(tx, obj);
85 }
86 }
87 else if (writer.isOwnedBy(tx))
88 {
89 return true;
90 }
91 return false;
92 }
93
94
95
96
97
98
99
100
101
102 public boolean upgradeLock(TransactionImpl tx, Object obj)
103 {
104 LockEntry writer = getWriter(obj);
105 if (writer == null)
106 {
107 Collection readers = this.getReaders(obj);
108 if (readers.size() == 1)
109 {
110 LockEntry reader = (LockEntry) readers.iterator().next();
111 if (reader.isOwnedBy(tx))
112 {
113 if (upgradeLock(reader))
114 return true;
115 else
116 return upgradeLock(tx, obj);
117 }
118 }
119 else if (readers.size() == 0)
120 {
121 if (setWriter(tx, obj))
122 return true;
123 else
124 return upgradeLock(tx, obj);
125 }
126
127
128 }
129 else if (writer.isOwnedBy(tx))
130 {
131 return true;
132 }
133
134 return false;
135 }
136
137
138
139
140
141
142
143
144 public boolean releaseLock(TransactionImpl tx, Object obj)
145 {
146 LockEntry writer = getWriter(obj);
147 if (writer != null && writer.isOwnedBy(tx))
148 {
149 removeWriter(writer);
150 return true;
151 }
152 if (hasReadLock(tx, obj))
153 {
154 removeReader(tx, obj);
155 return true;
156 }
157 return false;
158 }
159
160
161
162
163
164
165
166 public boolean checkRead(TransactionImpl tx, Object obj)
167 {
168 if (hasReadLock(tx, obj))
169 {
170 return true;
171 }
172 LockEntry writer = getWriter(obj);
173 if (writer != null && writer.isOwnedBy(tx))
174 {
175 return true;
176 }
177 else
178 return false;
179 }
180
181
182
183
184
185
186
187 public boolean checkWrite(TransactionImpl tx, Object obj)
188 {
189 LockEntry writer = getWriter(obj);
190 return (writer != null && writer.isOwnedBy(tx));
191 }
192 }