You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
cd docker;
docker-compose -f docker-compose-mysql.yaml up -d;
docker exec -it $(docker ps | grep mysql | awk -F""'{ print $1}') bash;
mysql -u root
create database test;
DB Lock
Pessimistic Lock
publicinterfaceStockRepositoryextendsJpaRepository<Stock, Long> {
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select s from Stock s where s.id = :id")
Optional<Stock> findByIdWithPessimisticLock(@Param("id") Longid);
}
재고 관리시스템 구현 코드 예시
@ServicepublicclassStockPessimisticLockServiceimplementsStockCommand {
privatefinalStockRepositorystockRepository;
publicStockPessimisticLockService(StockRepositorystockRepository) {
this.stockRepository = stockRepository;
}
/** * Decrease stock quantity. * @param id Product ID * @param quantity Quantity to decrease * @throws IllegalStateException If the stock quantity is less than the quantity to decrease */@Transactional@OverridepublicvoiddecreaseStockQuantity(Longid, Longquantity) {
Stockstock = stockRepository.findByIdWithPessimisticLock(id)
.orElseThrow(IllegalStateException::new);
stock.decrease(quantity);
}
}
Optimistic Lock
publicinterfaceStockRepositoryextendsJpaRepository<Stock, Long> {
@Lock(LockModeType.OPTIMISTIC)
@Query("select s from Stock s where s.id = :id")
Optional<Stock> findByIdWithOptimisticLock(@Param("id") Longid);
}