1 | |
package org.apache.ojb.otm.lock; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import org.apache.ojb.broker.Identity; |
19 | |
import org.apache.ojb.broker.PersistenceBroker; |
20 | |
import org.apache.ojb.otm.core.Transaction; |
21 | |
import org.apache.ojb.otm.lock.isolation.TransactionIsolation; |
22 | |
import org.apache.ojb.otm.lock.map.LockMap; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class LockManager |
32 | |
{ |
33 | |
private static LockManager _Instance = new LockManager(); |
34 | |
|
35 | |
public static LockManager getInstance () |
36 | |
{ |
37 | |
return _Instance; |
38 | |
} |
39 | |
|
40 | |
private LockManager() |
41 | |
{ |
42 | |
|
43 | |
} |
44 | |
|
45 | |
public void ensureLock(Identity oid, Transaction tx, int lock, |
46 | |
PersistenceBroker pb) |
47 | |
throws LockingException |
48 | |
{ |
49 | |
LockMap lockMap = tx.getKit().getLockMap(); |
50 | |
ObjectLock objectLock = lockMap.getLock(oid); |
51 | |
TransactionIsolation isolation; |
52 | |
|
53 | |
isolation = IsolationFactory.getIsolationLevel(pb, objectLock); |
54 | |
|
55 | |
if (lock == LockType.READ_LOCK) |
56 | |
{ |
57 | |
isolation.readLock(tx, objectLock); |
58 | |
} |
59 | |
else if (lock == LockType.WRITE_LOCK) |
60 | |
{ |
61 | |
isolation.writeLock(tx, objectLock); |
62 | |
} |
63 | |
} |
64 | |
|
65 | |
public int getLockHeld(Identity oid, Transaction tx) |
66 | |
{ |
67 | |
LockMap lockMap = tx.getKit().getLockMap(); |
68 | |
ObjectLock lock = lockMap.getLock(oid); |
69 | |
|
70 | |
int lockHeld = LockType.NO_LOCK; |
71 | |
if (tx.equals(lock.getWriter())) |
72 | |
{ |
73 | |
lockHeld = LockType.WRITE_LOCK; |
74 | |
} |
75 | |
else if (lock.isReader(tx)) |
76 | |
{ |
77 | |
lockHeld = LockType.READ_LOCK; |
78 | |
} |
79 | |
|
80 | |
return lockHeld; |
81 | |
} |
82 | |
|
83 | |
public void releaseLock(Identity oid, Transaction tx) |
84 | |
{ |
85 | |
LockMap lockMap = tx.getKit().getLockMap(); |
86 | |
|
87 | |
ObjectLock objectLock = lockMap.getLock(oid); |
88 | |
objectLock.releaseLock(tx); |
89 | |
} |
90 | |
} |