1. Run4C2Demo
src/main/java/com/example/lockdeepdive/demo/Run4C2Demo.javaStartet die konkurrierenden Szenarien und zeigt die beobachtbaren Ergebnisse der verschiedenen Sperrstrategien.
- Typ
- class Run4C2Demo
- Verwendet
- OptimisticInventoryService, PessimisticInventoryService, RowLockManager, SimulatedTransaction, DeadlockSimulator
- Verwendet von
- —
- Einstiege
- main(String[] args)
package com.example.lockdeepdive.demo;
import com.example.lockdeepdive.deadlock.*;
import com.example.lockdeepdive.inventory.*;
import com.example.lockdeepdive.isolation.*;
import com.example.lockdeepdive.locking.*;
import com.example.lockdeepdive.shared.*;
import java.time.Duration;
public final class Run4C2Demo {
public static void main(String[] args) {
ProductId product = ProductId.of("SKU-LOCK-1");
InventoryTable table = new InventoryTable();
table.insert(product, 10);
NaiveReservationSession a = new NaiveReservationSession(table, table.read(product));
NaiveReservationSession b = new NaiveReservationSession(table, table.read(product));
a.planReservation(7);
b.planReservation(6);
a.commitWithoutVersionCheck();
b.commitWithoutVersionCheck();
System.out.println("LOST_UPDATE_FINAL_QUANTITY=" + table.quantity(product));
InventoryTable optimisticTable = new InventoryTable();
optimisticTable.insert(product, 10);
OptimisticInventoryService optimistic = new OptimisticInventoryService(optimisticTable, 3);
System.out.println("OPTIMISTIC_FIRST=" + optimistic.reserve(product, 7).message());
System.out.println("OPTIMISTIC_SECOND=" + optimistic.reserve(product, 6).message());
InventoryTable pessimisticTable = new InventoryTable();
pessimisticTable.insert(product, 10);
PessimisticInventoryService pessimistic = new PessimisticInventoryService(pessimisticTable, new RowLockManager(), Duration.ofMillis(300));
System.out.println("PESSIMISTIC=" + pessimistic.reserve(product, 5).message());
AccountTable accounts = new AccountTable();
AccountId account = AccountId.of("A-100");
accounts.insert(account, 100);
SimulatedTransaction readCommitted = new SimulatedTransaction(accounts, IsolationLevel.READ_COMMITTED);
System.out.println("RC_READ_1=" + readCommitted.read(account).balance());
accounts.update(account, 200);
System.out.println("RC_READ_2=" + readCommitted.read(account).balance());
DeadlockSimulationResult deadlock = new DeadlockSimulator().simulateOppositeLockOrder(Duration.ofMillis(200));
System.out.println("DEADLOCK_TIMEOUT=" + deadlock.deadlockWasBrokenByTimeout());
}
}