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.odmg.TransactionImpl;
019
020 /**
021 * The implementation of the Uncommited Reads Locking strategy.
022 * This strategy is the loosest of them all. It says
023 * you shouldn't need to get any Read locks whatsoever,
024 * but since it will probably try to get them, it will
025 * always give it to them.
026 *
027 * Locks are obtained on modifications to the database and held until end of
028 * transaction (EOT). Reading from the database does not involve any locking.
029 *
030 * Allows:
031 * Dirty Reads
032 * Non-Repeatable Reads
033 * Phantom Reads
034 *
035 * @author Thomas Mahler & David Dixon-Peugh
036 */
037 public class ReadUncommittedStrategy extends AbstractLockStrategy
038 {
039 /**
040 * acquire a read lock on Object obj for Transaction tx.
041 * @param tx the transaction requesting the lock
042 * @param obj the Object to be locked
043 * @return true if successful, else false
044 * When we read Uncommitted, we don't care about Reader locks
045 */
046 public boolean readLock(TransactionImpl tx, Object obj)
047 {
048 return true;
049 }
050
051 /**
052 * acquire a write lock on Object obj for Transaction tx.
053 * @param tx the transaction requesting the lock
054 * @param obj the Object to be locked
055 * @return true if successful, else false
056 *
057 */
058 public boolean writeLock(TransactionImpl tx, Object obj)
059 {
060 LockEntry writer = getWriter(obj);
061 if (writer == null)
062 {
063 if (setWriter(tx, obj))
064 return true;
065 else
066 return writeLock(tx, obj);
067 }
068 if (writer.isOwnedBy(tx))
069 {
070 return true; // If I'm the writer, then I can write.
071 }
072 return false;
073 }
074
075 /**
076 * acquire a lock upgrade (from read to write) lock on Object obj for Transaction tx.
077 * @param tx the transaction requesting the lock
078 * @param obj the Object to be locked
079 * @return true if successful, else false
080 *
081 */
082 public boolean upgradeLock(TransactionImpl tx, Object obj)
083 {
084 LockEntry writer = getWriter(obj);
085 if (writer == null)
086 {
087 if (setWriter(tx, obj))
088 return true;
089 else
090 return upgradeLock(tx, obj);
091 }
092 if (writer.isOwnedBy(tx))
093 {
094 return true; // If I already have Write, then I've upgraded.
095 }
096 return false;
097 }
098
099 /**
100 * release a lock on Object obj for Transaction tx.
101 * @param tx the transaction releasing the lock
102 * @param obj the Object to be unlocked
103 * @return true if successful, else false
104 *
105 */
106 public boolean releaseLock(TransactionImpl tx, Object obj)
107 {
108 LockEntry writer = getWriter(obj);
109 if (writer != null && writer.isOwnedBy(tx))
110 {
111 removeWriter(writer);
112 return true;
113 }
114 // readlocks cannot (and need not) be released, thus:
115 return true;
116 }
117
118 /**
119 * checks whether the specified Object obj is read-locked by Transaction tx.
120 * @param tx the transaction
121 * @param obj the Object to be checked
122 * @return true if lock exists, else false
123 */
124 public boolean checkRead(TransactionImpl tx, Object obj)
125 {
126 return true;
127 }
128
129 /**
130 * checks whether the specified Object obj is write-locked by Transaction tx.
131 * @param tx the transaction
132 * @param obj the Object to be checked
133 * @return true if lock exists, else false
134 */
135 public boolean checkWrite(TransactionImpl tx, Object obj)
136 {
137 LockEntry writer = getWriter(obj);
138 return (writer != null && writer.isOwnedBy(tx));
139 }
140 }