fix: lock room row before reordering on problem deletion - #63
Merged
Merged
Conversation
deleteRoomProblem read the room without acquiring a lock, then bulk- updated problemOrder before deleting the target row. Concurrent delete requests on the same room could race and leave problemOrder with gaps or an inconsistent sequence, more likely across multiple instances. Switch to roomRepository.findByIdForUpdate, the same pessimistic-lock pattern already used by createRoomProblemsByAi via RoomProblemPersister#saveGeneratedProblems, so concurrent deletes on the same room are serialized. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
📝 WalkthroughWalkthrough방 문제 삭제 시 일반 방 조회를 Changes방 문제 삭제 동시성 제어
Estimated code review effort: 1 (Trivial) | ~5 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…m-problem creation Both createCategory (name uniqueness) and createRoomProblem (problem order uniqueness) only guarded against duplicates with a pre-check (existsByName / relying on the DB constraint alone). Under concurrent requests the pre-check can pass for both callers, leaving the DB unique constraint as the only real guard, which surfaced as an unmapped DataIntegrityViolationException instead of the intended BusinessException. Wrap the save in saveAndFlush + catch DataIntegrityViolationException, matching the existing isDuplicateEmailViolation pattern in UserServiceImpl: inspect getMostSpecificCause().getMessage() for the constraint name and rethrow as a BusinessException, otherwise propagate. Added RoomProblemErrorCode.DUPLICATE_PROBLEM_ORDER (7005); reused the existing CATEGORY_NAME_DUPLICATED for categories. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
작업 내용
방/카테고리 도메인의 멀티 인스턴스(분산 환경) 대응 점검에서 발견된 동시성 이슈 3건 수정.
변경 사항
deleteRoomProblem이 방을 락 없이 조회(getRoom)한 뒤 순번을 벌크 재정렬하고 삭제해서, 같은 방에서 삭제 요청이 동시에 들어오면problemOrder에 gap이 생기거나 순서가 꼬일 수 있었음. 같은 파일의 AI 채번 로직(createRoomProblemsByAi)이 이미 쓰던roomRepository.findByIdForUpdate비관적 락 패턴을 재사용해 해결.createRoomProblem은 클라이언트가 지정한 순번을 그대로 저장하는데, 동시 요청 시 순번이 겹치면 DB 유니크 제약(UQ_ROOM_PROBLEM_ROOM_ORDER)이 막아주긴 하지만DataIntegrityViolationException이 그대로 노출됐음.saveAndFlush+DataIntegrityViolationExceptioncatch로 감싸서RoomProblemErrorCode.DUPLICATE_PROBLEM_ORDER(신규, 7005)로 매핑.createCategory가existsByName체크 후save하는 check-then-act라, 동시 요청 시 두 요청 모두 체크를 통과하고 DB 제약에서만 걸릴 수 있었음. 동일하게saveAndFlush+ catch로 감싸 기존CATEGORY_NAME_DUPLICATED(3003)로 매핑.UserServiceImpl.isDuplicateEmailViolation에 이미 있던 패턴(제약명 문자열 매칭 후BusinessException변환)을 그대로 재사용.체크리스트
참고 사항
updateCategory(ProblemCategoryServiceImpl.java)도 동일한 TOCTOU 패턴이 남아있으나 이번 스코프에서는 제외함 (별도 확인 필요 시 후속 처리)../gradlew.bat :momogo-core:compileJava빌드 성공 확인 (커밋별로 모두 확인).관련 이슈