001: package org.apache.ojb.broker.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: /**
019: * Factory class used to obtain the proper {@link LockIsolation} level.
020: *
021: * @version $Id: LockIsolationManager.java,v 1.1.2.2 2005/12/21 22:25:32 tomdz Exp $
022: */
023: class LockIsolationManager {
024: private LockIsolation readUncommitedStrategy;
025: private LockIsolation readCommitedStrategy;
026: private LockIsolation readRepeatableStrategy;
027: private LockIsolation serializableStrategy;
028:
029: LockIsolationManager() {
030: readUncommitedStrategy = new ReadUncommittedIsolation();
031: readCommitedStrategy = new ReadCommittedIsolation();
032: readRepeatableStrategy = new RepeatableReadIsolation();
033: serializableStrategy = new SerializableIsolation();
034: }
035:
036: /**
037: * Obtains a lock isolation for Object obj. The Strategy to be used is
038: * selected by evaluating the ClassDescriptor of obj.getClass().
039: */
040: public LockIsolation getStrategyFor(int isolationLevel) {
041: switch (isolationLevel) {
042: case LockManager.IL_READ_UNCOMMITTED:
043: return readUncommitedStrategy;
044: case LockManager.IL_READ_COMMITTED:
045: return readCommitedStrategy;
046: case LockManager.IL_REPEATABLE_READ:
047: return readRepeatableStrategy;
048: case LockManager.IL_SERIALIZABLE:
049: return serializableStrategy;
050: default:
051: return readUncommitedStrategy;
052: }
053: }
054:
055: //===============================================
056: // inner class, LockIsolation implementation
057: //===============================================
058: /**
059: * The implementation of the Uncommited Reads Locking strategy.
060: * This strategy is the loosest of them all. It says
061: * you shouldn't need to get any Read locks whatsoever,
062: * but since it will probably try to get them, it will
063: * always give it to them.
064: * <p/>
065: * Allows:
066: * Dirty Reads
067: * Non-Repeatable Reads
068: * Phantom Reads
069: */
070: class ReadUncommittedIsolation extends LockIsolation {
071: ReadUncommittedIsolation() {
072: }
073:
074: public int getIsolationLevel() {
075: return LockManager.IL_READ_UNCOMMITTED;
076: }
077:
078: public String getIsolationLevelAsString() {
079: return LockManager.LITERAL_IL_READ_UNCOMMITTED;
080: }
081:
082: public boolean allowMultipleRead() {
083: return true;
084: }
085:
086: public boolean allowWriteWhenRead() {
087: return true;
088: }
089:
090: public boolean allowReadWhenWrite() {
091: return true;
092: }
093: }
094:
095: //===============================================
096: // inner class, LockIsolation implementation
097: //===============================================
098: /**
099: * The implementation of the Commited Reads Locking strategy.
100: * ReadCommitted - Reads and Writes require locks.
101: * <p/>
102: * Locks are acquired for reading and modifying the database.
103: * Locks are released after reading but locks on modified objects
104: * are held until EOT.
105: * <p/>
106: * Allows:
107: * Non-Repeatable Reads,
108: * Phantom Reads.
109: */
110: class ReadCommittedIsolation extends LockIsolation {
111: ReadCommittedIsolation() {
112: }
113:
114: public int getIsolationLevel() {
115: return LockManager.IL_READ_COMMITTED;
116: }
117:
118: public String getIsolationLevelAsString() {
119: return LockManager.LITERAL_IL_READ_COMMITTED;
120: }
121:
122: public boolean allowMultipleRead() {
123: return true;
124: }
125:
126: public boolean allowWriteWhenRead() {
127: return true;
128: }
129:
130: public boolean allowReadWhenWrite() {
131: return false;
132: }
133: }
134:
135: //===============================================
136: // inner class, LockIsolation implementation
137: //===============================================
138: /**
139: * The implementation of the Repeatable Reads Locking strategy.
140: * Locks are obtained for reading and modifying the database.
141: * Locks on all modified objects are held until EOT.
142: * Locks obtained for reading data are held until EOT.
143: * Allows:
144: * Phantom Reads
145: */
146: class RepeatableReadIsolation extends LockIsolation {
147: public RepeatableReadIsolation() {
148: }
149:
150: public int getIsolationLevel() {
151: return LockManager.IL_REPEATABLE_READ;
152: }
153:
154: public String getIsolationLevelAsString() {
155: return LockManager.LITERAL_IL_REPEATABLE_READ;
156: }
157:
158: public boolean allowMultipleRead() {
159: return true;
160: }
161:
162: public boolean allowWriteWhenRead() {
163: return false;
164: }
165:
166: public boolean allowReadWhenWrite() {
167: return false;
168: }
169: }
170:
171: //===============================================
172: // inner class, LockIsolation implementation
173: //===============================================
174: /**
175: * The implementation of the Serializable Locking strategy.
176: */
177: class SerializableIsolation extends LockIsolation {
178:
179: SerializableIsolation() {
180: }
181:
182: public int getIsolationLevel() {
183: return LockManager.IL_SERIALIZABLE;
184: }
185:
186: public String getIsolationLevelAsString() {
187: return LockManager.LITERAL_IL_SERIALIZABLE;
188: }
189:
190: public boolean allowMultipleRead() {
191: return false;
192: }
193:
194: public boolean allowWriteWhenRead() {
195: return false;
196: }
197:
198: public boolean allowReadWhenWrite() {
199: return false;
200: }
201: }
202: }
|