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 Commited Reads Locking stra
022: * ReadCommitted - Reads and Writes require locks.
023: *
024: * Locks are acquired for reading and modifying the database.
025: * Locks are released after reading but locks on modified objects
026: * are held until EOT.
027: *
028: * Allows:
029: * Non-Repeatable Reads
030: * Phantom Readstegy.
031: *
032: * @author Thomas Mahler & David Dixon-Peugh
033: */
034: public class ReadCommittedStrategy extends AbstractLockStrategy {
035:
036: /**
037: * acquire a read lock on Object obj for Transaction tx.
038: * @param tx the transaction requesting the lock
039: * @param obj the Object to be locked
040: * @return true if successful, else false
041: *
042: */
043: public boolean readLock(TransactionImpl tx, Object obj) {
044:
045: LockEntry writer = getWriter(obj);
046: if (writer == null) {
047: addReader(tx, obj);
048: // if there has been a successful write locking, try again
049: if (getWriter(obj) == null)
050: return true;
051: else {
052: removeReader(tx, obj);
053: return readLock(tx, obj);
054: }
055: }
056: if (writer.isOwnedBy(tx)) {
057: return true; // If I'm the writer, I can read.
058: } else {
059: return false;
060: }
061: }
062:
063: /**
064: * acquire a write lock on Object obj for Transaction tx.
065: * @param tx the transaction requesting the lock
066: * @param obj the Object to be locked
067: * @return true if successful, else false
068: *
069: */
070: public boolean writeLock(TransactionImpl tx, Object obj) {
071: LockEntry writer = getWriter(obj);
072: // if there is no writer yet we can try to get the global write lock
073: if (writer == null) {
074: // if lock could be acquired return true
075: if (setWriter(tx, obj))
076: return true;
077: // else try again
078: else
079: return writeLock(tx, obj);
080: }
081: if (writer.isOwnedBy(tx)) {
082: return true; // If I'm the writer, then I can write.
083: }
084:
085: return false;
086: }
087:
088: /**
089: * acquire a lock upgrade (from read to write) lock on Object obj for Transaction tx.
090: * @param tx the transaction requesting the lock
091: * @param obj the Object to be locked
092: * @return true if successful, else false
093: *
094: */
095: public boolean upgradeLock(TransactionImpl tx, Object obj) {
096: LockEntry writer = getWriter(obj);
097: if (writer == null) {
098: // if lock could be acquired return true
099: if (setWriter(tx, obj))
100: return true;
101: // else try again
102: else
103: return upgradeLock(tx, obj);
104: }
105: if (writer.isOwnedBy(tx)) {
106: return true; // If I already have Write, then I've upgraded.
107: }
108:
109: return false;
110: }
111:
112: /**
113: * release a lock on Object obj for Transaction tx.
114: * @param tx the transaction releasing the lock
115: * @param obj the Object to be unlocked
116: * @return true if successful, else false
117: *
118: */
119: public boolean releaseLock(TransactionImpl tx, Object obj) {
120: LockEntry writer = getWriter(obj);
121:
122: if (writer != null && writer.isOwnedBy(tx)) {
123: removeWriter(writer);
124: return true;
125: }
126:
127: if (hasReadLock(tx, obj)) {
128: removeReader(tx, obj);
129: return true;
130: }
131: return false;
132: }
133:
134: /**
135: * checks whether the specified Object obj is read-locked by Transaction tx.
136: * @param tx the transaction
137: * @param obj the Object to be checked
138: * @return true if lock exists, else false
139: */
140: public boolean checkRead(TransactionImpl tx, Object obj) {
141: if (hasReadLock(tx, obj)) {
142: return true;
143: }
144: LockEntry writer = getWriter(obj);
145: if (writer.isOwnedBy(tx)) {
146: return true;
147: }
148: return false;
149: }
150:
151: /**
152: * checks whether the specified Object obj is write-locked by Transaction tx.
153: * @param tx the transaction
154: * @param obj the Object to be checked
155: * @return true if lock exists, else false
156: */
157: public boolean checkWrite(TransactionImpl tx, Object obj) {
158: LockEntry writer = getWriter(obj);
159: if (writer == null)
160: return false;
161: else if (writer.isOwnedBy(tx))
162: return true;
163: else
164: return false;
165: }
166: }
|