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