|
1 | 1 | package org.dfbf.soundlink.domain.emotionRecord.service; |
2 | 2 |
|
| 3 | +import jakarta.persistence.OptimisticLockException; |
3 | 4 | import lombok.RequiredArgsConstructor; |
4 | 5 | import lombok.extern.slf4j.Slf4j; |
5 | 6 | import org.dfbf.soundlink.domain.chat.entity.ChatRoom; |
|
24 | 25 | import org.springframework.data.domain.PageRequest; |
25 | 26 | import org.springframework.data.domain.Pageable; |
26 | 27 | import org.springframework.data.domain.Sort; |
| 28 | +import org.springframework.retry.annotation.Backoff; |
| 29 | +import org.springframework.retry.annotation.Recover; |
| 30 | +import org.springframework.retry.annotation.Retryable; |
27 | 31 | import org.springframework.stereotype.Service; |
28 | 32 | import org.springframework.transaction.annotation.Transactional; |
29 | 33 |
|
@@ -181,6 +185,11 @@ public ResponseResult getVideoIdBySpotifyId(String spotifyId) { |
181 | 185 | } |
182 | 186 |
|
183 | 187 | @Transactional |
| 188 | + @Retryable( |
| 189 | + retryFor = OptimisticLockException.class, // 낙관적 락 충돌 시 재시도 |
| 190 | + maxAttempts = 3, // 최대 3번 재시도 |
| 191 | + backoff = @Backoff(delay = 100) // 100ms(0.1초) 대기 후 재시도 |
| 192 | + ) |
184 | 193 | public ResponseResult updateEmotionRecord(Long recordId, EmotionRecordUpdateRequestDTO updateDTO) { |
185 | 194 | try { |
186 | 195 | // 기존 감정 기록 조회 |
@@ -223,6 +232,16 @@ public ResponseResult updateEmotionRecord(Long recordId, EmotionRecordUpdateRequ |
223 | 232 | } |
224 | 233 | } |
225 | 234 |
|
| 235 | + // 낙관적 락 재시도 실패 시 실행되는 메서드 |
| 236 | + // Recover 어노테이션에 의해 실패 시, 자동 호출됨 |
| 237 | + @Recover |
| 238 | + public ResponseResult recoverFromOptimisticLock(OptimisticLockException e, Long recordId, EmotionRecordUpdateRequestDTO updateDTO) { |
| 239 | + log.error("감정 기록 업데이트 중 동시성 충돌 발생. recordId: {}, spotifyId: {}, error: {}", |
| 240 | + recordId, updateDTO.spotifyId(), e.getMessage()); |
| 241 | + |
| 242 | + return new ResponseResult(ErrorCode.CONCURRENCY_ERROR, e.getMessage()); |
| 243 | + } |
| 244 | + |
226 | 245 | @Transactional |
227 | 246 | public ResponseResult deleteEmotionRecord(Long recordId) { |
228 | 247 | try { |
|
0 commit comments