Replies: 1 comment
-
💡 비관적 락(Pessimistic Lock) 실무 적용 및 보충 설명작성해주신 동시성 문제와 비관적 락의 개념에 대해 깊이 공감하며, 실제 프로젝트(Spring Data JPA 환경 등)에서 이 기능을 어떻게 제어하고 최적화할 수 있는지 보충 답변을 드립니다. 1. JPA에서의 실전 구현Spring Data JPA를 사용하신다면 public interface CouponRepository extends JpaRepository<Coupon, Long> {
// SELECT ... FOR UPDATE 쿼리 생성
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select c from Coupon c where c.id = :id")
Optional<Coupon> findByIdWithPessimisticLock(Long id);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
동시성 문제 상황
쿠폰 재고: 100개
동시에 1000명이 다운로드 요청
Thread A: 재고 조회 (100개) → 재고 감소 (99개)
Thread B: 재고 조회 (100개) → 재고 감소 (99개) // 문제!
Thread C: 재고 조회 (100개) → 재고 감소 (99개) // 문제!
해결 방법
동시성 문제를 해결하는 대표적인 두 가지 방법:
핵심 개념
데이터를 읽는 순간 락(lock)을 걸어서 다른 트랜잭션의 접근을 차단합니다.
동작 원리
[사용자 A]
[사용자 B]
✅ 장점
✅단점
Beta Was this translation helpful? Give feedback.
All reactions