Skip to content

Commit

Permalink
feat : 투표시 동시성 문제를 방지하기 위한 처리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
1000kkannoo committed Sep 19, 2023
1 parent b49199d commit 9f57ae4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public CustomResponseEntity<VoteResponse.SelectResult> readVote(
return CustomResponseEntity.success(voteService.readVote(groupId, user));
}

// TODO: 동시성 문제가 발생할 것 같다
// 투표 참여 API
@PostMapping("/{groupId}/vote/select")
public CustomResponseEntity<VoteResponse.SelectResult> selectVote(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.moim.backend.domain.groupvote.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;

Expand Down Expand Up @@ -36,6 +33,9 @@ public class Vote {

private LocalDateTime endAt;

@Version
private Long version;

public void conclusionVote() {
this.isClosed = TRUE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.moim.backend.domain.user.entity.Users;
import com.moim.backend.global.common.Result;
import com.moim.backend.global.common.exception.CustomException;
import jakarta.persistence.OptimisticLockException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -66,17 +67,21 @@ public VoteResponse.Create createVote(VoteServiceRequest.Create request, Long gr
public VoteResponse.SelectResult selectVote(
Long groupId, List<Long> selectPlaceIds, Users user, LocalDateTime now
) {
Groups group = getGroups(groupId);
Vote vote = getVote(groupId);
validateVote(selectPlaceIds, now, vote);
try{
Groups group = getGroups(groupId);
Vote vote = getVote(groupId);
validateVote(selectPlaceIds, now, vote);

// 이미 투표를 했다면, 현재 유저가 투표한 목록을 가져온 후 제거
removeUserVotesIfExist(selectPlaceIds, user, vote);
// 이미 투표를 했다면, 현재 유저가 투표한 목록을 가져온 후 제거
removeUserVotesIfExist(selectPlaceIds, user, vote);

// 요청받은 bestPlaceId 값을 이용해 for 문을 돌면서 투표 save
saveUserVotesForSelectPlaces(selectPlaceIds, user, vote);
// 요청받은 bestPlaceId 값을 이용해 for 문을 돌면서 투표 save
saveUserVotesForSelectPlaces(selectPlaceIds, user, vote);

return VoteResponse.SelectResult.response(group, vote, toVoteStatusResponse(user, vote));
return VoteResponse.SelectResult.response(group, vote, toVoteStatusResponse(user, vote));
} catch (OptimisticLockException ole) {
throw new CustomException(CONCURRENCY_ISSUE_DETECTED);
}
}

private List<VoteResponse.VoteStatus> toVoteStatusResponse(Users user, Vote vote) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/moim/backend/global/common/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum Result {
// 공통
OK(0, "성공"),
FAIL(-1, "실패"),
CONCURRENCY_ISSUE_DETECTED(-2, "동시성 문제가 발생하였습니다. 재시도해주세요"),

// 소셜로그인
INVALID_ACCESS_INFO(-900, "프로필을 요청하기위한 액세스 정보가 유효하지 않습니다."),
Expand Down

0 comments on commit 9f57ae4

Please sign in to comment.