Skip to content

Commit

Permalink
feat #82 : 동시성 문제 해결하기
Browse files Browse the repository at this point in the history
  • Loading branch information
pie2457 committed Dec 31, 2023
1 parent 6654501 commit b402239
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ private void verifyExistsItem(Long itemId) {
}
}

@Transactional
public ItemDetailResponse itemDetails(Principal principal, Long itemId) {
Item findItem = itemRepository.findById(itemId)
Item findItem = itemRepository.findByIdWithPessimisticLock(itemId)
.orElseThrow(() -> new NotFoundException(ErrorCode.NOT_FOUND_ITEM));

itemViewCountRedisService.addViewCount(principal.getNickname(), itemId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

import java.util.Optional;

import javax.persistence.LockModeType;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.querydsl.core.types.dsl.BooleanExpression;

Expand All @@ -15,6 +20,10 @@ public interface ItemRepository extends JpaRepository<Item, Long> {

Optional<Item> findByIdAndMemberId(Long itemId, Long memberId);

@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select item from Item item where item.id = :itemId")
Optional<Item> findByIdWithPessimisticLock(@Param("itemId") Long itemId);

boolean existsItemById(Long itemId);

default BooleanExpression lessThanItemId(Long itemId) {
Expand Down

0 comments on commit b402239

Please sign in to comment.