1 package org.apache.ojb.odmg.locking; 2 3 17 18 import org.apache.ojb.broker.PersistenceBrokerException; 19 import org.apache.ojb.broker.core.proxy.ProxyHelper; 20 import org.apache.ojb.broker.locking.IsolationLevels; 21 import org.apache.ojb.broker.metadata.ClassDescriptor; 22 import org.apache.ojb.broker.util.logging.LoggerFactory; 23 import org.apache.ojb.odmg.TransactionImpl; 24 import org.apache.ojb.odmg.TxManagerFactory; 25 26 32 public class LockStrategyFactory 33 { 34 35 39 private LockStrategyFactory() 40 { 41 } 42 43 private static LockStrategy readUncommitedStrategy = new ReadUncommittedStrategy(); 44 private static LockStrategy readCommitedStrategy = new ReadCommittedStrategy(); 45 private static LockStrategy readRepeatableStrategy = new RepeatableReadStrategy(); 46 private static LockStrategy serializableStrategy = new SerializableStrategy(); 47 private static LockStrategy noopStrategy = new NOOPStrategy(); 48 49 50 56 public static LockStrategy getStrategyFor(Object obj) 57 { 58 int isolationLevel = getIsolationLevel(obj); 59 switch (isolationLevel) 60 { 61 case IsolationLevels.IL_READ_UNCOMMITTED: 62 return readUncommitedStrategy; 63 case IsolationLevels.IL_READ_COMMITTED: 64 return readCommitedStrategy; 65 case IsolationLevels.IL_REPEATABLE_READ: 66 return readRepeatableStrategy; 67 case IsolationLevels.IL_SERIALIZABLE: 68 return serializableStrategy; 69 case IsolationLevels.IL_OPTIMISTIC: 70 return noopStrategy; 71 case IsolationLevels.IL_NONE: 72 return noopStrategy; 73 default: 74 return readUncommitedStrategy; 75 } 76 } 77 78 84 public static int getIsolationLevel(Object obj) 85 { 86 Class c = ProxyHelper.getRealClass(obj); 87 int isolationLevel = IsolationLevels.IL_READ_UNCOMMITTED; 88 89 try 90 { 91 ClassDescriptor cld = TxManagerFactory.instance().getCurrentTransaction().getBroker().getClassDescriptor(c); 92 isolationLevel = cld.getIsolationLevel(); 93 } 94 catch (PersistenceBrokerException e) 95 { 96 LoggerFactory.getDefaultLogger().error("[LockStrategyFactory] Can't detect locking isolation level", e); 97 } 98 return isolationLevel; 99 } 100 101 static class NOOPStrategy implements LockStrategy 102 { 103 public boolean readLock(TransactionImpl tx, Object obj) 104 { 105 return true; 106 } 107 108 public boolean writeLock(TransactionImpl tx, Object obj) 109 { 110 return true; 111 } 112 113 public boolean upgradeLock(TransactionImpl tx, Object obj) 114 { 115 return true; 116 } 117 118 public boolean releaseLock(TransactionImpl tx, Object obj) 119 { 120 return false; 121 } 122 123 public boolean checkRead(TransactionImpl tx, Object obj) 124 { 125 return false; 126 } 127 128 public boolean checkWrite(TransactionImpl tx, Object obj) 129 { 130 return false; 131 } 132 } 133 } 134 | Popular Tags |