1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
public class TransactionIsolationManager { private final MVCCVersionManager mvccManager; private final LockManager lockManager; private final TransactionManager transactionManager; public TransactionIsolationManager() { this.mvccManager = new MVCCVersionManager(); this.lockManager = new LockManager(); this.transactionManager = new TransactionManager(); }
public Object executeQuery(long transactionId, IsolationLevel isolationLevel, String tableName, String key) { switch (isolationLevel) { case READ_UNCOMMITTED: return executeReadUncommitted(transactionId, tableName, key); case READ_COMMITTED: return executeReadCommitted(transactionId, tableName, key); case REPEATABLE_READ: return executeRepeatableRead(transactionId, tableName, key); case SERIALIZABLE: return executeSerializable(transactionId, tableName, key); default: throw new IllegalArgumentException("不支持的隔离级别: " + isolationLevel); } }
public Object executeUpdate(long transactionId, IsolationLevel isolationLevel, String tableName, String key, Object newData) { switch (isolationLevel) { case READ_UNCOMMITTED: return executeUpdateReadUncommitted(transactionId, tableName, key, newData); case READ_COMMITTED: return executeUpdateReadCommitted(transactionId, tableName, key, newData); case REPEATABLE_READ: return executeUpdateRepeatableRead(transactionId, tableName, key, newData); case SERIALIZABLE: return executeUpdateSerializable(transactionId, tableName, key, newData); default: throw new IllegalArgumentException("不支持的隔离级别: " + isolationLevel); } }
private Object executeReadUncommitted(long transactionId, String tableName, String key) { return mvccManager.currentRead(tableName, key, LockType.SHARED); }
private Object executeReadCommitted(long transactionId, String tableName, String key) { ReadView readView = mvccManager.createReadView(transactionId); return mvccManager.snapshotRead(tableName, key, readView); }
private Object executeRepeatableRead(long transactionId, String tableName, String key) { ReadView readView = mvccManager.getReadView(transactionId); if (readView == null) { readView = mvccManager.createReadView(transactionId); } return mvccManager.snapshotRead(tableName, key, readView); }
private Object executeSerializable(long transactionId, String tableName, String key) { return mvccManager.currentRead(tableName, key, LockType.EXCLUSIVE); }
private Object executeUpdateReadUncommitted(long transactionId, String tableName, String key, Object newData) { return updateData(transactionId, tableName, key, newData); }
private Object executeUpdateReadCommitted(long transactionId, String tableName, String key, Object newData) { lockManager.acquireLock(tableName, key, LockType.EXCLUSIVE, transactionId); try { return updateData(transactionId, tableName, key, newData); } finally { lockManager.releaseLock(tableName, key, transactionId); } }
private Object executeUpdateRepeatableRead(long transactionId, String tableName, String key, Object newData) { lockManager.acquireLock(tableName, key, LockType.EXCLUSIVE, transactionId); try { return updateData(transactionId, tableName, key, newData); } finally { lockManager.releaseLock(tableName, key, transactionId); } }
private Object executeUpdateSerializable(long transactionId, String tableName, String key, Object newData) { lockManager.acquireLock(tableName, key, LockType.EXCLUSIVE, transactionId); try { return updateData(transactionId, tableName, key, newData); } finally { lockManager.releaseLock(tableName, key, transactionId); } }
private Object updateData(long transactionId, String tableName, String key, Object newData) { Object oldData = mvccManager.currentRead(tableName, key, LockType.EXCLUSIVE); UndoLog undoLog = mvccManager.getUndoLogManager().createUndoLog(tableName, key, oldData, newData); Version newVersion = new Version(transactionId, System.currentTimeMillis(), newData, undoLog.getLogId()); mvccManager.getVersionChainManager().addVersion(tableName, key, newVersion); return newData; } }
class LockManager { private final Map<String, Lock> locks; private final Map<Long, Set<String>> transactionLocks; public LockManager() { this.locks = new ConcurrentHashMap<>(); this.transactionLocks = new ConcurrentHashMap<>(); }
public void acquireLock(String tableName, String key, LockType lockType, long transactionId) { String lockKey = tableName + ":" + key; Lock existingLock = locks.get(lockKey); if (existingLock != null) { if (!isLockCompatible(existingLock.getLockType(), lockType)) { waitForLock(lockKey); } } Lock newLock = new Lock(tableName, key, lockType); locks.put(lockKey, newLock); transactionLocks.computeIfAbsent(transactionId, k -> ConcurrentHashMap.newKeySet()) .add(lockKey); }
public void releaseLock(String tableName, String key, long transactionId) { String lockKey = tableName + ":" + key; Lock lock = locks.get(lockKey); if (lock != null && lock.getTransactionId() == transactionId) { locks.remove(lockKey); Set<String> transactionLockSet = transactionLocks.get(transactionId); if (transactionLockSet != null) { transactionLockSet.remove(lockKey); } } }
private boolean isLockCompatible(LockType existingLockType, LockType requestedLockType) { if (existingLockType == LockType.SHARED && requestedLockType == LockType.SHARED) { return true; } return false; }
private void waitForLock(String lockKey) { try { Thread.sleep(10); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }
enum IsolationLevel { READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE }
|