Skip to content

Commit

Permalink
Merge pull request #134 from Team-Motivoo/fix/#133-pessimistic_lock_e…
Browse files Browse the repository at this point in the history
…rror

[FIX] UserMission 업데이트 쿼리에서의 비관적 락 문제 해결
  • Loading branch information
jun02160 authored Mar 6, 2024
2 parents 3294423 + abbff53 commit ac04342
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ motivoo-firebase-adminsdk.json
**.http

**.sql
logback-**.xml

local.properties

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public void updateMissionFromEmpty(final Mission mission) {
this.updateCompletedStatus(IN_PROGRESS);
}

public void updateMissionQuest(MissionQuest missionQuest) {
this.missionQuest = missionQuest;
}

public boolean isEmptyUserMission() {
return this.getMission().getTarget().equals(UserType.NONE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface UserMissionRepository extends JpaRepository<UserMission, Long>

//== UPDATE ==//
@Modifying
@Query("UPDATE UserMission um SET um.mission = :mission, um.missionQuest = :quest, um.completedStatus = :status WHERE um.user = :user AND DATE(um.createdAt) = DATE(:date)")
void updateValidTodayMission(Mission mission, MissionQuest quest, CompletedStatus status, User user, LocalDate date);
@Query("UPDATE UserMission um SET um.mission = :mission, um.missionQuest = :quest WHERE um.user = :user AND DATE(um.createdAt) = DATE(:date)")
void updateValidTodayMission(Mission mission, MissionQuest quest, User user, LocalDate date);
}

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void deleteById(Long userMissionId) {

//== UPDATE ==//
public void updateUserMission(User user, Mission mission, MissionQuest quest) {
userMissionRepository.updateValidTodayMission(mission, quest, IN_PROGRESS, user, LocalDate.now());
userMissionRepository.updateValidTodayMission(mission, quest, user, LocalDate.now());
}

//== CREATE ==//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ public List<UserMissionChoices> getFilteredMissionChoices(User user, List<Missio
return missionChoices;
}

public static void setTodayMission(UserMission todayMission, Mission mission, MissionQuest missionQuest) {
todayMission.updateMissionFromEmpty(mission);
todayMission.updateCompletedStatus(CompletedStatus.IN_PROGRESS);
todayMission.updateMissionQuest(missionQuest);
}

@NotNull
public UserMission createEmptyUserMission(User user, LocalDate date, Mission mission, MissionQuest quest) {
UserMission um = UserMission.builderForEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ public Long choiceTodayMission(final TodayMissionChoiceCommand request, final Lo
}

MissionQuest missionQuest = missionQuestRetriever.getRandomMissionQuest();
userMissionRetriever.updateUserMission(user, mission, missionQuest);
// userMissionRetriever.updateUserMission(user, mission, missionQuest);
UserMission todayMission = user.getCurrentUserMission();
setTodayMission(todayMission, mission, missionQuest);
return todayMission.getId();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sopt.org.motivoo.domain.parentchild.entity;

import static sopt.org.motivoo.domain.parentchild.exception.ParentchildExceptionType.*;

import java.util.List;

import jakarta.persistence.Column;
Expand All @@ -12,6 +14,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import sopt.org.motivoo.domain.common.BaseTimeEntity;
import sopt.org.motivoo.domain.parentchild.exception.ParentchildException;
import sopt.org.motivoo.domain.parentchild.exception.ParentchildExceptionType;
import sopt.org.motivoo.domain.user.entity.User;

@Getter
Expand Down Expand Up @@ -43,10 +47,9 @@ public void matchingSuccess() {
public boolean validateParentchild(int userCnt) {

// 부모자식 관계에 대한 예외처리
if (userCnt != 2) {
return false;
if (userCnt >= 3) {
throw new ParentchildException(INVALID_PARENTCHILD_RELATION);
}

return true;
return userCnt == 2 && isMatched;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public enum UserExceptionType implements BusinessExceptionType {
NULL_VALUE_USERTYPE_ENUM(HttpStatus.BAD_REQUEST, "데이터베이스의 유저 타입이 유효하지 않은 값입니다."),
NULL_VALUE_AGE(HttpStatus.BAD_REQUEST, "유저의 나이는 null이어서는 안 됩니다."),
ALREADY_WITHDRAW_USER(HttpStatus.BAD_REQUEST, "이미 탈퇴한 유저입니다."),
ALREADY_WITHDRAW_OPPONENT_USER(HttpStatus.BAD_REQUEST, "상대 유저가 탈퇴하여 요청을 처리할 수 없습니다."),

/**
* 401 Unauthorized
Expand All @@ -36,7 +35,7 @@ public enum UserExceptionType implements BusinessExceptionType {
/**
* 412 Precondition Failed
*/
// ALREADY_WITHDRAW_OPPONENT_USER(HttpStatus.PRECONDITION_FAILED, "상대 유저가 탈퇴하여 요청을 처리할 수 없습니다."),
ALREADY_WITHDRAW_OPPONENT_USER(HttpStatus.PRECONDITION_FAILED, "상대 유저가 탈퇴하여 요청을 처리할 수 없습니다."),

;

Expand Down

0 comments on commit ac04342

Please sign in to comment.