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: * acquire a read lock on Object obj for Transaction tx.
040: * @param tx the transaction requesting the lock
041: * @param obj the Object to be locked
042: * @return true if successful, else false
043: * When we read Uncommitted, we don't care about Reader locks
044: */
045: public boolean readLock(TransactionImpl tx, Object obj) {
046: return true;
047: }
048:
049: /**
050: * acquire a write lock on Object obj for Transaction tx.
051: * @param tx the transaction requesting the lock
052: * @param obj the Object to be locked
053: * @return true if successful, else false
054: *
055: */
056: public boolean writeLock(TransactionImpl tx, Object obj) {
057: LockEntry writer = getWriter(obj);
058: if (writer == null) {
059: if (setWriter(tx, obj))
060: return true;
061: else
062: return writeLock(tx, obj);
063: }
064: if (writer.isOwnedBy(tx)) {
065: return true; // If I'm the writer, then I can write.
066: }
067: return false;
068: }
069:
070: /**
071: * acquire a lock upgrade (from read to write) lock on Object obj for Transaction tx.
072: * @param tx the transaction requesting the lock
073: * @param obj the Object to be locked
074: * @return true if successful, else false
075: *
076: */
077: public boolean upgradeLock(TransactionImpl tx, Object obj) {
078: LockEntry writer = getWriter(obj);
079: if (writer == null) {
080: if (setWriter(tx, obj))
081: return true;
082: else
083: return upgradeLock(tx, obj);
084: }
085: if (writer.isOwnedBy(tx)) {
086: return true; // If I already have Write, then I've upgraded.
087: }
088: return false;
089: }
090:
091: /**
092: * release a lock on Object obj for Transaction tx.
093: * @param tx the transaction releasing the lock
094: * @param obj the Object to be unlocked
095: * @return true if successful, else false
096: *
097: */
098: public boolean releaseLock(TransactionImpl tx, Object obj) {
099: LockEntry writer = getWriter(obj);
100: if (writer != null && writer.isOwnedBy(tx)) {
101: removeWriter(writer);
102: return true;
103: }
104: // readlocks cannot (and need not) be released, thus:
105: return true;
106: }
107:
108: /**
109: * checks whether the specified Object obj is read-locked by Transaction tx.
110: * @param tx the transaction
111: * @param obj the Object to be checked
112: * @return true if lock exists, else false
113: */
114: public boolean checkRead(TransactionImpl tx, Object obj) {
115: return true;
116: }
117:
118: /**
119: * checks whether the specified Object obj is write-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 checkWrite(TransactionImpl tx, Object obj) {
125: LockEntry writer = getWriter(obj);
126: return (writer != null && writer.isOwnedBy(tx));
127: }
128: }
|