Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WHD-290] Fix: Order update with pessimistic lock #61

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public OrderIdResponse registerOrder(Long groupId, CreateOrderRequest request) {
@Transactional
public void confirmOrderPayment(Long groupId, PaymentCompleteReqeust request, LocalDate date) {
Member member = securityUtil.getMember();
Order order = orderRepository.getById(request.orderId());
Order order = orderRepository.getByIdWithLock(request.orderId());

if (order.isOrderComplete()) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package woohakdong.server.domain.order;

import jakarta.persistence.LockModeType;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;

public interface OrderJpaRepository extends JpaRepository<Order, Long> {
boolean existsByOrderMerchantUid(String orderMerchantUid);

Optional<Order> findByOrderMerchantUid(String orderMerchantUid);

@Lock(LockModeType.PESSIMISTIC_WRITE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

락을 응용하여 잘 쓰신거 같습니다.

@Query("SELECT o FROM Order o WHERE o.orderId = :orderId")
Optional<Order> findByOrderIdWithLock(Long orderId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public interface OrderRepository {
Order save(Order order);

Order getById(Long orderId);
Order getByIdWithLock(Long orderId);

boolean existsByOrderMerchantUid(String orderMerchantUid);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public Order save(Order order) {
}

@Override
public Order getById(Long orderId) {
return orderJpaRepository.findById(orderId)
public Order getByIdWithLock(Long orderId) {
return orderJpaRepository.findByOrderIdWithLock(orderId)
.orElseThrow(() -> new CustomException(ORDER_NOT_FOUND));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import woohakdong.server.api.controller.group.dto.CreateOrderRequest;
Expand All @@ -31,7 +30,6 @@
import woohakdong.server.api.controller.group.dto.PortOneWebhookRequest;
import woohakdong.server.SecurityContextSetup;
import woohakdong.server.api.service.bank.MockBankService;
import woohakdong.server.config.WithoutRedisConfig;
import woohakdong.server.domain.admin.adminAccount.AdminAccount;
import woohakdong.server.domain.admin.adminAccount.AdminAccountRepository;
import woohakdong.server.domain.club.Club;
Expand Down Expand Up @@ -103,7 +101,7 @@ void registerOrder() {
OrderIdResponse response = orderService.registerOrder(group.getGroupId(), request);

// Then
Order order = orderRepository.getById(response.orderId());
Order order = orderRepository.getByIdWithLock(response.orderId());
assertThat(order)
.extracting("orderMerchantUid", "orderAmount", "orderStatus", "member.memberId")
.containsExactly("m-12315", 10000, INIT, member.getMemberId());
Expand Down
Loading