Skip to content

Commit bce94e3

Browse files
committed
refactor:동기화를 위해 transactionTemplate 사용
1 parent b641b5c commit bce94e3

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

  • apps/commerce-api/src/main/java/com/loopers/application/like

apps/commerce-api/src/main/java/com/loopers/application/like/LikeFacade.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,26 @@ public class LikeFacade {
2323
public LikeInfo like(long userId, long productId) {
2424
Optional<Like> existingLike = likeService.findLike(userId, productId);
2525

26-
2726
if (existingLike.isPresent()) {
2827
Product product = productService.getProduct(productId);
2928
return LikeInfo.from(existingLike.get(), product.getLikeCount());
3029
}
3130

3231
for (int i = 0; i < RETRY_COUNT; i++) {
3332
try {
33+
return transactionTemplate.execute(status -> {
34+
Like newLike = likeService.save(userId, productId);
35+
int updatedLikeCount = productService.increaseLikeCount(productId);
36+
return LikeInfo.from(newLike, updatedLikeCount);
37+
});
3438

35-
Like newLike = likeService.save(userId, productId);
36-
int updatedLikeCount = productService.increaseLikeCount(productId);
37-
38-
return LikeInfo.from(newLike, updatedLikeCount);
3939
} catch (ObjectOptimisticLockingFailureException e) {
4040
if (i == RETRY_COUNT - 1) {
4141
throw e;
4242
}
4343
sleep(50);
4444
}
4545
}
46-
4746
throw new IllegalStateException("좋아요 처리 재시도 횟수를 초과했습니다.");
4847
}
4948

0 commit comments

Comments
 (0)