01: package org.apache.ojb.otm.lock;
02:
03: /* Copyright 2003-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
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: * Manages locks on objects across transactions.
27: *
28: * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
29: *
30: */
31: public class LockManager {
32: private static LockManager _Instance = new LockManager();
33:
34: public static LockManager getInstance() {
35: return _Instance;
36: }
37:
38: private LockManager() {
39:
40: }
41:
42: public void ensureLock(Identity oid, Transaction tx, int lock,
43: PersistenceBroker pb) throws LockingException {
44: LockMap lockMap = tx.getKit().getLockMap();
45: ObjectLock objectLock = lockMap.getLock(oid);
46: TransactionIsolation isolation;
47:
48: isolation = IsolationFactory.getIsolationLevel(pb, objectLock);
49:
50: if (lock == LockType.READ_LOCK) {
51: isolation.readLock(tx, objectLock);
52: } else if (lock == LockType.WRITE_LOCK) {
53: isolation.writeLock(tx, objectLock);
54: }
55: }
56:
57: public int getLockHeld(Identity oid, Transaction tx) {
58: LockMap lockMap = tx.getKit().getLockMap();
59: ObjectLock lock = lockMap.getLock(oid);
60:
61: int lockHeld = LockType.NO_LOCK;
62: if (tx.equals(lock.getWriter())) {
63: lockHeld = LockType.WRITE_LOCK;
64: } else if (lock.isReader(tx)) {
65: lockHeld = LockType.READ_LOCK;
66: }
67:
68: return lockHeld;
69: }
70:
71: public void releaseLock(Identity oid, Transaction tx) {
72: LockMap lockMap = tx.getKit().getLockMap();
73:
74: ObjectLock objectLock = lockMap.getLock(oid);
75: objectLock.releaseLock(tx);
76: }
77: }
|