Skip to content

Commit

Permalink
[feat] 동시성 문제 해결하기 (#83)
Browse files Browse the repository at this point in the history
* feat #82 : 동시성 문제 해결하기

* test #82 : 동시성 문제 해결 테스트

* refactor #82 : 필요없는 if문 삭제 및 불필요한 변수 할당 제거

* docs #82 : 스키마 변경
  • Loading branch information
pie2457 authored Jan 3, 2024
1 parent 6654501 commit 9e8b83d
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,9 @@ private void updateImages(List<MultipartFile> itemImages, Item findItem) {
if (itemImages == null || itemImages.isEmpty()) {
return;
}
if (!itemImages.isEmpty()) {
List<String> updateImageUrls = imageService.uploadImagesToS3(itemImages);
imageRepository.saveAllImages(Image.createImage(updateImageUrls, findItem));
}

List<String> updateImageUrls = imageService.uploadImagesToS3(itemImages);
imageRepository.saveAllImages(Image.createImage(updateImageUrls, findItem));
}

private String updateThumbnail(Item item, String thumbnailUrl, MultipartFile thumbnail) {
Expand Down Expand Up @@ -170,8 +169,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
Loading

0 comments on commit 9e8b83d

Please sign in to comment.