001: package org.apache.ojb.odmg.locking;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.broker.Identity;
019: import org.apache.ojb.broker.util.logging.Logger;
020: import org.apache.ojb.broker.util.logging.LoggerFactory;
021: import org.apache.ojb.odmg.TransactionImpl;
022:
023: /**
024: * The OJB default implementation of a Locking mechanism.
025: * This Implementation supports 4 transaction isolation levels
026: * as specified in the interface {@link org.apache.ojb.broker.locking.IsolationLevels}:
027: * public final int IL_READ_UNCOMMITTED = 0;
028: * public final int IL_READ_COMMITTED = 1;
029: * public final int IL_REPEATABLE_READ = 2;
030: * public final int IL_SERIALIZABLE = 3;
031: * Isolationlevels can be adjusted per class.
032: * The proper lockhandling is done in the respective LockStrategy implementation.
033: * This default implementation provides persistent Locks that are stored in
034: * a special database table.
035: * To keep the locks in the database and not in memory allows to use
036: * them accross multiple distributed ODMG clients.
037: *
038: * Of course this solution causes a lot of database reads and writes even if
039: * no real application data is written to the database. This solution may
040: * thus not be suited for all environments. As the LockManager is pluggable
041: * its possible to replace the default implementation by user defined
042: * implementations.
043: * A different solution might be to implement the LockManager as an additional
044: * standalone server, that allows to elminate additional db reads and writes.
045: *
046: * @author thma
047: * @deprecated
048: */
049: public class LockManagerDefaultImpl implements LockManager {
050: private Logger log = LoggerFactory
051: .getLogger(LockManagerDefaultImpl.class);
052:
053: public LockManagerDefaultImpl() {
054: }
055:
056: /**
057: * aquires a readlock for transaction tx on object obj.
058: * Returns true if successful, else false.
059: */
060: public synchronized boolean readLock(TransactionImpl tx, Object obj) {
061: if (log.isDebugEnabled())
062: log.debug("LM.readLock(tx-" + tx.getGUID() + ", "
063: + new Identity(obj, tx.getBroker()).toString()
064: + ")");
065: LockStrategy lockStrategy = LockStrategyFactory
066: .getStrategyFor(obj);
067: return lockStrategy.readLock(tx, obj);
068: }
069:
070: public boolean readLock(TransactionImpl tx, Identity oid, Object obj) {
071: return readLock(tx, obj);
072: }
073:
074: /**
075: * aquires a writelock for transaction tx on object obj.
076: * Returns true if successful, else false.
077: */
078: public synchronized boolean writeLock(TransactionImpl tx, Object obj) {
079: if (log.isDebugEnabled())
080: log.debug("LM.writeLock(tx-" + tx.getGUID() + ", "
081: + new Identity(obj, tx.getBroker()).toString()
082: + ")");
083: LockStrategy lockStrategy = LockStrategyFactory
084: .getStrategyFor(obj);
085: return lockStrategy.writeLock(tx, obj);
086: }
087:
088: public boolean writeLock(TransactionImpl tx, Identity oid,
089: Object obj) {
090: return writeLock(tx, obj);
091: }
092:
093: /**
094: * upgrades readlock for transaction tx on object obj to a writelock.
095: * If no readlock existed a writelock is acquired anyway.
096: * Returns true if successful, else false.
097: */
098: public synchronized boolean upgradeLock(TransactionImpl tx,
099: Object obj) {
100: if (log.isDebugEnabled())
101: log.debug("LM.upgradeLock(tx-" + tx.getGUID() + ", "
102: + new Identity(obj, tx.getBroker()).toString()
103: + ")");
104: LockStrategy lockStrategy = LockStrategyFactory
105: .getStrategyFor(obj);
106: return lockStrategy.upgradeLock(tx, obj);
107: }
108:
109: public boolean upgradeLock(TransactionImpl tx, Identity oid,
110: Object obj) {
111: return upgradeLock(tx, obj);
112: }
113:
114: /**
115: * releases a lock for transaction tx on object obj.
116: * Returns true if successful, else false.
117: */
118: public synchronized boolean releaseLock(TransactionImpl tx,
119: Object obj) {
120: if (log.isDebugEnabled())
121: log.debug("LM.releaseLock(tx-" + tx.getGUID() + ", "
122: + new Identity(obj, tx.getBroker()).toString()
123: + ")");
124: LockStrategy lockStrategy = LockStrategyFactory
125: .getStrategyFor(obj);
126: return lockStrategy.releaseLock(tx, obj);
127: }
128:
129: public boolean releaseLock(TransactionImpl tx, Identity oid,
130: Object obj) {
131: return releaseLock(tx, obj);
132: }
133:
134: /**
135: * checks if there is a readlock for transaction tx on object obj.
136: * Returns true if so, else false.
137: */
138: public synchronized boolean checkRead(TransactionImpl tx, Object obj) {
139: if (log.isDebugEnabled())
140: log.debug("LM.checkRead(tx-" + tx.getGUID() + ", "
141: + new Identity(obj, tx.getBroker()).toString()
142: + ")");
143: LockStrategy lockStrategy = LockStrategyFactory
144: .getStrategyFor(obj);
145: return lockStrategy.checkRead(tx, obj);
146: }
147:
148: public boolean checkRead(TransactionImpl tx, Identity oid,
149: Object obj) {
150: return checkRead(tx, obj);
151: }
152:
153: /**
154: * checks if there is a writelock for transaction tx on object obj.
155: * Returns true if so, else false.
156: */
157: public synchronized boolean checkWrite(TransactionImpl tx,
158: Object obj) {
159: if (log.isDebugEnabled())
160: log.debug("LM.checkWrite(tx-" + tx.getGUID() + ", "
161: + new Identity(obj, tx.getBroker()).toString()
162: + ")");
163: LockStrategy lockStrategy = LockStrategyFactory
164: .getStrategyFor(obj);
165: return lockStrategy.checkWrite(tx, obj);
166: }
167:
168: public boolean checkWrite(TransactionImpl tx, Identity oid,
169: Object obj) {
170: return checkWrite(tx, obj);
171: }
172: }
|