From 3b343804477fad57224da6164e2ce95efcf31f64 Mon Sep 17 00:00:00 2001 From: donghar Date: Sat, 24 Aug 2024 15:05:03 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[feat]=20=EC=B9=B4=ED=8A=B8=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=EC=8B=9C=20X-lock=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cart/persistence/jpa/repository/CartEntityRepository.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/camp/woowak/lab/cart/persistence/jpa/repository/CartEntityRepository.java b/src/main/java/camp/woowak/lab/cart/persistence/jpa/repository/CartEntityRepository.java index ca0177d4..5a20c6df 100644 --- a/src/main/java/camp/woowak/lab/cart/persistence/jpa/repository/CartEntityRepository.java +++ b/src/main/java/camp/woowak/lab/cart/persistence/jpa/repository/CartEntityRepository.java @@ -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 { + @Lock(LockModeType.PESSIMISTIC_WRITE) Optional findByCustomerId(UUID customerId); } From f1f74d1a7a6ad8d88ecc58e0853e3cd1952883e4 Mon Sep 17 00:00:00 2001 From: donghar Date: Sun, 25 Aug 2024 16:00:07 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[test]=20TransactionTemplate=EB=A5=BC=20?= =?UTF-8?q?=ED=86=B5=ED=95=B4=EC=84=9C=20Cart=EB=A5=BC=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jpa/repository/JpaCartRepositoryTest.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/test/java/camp/woowak/lab/cart/persistence/jpa/repository/JpaCartRepositoryTest.java b/src/test/java/camp/woowak/lab/cart/persistence/jpa/repository/JpaCartRepositoryTest.java index 7aa600a4..615240f8 100644 --- a/src/test/java/camp/woowak/lab/cart/persistence/jpa/repository/JpaCartRepositoryTest.java +++ b/src/test/java/camp/woowak/lab/cart/persistence/jpa/repository/JpaCartRepositoryTest.java @@ -17,6 +17,7 @@ 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; @@ -24,6 +25,9 @@ @DataJpaTest @Transactional class JpaCartRepositoryTest { + @Autowired + private TransactionTemplate transactionTemplate; + @Autowired private CartRepository cartRepository; @Autowired @@ -51,7 +55,8 @@ class FindByCustomerIdIs { @Test @DisplayName("저장된 CartEntity가 있는 경우") void returnOptionalWhenCartEntityIsFound() { - Optional findCart = cartRepository.findByCustomerId(fakeCustomerId.toString()); + Optional findCart = transactionTemplate.execute( + (status) -> cartRepository.findByCustomerId(fakeCustomerId.toString())); assertThat(findCart.isPresent()).isTrue(); assertThat(findCart.get().getCustomerId()).isEqualTo(fakeCustomerId.toString()); } @@ -60,7 +65,8 @@ void returnOptionalWhenCartEntityIsFound() { @DisplayName("저장된 CartEntity가 없는 경우") void returnEmptyOptionalWhenCartEntityIsNotFound() { cartEntityRepository.deleteAll(); - Optional findCart = cartRepository.findByCustomerId(fakeCustomerId.toString()); + Optional findCart = transactionTemplate.execute( + (status) -> cartRepository.findByCustomerId(fakeCustomerId.toString())); assertThat(findCart.isPresent()).isFalse(); } } @@ -84,4 +90,4 @@ void success() { assertThat(save.getCustomerId()).isEqualTo(newFakeCustomerId.toString()); } } -} \ No newline at end of file +}