Skip to content

Commit

Permalink
refactor: 골룸 참여 시 발생하는 동시성 이슈를 비관적 락으로 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
miseongk committed Nov 17, 2023
1 parent 359926a commit a241b85
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

public interface GoalRoomQueryRepository {

Optional<GoalRoom> findGoalRoomByIdWithPessimisticLock(Long goalRoomId);

Optional<GoalRoom> findByIdWithRoadmapContent(final Long goalRoomId);

Optional<GoalRoom> findByIdWithContentAndTodos(final Long goalRoomId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import co.kirikiri.persistence.goalroom.dto.RoadmapGoalRoomsOrderType;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import jakarta.persistence.LockModeType;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
Expand All @@ -27,6 +28,16 @@ public GoalRoomQueryRepositoryImpl() {
super(GoalRoom.class);
}

@Override
public Optional<GoalRoom> findGoalRoomByIdWithPessimisticLock(final Long goalRoomId) {
return Optional.ofNullable(selectFrom(goalRoom)
.innerJoin(goalRoom.goalRoomPendingMembers.values, goalRoomPendingMember)
.fetchJoin()
.where(goalRoom.id.eq(goalRoomId))
.setLockMode(LockModeType.PESSIMISTIC_WRITE)
.fetchOne());
}

@Override
public Optional<GoalRoom> findByIdWithRoadmapContent(final Long goalRoomId) {
return Optional.ofNullable(selectFrom(goalRoom)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package co.kirikiri.persistence.goalroom;

import co.kirikiri.domain.goalroom.GoalRoom;
import org.springframework.data.jpa.repository.JpaRepository;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface GoalRoomRepository extends JpaRepository<GoalRoom, Long>, GoalRoomQueryRepository {

@Override

Optional<GoalRoom> findById(final Long goalRoomId);

List<GoalRoom> findAllByEndDate(final LocalDate endDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void join(final String identifier, final Long goalRoomId) {
}

private GoalRoom findGoalRoomById(final Long goalRoomId) {
return goalRoomRepository.findById(goalRoomId)
return goalRoomRepository.findGoalRoomByIdWithPessimisticLock(goalRoomId)
.orElseThrow(() -> new NotFoundException("존재하지 않는 골룸입니다. goalRoomId = " + goalRoomId));
}

Expand Down

0 comments on commit a241b85

Please sign in to comment.