1 package org.apache.ojb.odmg.locking; 2 3 17 18 import org.apache.ojb.odmg.TransactionImpl; 19 20 import java.util.Collection ; 21 22 32 public class RepeatableReadStrategy extends AbstractLockStrategy 33 { 34 41 public boolean readLock(TransactionImpl tx, Object obj) 42 { 43 LockEntry writer = getWriter(obj); 44 if (writer == null) 45 { 46 if (addReader(tx, obj)) 47 return true; 48 else 49 return readLock(tx, obj); 50 } 51 if (writer.isOwnedBy(tx)) 52 { 53 return true; } 55 else 56 return false; 57 58 } 59 60 67 public boolean writeLock(TransactionImpl tx, Object obj) 68 { 69 LockEntry writer = getWriter(obj); 70 Collection readers = getReaders(obj); 71 if (writer == null) 72 { 73 if (readers.size() == 0) 74 { 75 if (setWriter(tx, obj)) 76 return true; 77 else 78 return writeLock(tx, obj); 79 } 80 81 else if (readers.size() == 1) 82 { 83 if (((LockEntry) readers.iterator().next()).isOwnedBy(tx)) 84 return upgradeLock(tx, obj); 85 } 86 } 87 else if (writer.isOwnedBy(tx)) 88 { 89 return true; } 91 return false; 92 } 93 94 95 102 public boolean upgradeLock(TransactionImpl tx, Object obj) 103 { 104 LockEntry writer = getWriter(obj); 105 if (writer == null) 106 { 107 Collection readers = this.getReaders(obj); 108 if (readers.size() == 1) 109 { 110 LockEntry reader = (LockEntry) readers.iterator().next(); 111 if (reader.isOwnedBy(tx)) 112 { 113 if (upgradeLock(reader)) 114 return true; 115 else 116 return upgradeLock(tx, obj); 117 } 118 } 119 else if (readers.size() == 0) 120 { 121 if (setWriter(tx, obj)) 122 return true; 123 else 124 return upgradeLock(tx, obj); 125 } 126 127 128 } 129 else if (writer.isOwnedBy(tx)) 130 { 131 return true; } 133 134 return false; 135 } 136 137 144 public boolean releaseLock(TransactionImpl tx, Object obj) 145 { 146 LockEntry writer = getWriter(obj); 147 if (writer != null && writer.isOwnedBy(tx)) 148 { 149 removeWriter(writer); 150 return true; 151 } 152 if (hasReadLock(tx, obj)) 153 { 154 removeReader(tx, obj); 155 return true; 156 } 157 return false; 158 } 159 160 166 public boolean checkRead(TransactionImpl tx, Object obj) 167 { 168 if (hasReadLock(tx, obj)) 169 { 170 return true; 171 } 172 LockEntry writer = getWriter(obj); 173 if (writer != null && writer.isOwnedBy(tx)) 174 { 175 return true; 176 } 177 else 178 return false; 179 } 180 181 187 public boolean checkWrite(TransactionImpl tx, Object obj) 188 { 189 LockEntry writer = getWriter(obj); 190 return (writer != null && writer.isOwnedBy(tx)); 191 } 192 } 193 | Popular Tags |