Skip to content

Commit

Permalink
feat: 주문, 담기 시 낙관적 락 동시성 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEJaeHyeok97 committed Dec 7, 2024
1 parent 46fb526 commit eb3bb85
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.jangburich.domain.point.domain.PointTransaction;
import com.jangburich.domain.point.domain.TransactionType;
import com.jangburich.domain.point.domain.repository.PointTransactionRepository;
import jakarta.persistence.OptimisticLockException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -71,7 +72,11 @@ public Message addCart(String userProviderId, AddCartRequest addCartRequest) {

if (optionalCart.isPresent()) {
Cart existingCart = optionalCart.get();
existingCart.updateQuantity(existingCart.getQuantity() + addCartRequest.quantity());
try {
existingCart.updateQuantity(existingCart.getQuantity() + addCartRequest.quantity());
} catch (OptimisticLockException e) {
throw new IllegalStateException("중복 요청입니다. 이전 요청이 처리 중입니다.");
}

return Message.builder()
.message("장바구니에 상품을 추가했습니다.")
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/jangburich/domain/order/domain/Cart.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Version;
import java.util.List;
import lombok.AccessLevel;
import lombok.Builder;
Expand Down Expand Up @@ -48,6 +49,9 @@ public class Cart extends BaseEntity {
@JoinColumn(name = "store_id")
private Store store;

@Version
private Long version;


@Builder
public Cart(Integer quantity, Orders orders, Menu menu, User user, Store store) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/jangburich/global/error/ApiControllerAdvice.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.jangburich.global.payload.ApiResponse;
import com.jangburich.global.payload.ErrorCode;
import com.jangburich.global.payload.ErrorResponse;
import jakarta.persistence.OptimisticLockException;
import javax.security.sasl.AuthenticationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -15,6 +16,18 @@
@RestControllerAdvice(annotations = RestController.class)
public class ApiControllerAdvice {

@ExceptionHandler(OptimisticLockException.class)
public ResponseEntity<ApiResponse> handleOptimisticLockException(OptimisticLockException e) {
final ErrorResponse response = ErrorResponse
.builder()
.status(HttpStatus.CONFLICT.value())
.code("OPTIMISTIC_LOCK")
.message("동시 처리 중 충돌이 발생했습니다. 다시 시도해주세요.")
.build();
ApiResponse apiResponse = ApiResponse.builder().check(false).information(response).build();
return new ResponseEntity<>(apiResponse, HttpStatus.CONFLICT);
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
protected ResponseEntity<?> handleHttpRequestMethodNotSupportedException(
HttpRequestMethodNotSupportedException e) {
Expand Down

0 comments on commit eb3bb85

Please sign in to comment.