| 18 | import org.jetbrains.annotations.Nullable; |
| 19 | |
| 20 | public final class Transaction { |
| 21 | private static final @NotNull Logger logger = LogManager.getLogger(Transaction.class); |
| 22 | private static final ThreadLocal<Transaction> threadLocal = new ThreadLocal<>(); |
| 23 | |
| 24 | public static @NotNull Transaction create(@NotNull Locks locks) { |
| 25 | var t = threadLocal.get(); |
| 26 | if (t == null) |
| 27 | threadLocal.set(t = new Transaction()); |
| 28 | t.locks = locks; |
| 29 | t.created = true; |
| 30 | return t; |
| 31 | } |
| 32 | |
| 33 | public static void destroy() { |
| 34 | var t = threadLocal.get(); |
| 35 | if (t != null) |
| 36 | t.reuseTransaction(); |
| 37 | } |
| 38 | |
| 39 | public static @Nullable Transaction getCurrent() { |
| 40 | var t = threadLocal.get(); |
| 41 | return t != null && t.created ? t : null; |
| 42 | } |
| 43 | |
| 44 | public static @Nullable Transaction getCurrentVerifyRead(@NotNull Bean bean) { |
| 45 | return getCurrent(); |
| 46 | } |
| 47 | |
| 48 | public static @NotNull Transaction getCurrentVerifyWrite(@NotNull Bean bean) { |
| 49 | var t = getCurrent(); |
| 50 | if (t == null) |
| 51 | throw new IllegalStateException("not in transaction"); |
| 52 | t.verifyRecordForWrite(bean); |
| 53 | return t; |
| 54 | } |
| 55 | |
| 56 | private final ArrayList<Lockey> holdLocks = new ArrayList<>(); // 读写锁的话需要一个包装类,用来记录当前维持的是哪个锁。 |
| 57 | private final ArrayList<Procedure> procedureStack = new ArrayList<>(); // 嵌套存储过程栈。 |
| 58 | final ArrayList<Runnable> logActions = new ArrayList<>(); |
| 59 | private final ArrayList<Savepoint> savepoints = new ArrayList<>(); |
| 60 | private final ArrayList<Savepoint.Action> actions = new ArrayList<>(); |
| 61 | private final TreeMap<TableKey, RecordAccessed> accessedRecords = new TreeMap<>(); |
| 62 | private Locks locks; |
| 63 | private @NotNull TransactionState state = TransactionState.Running; |
| 64 | private boolean created; |
| 65 | private boolean alwaysReleaseLockWhenRedo; |
| 66 | private final ArrayList<Bean> redoBeans = new ArrayList<>(); |
| 67 | private final ArrayList<Runnable> redoActions = new ArrayList<>(); |
| 68 | private @Nullable OnzProcedure onzProcedure; |
| 69 | final Profiler profiler = new Profiler(); |
| 70 | private final AtomicLong totalTransaction = new AtomicLong(); |
| 71 | private Long tid; // 线程不安全。 |
| 72 | |
| 73 | private Transaction() { |
| 74 | } |
| 75 | |
| 76 | public @NotNull ArrayList<Procedure> getProcedureStack() { |
| 77 | return procedureStack; |
nothing calls this directly
no outgoing calls
no test coverage detected