1 package org.apache.ojb.otm.lock.isolation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.util.Collection;
19 import java.util.Iterator;
20
21 import org.apache.ojb.otm.core.Transaction;
22 import org.apache.ojb.otm.lock.LockingException;
23 import org.apache.ojb.otm.lock.ObjectLock;
24
25 public class RepeatableReadIsolation extends AbstractIsolation
26 {
27
28
29
30
31 public void readLock(Transaction tx, ObjectLock lock)
32 throws LockingException
33 {
34 Transaction writer = lock.getWriter();
35 if (writer == null)
36 {
37 lock.readLock(tx);
38 if (lock.getWriter() != null)
39 {
40 lock.releaseLock(tx);
41 readLock(tx, lock);
42 }
43 }
44 else if (tx != writer)
45 {
46 lock.waitForTx(writer);
47 readLock(tx, lock);
48 }
49
50 }
51
52
53
54
55 public void writeLock(Transaction tx, ObjectLock lock)
56 throws LockingException
57 {
58 Collection readers = lock.getReaders();
59
60 if (!readers.isEmpty())
61 {
62 for (Iterator it = readers.iterator(); it.hasNext(); )
63 {
64 Transaction reader = (Transaction) it.next();
65
66 if (reader != tx)
67 {
68 lock.waitForTx(reader);
69 writeLock(tx, lock);
70 return;
71 }
72 }
73 }
74
75 lock.writeLock(tx);
76 readers = lock.getReaders();
77 if (readers.size() > 1)
78 {
79 lock.releaseLock(tx);
80 writeLock(tx, lock);
81 }
82 }
83 }