/** * Dirty reads, non-repeatable reads and phantom reads are'n allowed. * Concurrent and serial execution of transactions with this isolation level * should have the same effect. */ SERIALIZABLE(Connection.TRANSACTION_SERIALIZABLE, Constants.LOCK_MODE_TABLE); }
/** * 用于写的 map * The map used for writing (the latest version). * <p> * Key: key the key of the data. * Value: { transactionId, oldVersion, value } */ publicfinal MVMap<K, VersionedValue<V>> map;
/** * 当前 map 关联的事务 * The transaction which is used for this map. */ privatefinal Transaction transaction;
/** * 根据隔离级别可能是事务开启时的快照,或者事务执行 statement 时候的快照. * * Snapshot of this map as of beginning of transaction or * first usage within transaction or * beginning of the statement, depending on isolation level */ private Snapshot<K,VersionedValue<V>> snapshot;
/** * 开始执行 statement 时候的快照. * * Snapshot of this map as of beginning of beginning of the statement */ private Snapshot<K,VersionedValue<V>> statementSnapshot;
/** * map数据是否被当前事务修改过 * Indicates whether underlying map was modified from within related transaction */ privateboolean hasChanges;
final TransactionStore store; final TransactionStore.RollbackListener listener;
// 事务id finalint transactionId;
/* 事务状态是一个原子复合字段:1.创建事务时初始化;2.事务内操作会递增logId;2.事务提交/回滚时修改事务状态 status * Transaction state is an atomic composite field: * bit 45 : flag whether transaction had rollback(s) 位 45:标记事务是否有回滚 * bits 44-41 : status 位 44-41:状态位 * bits 40 : overflow control bit, 1 indicates overflow 40:溢出控制位,1 表示溢出 * bits 39-0 : log id of the last entry in the undo log map 位 39-0:undo log 日志 map 中最后一个条目的日志 ID */ privatefinal AtomicLong statusAndLogId;
/** * Get the value for the given key, or null if value does not exist in accordance with transactional rules. * Value is taken from a snapshot, appropriate for an isolation level of the related transaction * * @param key the key * @return the value, or null if not found */ public V getFromSnapshot(K key) { switch (transaction.isolationLevel) { case READ_UNCOMMITTED: { // 直接从语句快照中获取值 Snapshot<K,VersionedValue<V>> snapshot = getStatementSnapshot(); VersionedValue<V> data = map.get(snapshot.root.root, key); if (data != null) { return data.getCurrentValue(); } returnnull; } case REPEATABLE_READ: case SNAPSHOT: case SERIALIZABLE: if (transaction.hasChanges()) { // RR 检查当前事务是否有更改.如果有更改,从语句快照中获取值 Snapshot<K,VersionedValue<V>> snapshot = getStatementSnapshot(); VersionedValue<V> data = map.get(snapshot.root.root, key); if (data != null) { longid= data.getOperationId(); if (id != 0L && transaction.transactionId == TransactionStore.getTransactionId(id)) { return data.getCurrentValue(); } } } //$FALL-THROUGH$ case READ_COMMITTED: default: Snapshot<K,VersionedValue<V>> snapshot = getSnapshot(); // 获取 snapshot return getFromSnapshot(snapshot.root, snapshot.committingTransactions, key); // 获取 snapshot 中的数据 } }
private V getFromSnapshot(RootReference<K, VersionedValue<V>> rootRef, BitSet committingTransactions, K key) { VersionedValue<V> data = map.get(rootRef.root, key); if (data == null) { // doesn't exist returnnull; } longid= data.getOperationId(); // 获取操作 id if (id != 0) { // 如果操作 id 不为 0 inttx= TransactionStore.getTransactionId(id); // 获取事务 id if (tx != transaction.transactionId && !committingTransactions.get(tx)) { // 如果事务 id 不等于当前事务 id 并且 committingTransactions 不包含这个事务(不是当前事务&事务未提交) // added/modified/removed by uncommitted transaction, change should not be visible return data.getCommittedValue(); // 返回已提交的值 } } // added/modified/removed by this transaction or another transaction which is committed by now return data.getCurrentValue(); // (当前事务 或 已提交事务),返回当前值 }