Skip to content

Commit

Permalink
refactor: 낙관적 락에서 데드락을 잡는 예외 추가
Browse files Browse the repository at this point in the history
CannotAcquireLockException 추가
  • Loading branch information
jemin committed Dec 27, 2023
1 parent b2b3623 commit 216f3aa
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface OptimisticLock {

int retryCount() default 3;
int retryCount() default 5;

long waitTime() default 1L;
long waitTime() default 500L;

boolean retry() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
import static cmc.mellyserver.common.aspect.lock.LockMessage.*;

import java.lang.reflect.Method;
import java.sql.SQLTransactionRollbackException;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.stereotype.Component;

import cmc.mellyserver.support.exception.BusinessException;
import cmc.mellyserver.support.exception.ErrorCode;
import jakarta.persistence.OptimisticLockException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -53,7 +52,7 @@ public Object lock(final ProceedingJoinPoint joinPoint) throws Throwable {
try {
return joinPoint.proceed();

} catch (OptimisticLockingFailureException | OptimisticLockException | SQLTransactionRollbackException ex) {
} catch (OptimisticLockingFailureException | CannotAcquireLockException ex) {
log.info(OPTIMISTIC_LOCK_RETRY);
Thread.sleep(waitTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public Long saveGroup(Long userId, CreateGroupRequestDto createGroupRequestDto)
return groupAndUserWriter.save(GroupAndUser.of(user.getId(), savedGroup)).getId();
}

// @DistributedLock(key = "#groupId")
@OptimisticLock(retryCount = 3, waitTime = 1000L) // 분산락을 획득한 트랜잭션이 커밋 전에 락을 풀어버리는 상황에 대비
@OptimisticLock
@CacheEvict(cacheNames = CacheNames.GROUP, key = "#groupId")
@Transactional
public void joinGroup(final Long userId, final Long groupId) {
Expand Down
1 change: 0 additions & 1 deletion support/monitoring/src/main/resources/monitoring-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ management.endpoints.web.exposure.include: '*'

management.endpoint.health.show-details: always
management.endpoint.prometheus.enabled: true
management.server.port: 9091
management.health.diskspace.enabled: false
management.health.circuitbreakers.enabled: true
management.health.redis.enabled: false
Expand Down

0 comments on commit 216f3aa

Please sign in to comment.