File tree Expand file tree Collapse file tree
apps/commerce-api/src/main/java/com/loopers/application/like Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88import lombok .RequiredArgsConstructor ;
99import org .springframework .orm .ObjectOptimisticLockingFailureException ;
1010import org .springframework .stereotype .Component ;
11+ import org .springframework .transaction .support .TransactionTemplate ;
1112
1213@ RequiredArgsConstructor
1314@ Component
1415public class LikeFacade {
1516
1617 private final ProductService productService ;
1718 private final LikeService likeService ;
19+ private final TransactionTemplate transactionTemplate ;
1820
1921 private static final int RETRY_COUNT = 30 ;
2022
@@ -46,20 +48,25 @@ public LikeInfo like(long userId, long productId) {
4648 }
4749
4850 public int unLike (long userId , long productId ) {
49-
5051 for (int i = 0 ; i < RETRY_COUNT ; i ++) {
5152 try {
52- likeService .unLike (userId , productId );
5353
54- return productService .decreaseLikeCount (productId );
54+ return transactionTemplate .execute (status -> {
55+
56+ likeService .unLike (userId , productId );
57+
58+ return productService .decreaseLikeCount (productId );
59+
60+ });
61+
5562 } catch (ObjectOptimisticLockingFailureException e ) {
63+
5664 if (i == RETRY_COUNT - 1 ) {
5765 throw e ;
5866 }
5967 sleep (50 );
6068 }
6169 }
62-
6370 throw new IllegalStateException ("싫어요 처리 재시도 횟수를 초과했습니다." );
6471 }
6572
You can’t perform that action at this time.
0 commit comments