001 package org.apache.ojb.otm.lock;
002
003 /* Copyright 2003-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.metadata.ClassDescriptor;
019 import org.apache.ojb.broker.locking.IsolationLevels;
020 import org.apache.ojb.broker.PersistenceBroker;
021 import org.apache.ojb.otm.lock.isolation.ReadCommittedIsolation;
022 import org.apache.ojb.otm.lock.isolation.ReadUncommittedIsolation;
023 import org.apache.ojb.otm.lock.isolation.RepeatableReadIsolation;
024 import org.apache.ojb.otm.lock.isolation.SerializableIsolation;
025 import org.apache.ojb.otm.lock.isolation.TransactionIsolation;
026
027 /**
028 *
029 * <javadoc>
030 *
031 * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
032 *
033 */
034 public class IsolationFactory
035 {
036
037 private static final TransactionIsolation READ_UNCOMMITTED_ISOLATION
038 = new ReadUncommittedIsolation();
039 private static final TransactionIsolation READ_COMMITTED_ISOLATION
040 = new ReadCommittedIsolation();
041 private static final TransactionIsolation REPEATABLE_READ_ISOLATION
042 = new RepeatableReadIsolation();
043 private static final TransactionIsolation SERIALIZABLE_ISOLATION
044 = new SerializableIsolation();
045
046
047 /**
048 *
049 * Fetches the isolation level of given class from its ClassDescriptor.
050 *
051 */
052 public static TransactionIsolation getIsolationLevel (PersistenceBroker pb,
053 ObjectLock lock)
054 {
055 /*
056 arminw: use real object class instead of top-level class
057 to match isolation level of given class
058 */
059 // Class clazz = lock.getTargetIdentity().getObjectsTopLevelClass();
060 Class clazz = lock.getTargetIdentity().getObjectsRealClass();
061 ClassDescriptor classDescriptor = pb.getClassDescriptor(clazz);
062 int isolationLevel = classDescriptor.getIsolationLevel();
063
064 TransactionIsolation isolation = null;
065 switch (isolationLevel) {
066
067 case IsolationLevels.IL_READ_UNCOMMITTED:
068 isolation = READ_UNCOMMITTED_ISOLATION;
069 break;
070
071 case IsolationLevels.IL_READ_COMMITTED:
072 isolation = READ_COMMITTED_ISOLATION;
073 break;
074
075 case IsolationLevels.IL_REPEATABLE_READ:
076 isolation = REPEATABLE_READ_ISOLATION;
077 break;
078
079 case IsolationLevels.IL_SERIALIZABLE:
080 isolation = SERIALIZABLE_ISOLATION;
081 break;
082
083 default:
084 throw new UnknownIsolationException(
085 "Isolation level " + isolationLevel + " is not supported");
086 }
087
088 return isolation;
089 }
090
091 }