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    {
051        private Logger log = LoggerFactory.getLogger(LockManagerDefaultImpl.class);
052    
053        public LockManagerDefaultImpl()
054        {
055        }
056    
057        /**
058         * aquires a readlock for transaction tx on object obj.
059         * Returns true if successful, else false.
060         */
061        public synchronized boolean readLock(TransactionImpl tx, Object obj)
062        {
063            if (log.isDebugEnabled()) log.debug("LM.readLock(tx-" + tx.getGUID() + ", " + new Identity(obj, tx.getBroker()).toString() + ")");
064            LockStrategy lockStrategy = LockStrategyFactory.getStrategyFor(obj);
065            return lockStrategy.readLock(tx, obj);
066        }
067    
068        public boolean readLock(TransactionImpl tx, Identity oid, Object obj)
069        {
070            return readLock(tx,obj);
071        }
072    
073        /**
074         * aquires a writelock for transaction tx on object obj.
075         * Returns true if successful, else false.
076         */
077        public synchronized boolean writeLock(TransactionImpl tx, Object obj)
078        {
079            if (log.isDebugEnabled()) log.debug("LM.writeLock(tx-" + tx.getGUID() + ", " + new Identity(obj, tx.getBroker()).toString() + ")");
080            LockStrategy lockStrategy = LockStrategyFactory.getStrategyFor(obj);
081            return lockStrategy.writeLock(tx, obj);
082        }
083    
084        public boolean writeLock(TransactionImpl tx, Identity oid, Object obj)
085        {
086            return writeLock(tx, obj);
087        }
088    
089        /**
090         * upgrades readlock for transaction tx on object obj to a writelock.
091         * If no readlock existed a writelock is acquired anyway.
092         * Returns true if successful, else false.
093         */
094        public synchronized boolean upgradeLock(TransactionImpl tx, Object obj)
095        {
096            if (log.isDebugEnabled()) log.debug("LM.upgradeLock(tx-" + tx.getGUID() + ", " + new Identity(obj, tx.getBroker()).toString() + ")");
097            LockStrategy lockStrategy = LockStrategyFactory.getStrategyFor(obj);
098            return lockStrategy.upgradeLock(tx, obj);
099        }
100    
101        public boolean upgradeLock(TransactionImpl tx, Identity oid, Object obj)
102        {
103            return upgradeLock(tx, obj);
104        }
105    
106        /**
107         * releases a lock for transaction tx on object obj.
108         * Returns true if successful, else false.
109         */
110        public synchronized boolean releaseLock(TransactionImpl tx, Object obj)
111        {
112            if (log.isDebugEnabled()) log.debug("LM.releaseLock(tx-" + tx.getGUID() + ", " + new Identity(obj, tx.getBroker()).toString() + ")");
113            LockStrategy lockStrategy = LockStrategyFactory.getStrategyFor(obj);
114            return lockStrategy.releaseLock(tx, obj);
115        }
116    
117        public boolean releaseLock(TransactionImpl tx, Identity oid, Object obj)
118        {
119            return releaseLock(tx, obj);
120        }
121    
122        /**
123         * checks if there is a readlock for transaction tx on object obj.
124         * Returns true if so, else false.
125         */
126        public synchronized boolean checkRead(TransactionImpl tx, Object obj)
127        {
128            if (log.isDebugEnabled()) log.debug("LM.checkRead(tx-" + tx.getGUID() + ", " + new Identity(obj, tx.getBroker()).toString() + ")");
129            LockStrategy lockStrategy = LockStrategyFactory.getStrategyFor(obj);
130            return lockStrategy.checkRead(tx, obj);
131        }
132    
133        public boolean checkRead(TransactionImpl tx, Identity oid, Object obj)
134        {
135            return checkRead(tx, obj);
136        }
137    
138        /**
139         * checks if there is a writelock for transaction tx on object obj.
140         * Returns true if so, else false.
141         */
142        public synchronized boolean checkWrite(TransactionImpl tx, Object obj)
143        {
144            if (log.isDebugEnabled()) log.debug("LM.checkWrite(tx-" + tx.getGUID() + ", " + new Identity(obj, tx.getBroker()).toString() + ")");
145            LockStrategy lockStrategy = LockStrategyFactory.getStrategyFor(obj);
146            return lockStrategy.checkWrite(tx, obj);
147        }
148    
149        public boolean checkWrite(TransactionImpl tx, Identity oid, Object obj)
150        {
151            return checkWrite(tx, obj);
152        }
153    }