Skip to content

Commit 606c919

Browse files
authored
Merge pull request #174 from kt-cloud-basic-project/SHOP-136
feat: Optimistic Lock ๊ตฌํ˜„
2 parents 8167ec4 + 66768bf commit 606c919

4 files changed

Lines changed: 17 additions & 6 deletions

File tree

โ€Žsrc/main/java/com/kt/common/exception/ErrorCode.javaโ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public enum ErrorCode {
5050
CANNOT_REFUND_ORDER(HttpStatus.BAD_REQUEST, "์ฃผ๋ฌธ ํ™˜๋ถˆ์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."),
5151
CANNOT_RETURN_ORDER(HttpStatus.BAD_REQUEST, "์ฃผ๋ฌธ ๋ฐ˜ํ’ˆ์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."),
5252
CANNOT_UPDATE_ORDER_STATUS(HttpStatus.BAD_REQUEST, "์ฃผ๋ฌธ ์ƒํƒœ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."),
53+
CONCURRENT_RESERVATION(HttpStatus.CONFLICT, "๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”"),
54+
5355

5456
//shoppingaddress
5557
NOT_SHOPPING_ADDRESS_OWNER(HttpStatus.FORBIDDEN, "๋ณธ์ธ์˜ ๋ฐฐ์†ก์ง€๋งŒ ์ˆ˜์ •/์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."),

โ€Žsrc/main/java/com/kt/controller/payment/PaymentController.javaโ€Ž

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ public ResponseEntity<?> confirmPayment(@RequestBody PaymentTossConfirmRequest r
134134
return ResponseEntity.ok(responseBody);
135135

136136
} catch (Exception e) {
137-
System.out.println("๊ฒฐ์ œ ์‹คํŒจsdlkjfnmalskdmfklamsdklfmaklsdmfklamsdflkmasdklf");
138-
139137
cancelTossPayment(paymentKey, "์‹œ์Šคํ…œ ์˜ค๋ฅ˜๋กœ ์ธํ•œ ์ž๋™ ์ทจ์†Œ");
140138

141139
orderService.rollback(orderIdLong);
@@ -242,7 +240,7 @@ public ApiResult<Map<String, String>> getClientKey() {
242240
return ApiResult.ok(Map.of("clientKey", tossPaymentsProperties.getClientKey()));
243241
}
244242

245-
/* ์ด ์ฝ”๋“œ service๋กœ ๋นผ์•ผํ•˜๋Š”์ง€ ๋“ฑ ๊ฒ€ํ† ์ข€ ํ•œ๋ฒˆ ใ…‚๋ถ€ํƒ๋“œ๋ฆด๊ฒŒ์š”!*/
243+
246244
private void cancelTossPayment(String paymentKey, String cancelReason) {
247245
String url = tossPaymentsProperties.getApiUrl() + "/" + paymentKey + "/cancel";
248246
String auth = tossPaymentsProperties.getSecretKey() + ":";

โ€Žsrc/main/java/com/kt/domain/product/Product.javaโ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import jakarta.persistence.JoinColumn;
1515
import jakarta.persistence.ManyToOne;
1616
import jakarta.persistence.OneToMany;
17+
import jakarta.persistence.Version;
1718
import lombok.Getter;
1819
import lombok.NoArgsConstructor;
1920

@@ -34,6 +35,9 @@ public class Product extends BaseEntity {
3435

3536
private boolean deleted;
3637

38+
@Version
39+
private Long version;
40+
3741
@ManyToOne
3842
@JoinColumn(name = "category_id")
3943
private Category category;

โ€Žsrc/main/java/com/kt/service/order/OrderService.javaโ€Ž

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Objects;
99

1010
import org.springframework.data.domain.Page;
11+
import org.springframework.orm.ObjectOptimisticLockingFailureException;
1112
import org.springframework.stereotype.Service;
1213
import org.springframework.transaction.annotation.Propagation;
1314
import org.springframework.transaction.annotation.Transactional;
@@ -124,10 +125,16 @@ public void save(Long orderId){
124125
var order = orderRepository.findByIdOrThrow(orderId, ErrorCode.NOT_FOUND_ORDER);
125126

126127
// stock ์ฐจ๊ฐ
127-
order.getOrderProducts().forEach(newProduct -> {
128+
for (OrderProduct newProduct : order.getOrderProducts()) {
128129
var product = newProduct.getProduct();
129-
product.updateStock(product.getStock() - newProduct.getCount());
130-
});
130+
131+
try {
132+
product.updateStock(product.getStock() - newProduct.getCount());
133+
productRepository.saveAndFlush(product); // ๋ฒ„์ „ ์ฒดํฌ
134+
} catch (ObjectOptimisticLockingFailureException e) {
135+
throw new CustomException(ErrorCode.CONCURRENT_RESERVATION);
136+
}
137+
}
131138

132139
order.updateStatus(OrderStatus.PAID);
133140
}

0 commit comments

Comments
ย (0)