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

[feat] 주문 시점에 배타락을 적용해서 문제를 해결 #148

Merged
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 @@ -4,9 +4,12 @@
import java.util.UUID;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;

import camp.woowak.lab.cart.persistence.jpa.entity.CartEntity;
import jakarta.persistence.LockModeType;

public interface CartEntityRepository extends JpaRepository<CartEntity, Long> {
@Lock(LockModeType.PESSIMISTIC_WRITE)
Optional<CartEntity> findByCustomerId(UUID customerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
import org.springframework.context.annotation.Bean;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import camp.woowak.lab.cart.domain.Cart;
import camp.woowak.lab.cart.repository.CartRepository;

@DataJpaTest
@Transactional
class JpaCartRepositoryTest {
@Autowired
private TransactionTemplate transactionTemplate;

@Autowired
private CartRepository cartRepository;
@Autowired
Expand Down Expand Up @@ -51,7 +55,8 @@ class FindByCustomerIdIs {
@Test
@DisplayName("저장된 CartEntity가 있는 경우")
void returnOptionalWhenCartEntityIsFound() {
Optional<Cart> findCart = cartRepository.findByCustomerId(fakeCustomerId.toString());
Optional<Cart> findCart = transactionTemplate.execute(
(status) -> cartRepository.findByCustomerId(fakeCustomerId.toString()));
assertThat(findCart.isPresent()).isTrue();
assertThat(findCart.get().getCustomerId()).isEqualTo(fakeCustomerId.toString());
}
Expand All @@ -60,7 +65,8 @@ void returnOptionalWhenCartEntityIsFound() {
@DisplayName("저장된 CartEntity가 없는 경우")
void returnEmptyOptionalWhenCartEntityIsNotFound() {
cartEntityRepository.deleteAll();
Optional<Cart> findCart = cartRepository.findByCustomerId(fakeCustomerId.toString());
Optional<Cart> findCart = transactionTemplate.execute(
(status) -> cartRepository.findByCustomerId(fakeCustomerId.toString()));
assertThat(findCart.isPresent()).isFalse();
}
}
Expand All @@ -84,4 +90,4 @@ void success() {
assertThat(save.getCustomerId()).isEqualTo(newFakeCustomerId.toString());
}
}
}
}