#27 stock: race condition solution 2 - db(named lock)
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package com.example.stock.facade;
|
||||
|
||||
import com.example.stock.repository.LockRepository;
|
||||
import com.example.stock.service.StockService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class NamedLockStockFacade {
|
||||
|
||||
private final LockRepository lockRepository;
|
||||
|
||||
private final StockService stockService;
|
||||
|
||||
@Transactional
|
||||
public void decrease(Long id, Long quantity) {
|
||||
try {
|
||||
lockRepository.getLock(id.toString());
|
||||
stockService.decreaseNewTransaction(id, quantity);
|
||||
} finally {
|
||||
lockRepository.releaseLock(id.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.stock.repository;
|
||||
|
||||
import com.example.stock.domain.Stock;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
public interface LockRepository extends JpaRepository<Stock, Long> {
|
||||
|
||||
@Query(value = "select get_lock(:key, 3000)", nativeQuery = true)
|
||||
void getLock(String key);
|
||||
|
||||
@Query(value = "select release_lock(:key)", nativeQuery = true)
|
||||
void releaseLock(String key);
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import com.example.stock.domain.Stock;
|
||||
import com.example.stock.repository.StockRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@@ -23,4 +24,16 @@ public class StockService {
|
||||
// 저장
|
||||
stockRepository.saveAndFlush(stock);
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
public void decreaseNewTransaction(Long id, Long quantity) {
|
||||
// 재고 조회
|
||||
Stock stock = stockRepository.findById(id).orElseThrow();
|
||||
|
||||
// 재고 감소
|
||||
stock.decrease(quantity);
|
||||
|
||||
// 저장
|
||||
stockRepository.saveAndFlush(stock);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ spring:
|
||||
url: jdbc:mysql://127.0.0.1:13306/stock_example
|
||||
username: root
|
||||
password: 1234
|
||||
hikari:
|
||||
maximum-pool-size: 40
|
||||
|
||||
logging:
|
||||
level:
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.example.stock.facade;
|
||||
|
||||
import com.example.stock.domain.Stock;
|
||||
import com.example.stock.repository.StockRepository;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@SpringBootTest
|
||||
class NamedLockStockFacadeTest {
|
||||
@Autowired
|
||||
private NamedLockStockFacade namedLockStockFacade;
|
||||
|
||||
@Autowired
|
||||
private StockRepository stockRepository;
|
||||
|
||||
@BeforeEach
|
||||
public void before() {
|
||||
Stock stock = new Stock(1L, 100L);
|
||||
|
||||
stockRepository.saveAndFlush(stock);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void after() {
|
||||
stockRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void 동시에_100개의_요청_named_lock() throws InterruptedException {
|
||||
int threadCount = 100;
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(32);
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(threadCount);
|
||||
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
executorService.submit(() -> {
|
||||
try {
|
||||
namedLockStockFacade.decrease(1L, 1L);
|
||||
} finally {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
latch.await();
|
||||
|
||||
Stock stock = stockRepository.findById(1L).orElseThrow();
|
||||
|
||||
// 100 - (1 * 100) = 0
|
||||
assertEquals(0L, stock.getQuantity());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user