fix - 事务&锁机制优化,防止重复扣费情况。
This commit is contained in:
@@ -24,7 +24,11 @@ import org.apache.commons.lang3.time.DateFormatUtils;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
import org.springframework.transaction.TransactionDefinition;
|
||||||
|
import org.springframework.transaction.TransactionStatus;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@@ -69,6 +73,9 @@ public class OrderService extends BaseServiceImpl {
|
|||||||
@Resource
|
@Resource
|
||||||
private MemberKeyLock memberKeyLock;
|
private MemberKeyLock memberKeyLock;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private PlatformTransactionManager transactionManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建订单
|
* 创建订单
|
||||||
*
|
*
|
||||||
@@ -314,7 +321,14 @@ public class OrderService extends BaseServiceImpl {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Order order = new Order();
|
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||||
|
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||||||
|
TransactionStatus txStatus = transactionManager.getTransaction(def);
|
||||||
|
Order order;
|
||||||
|
try {
|
||||||
|
order = orderMapper.findPendingBasketballOrder(memberId, venueId);
|
||||||
|
if (order == null) {
|
||||||
|
order = new Order();
|
||||||
order.setOrderSn(createSn());
|
order.setOrderSn(createSn());
|
||||||
order.setPayType(payTypeEnum.value);
|
order.setPayType(payTypeEnum.value);
|
||||||
order.setPrice(price);
|
order.setPrice(price);
|
||||||
@@ -325,6 +339,12 @@ public class OrderService extends BaseServiceImpl {
|
|||||||
order.setParentOrderId(venueId);
|
order.setParentOrderId(venueId);
|
||||||
order.setCreatedId(enterFlag);
|
order.setCreatedId(enterFlag);
|
||||||
orderMapper.insert(order);
|
orderMapper.insert(order);
|
||||||
|
}
|
||||||
|
transactionManager.commit(txStatus);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
transactionManager.rollback(txStatus);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
if (EnterEnum.OUT.value == enterFlag) {
|
if (EnterEnum.OUT.value == enterFlag) {
|
||||||
BarcodeOrderTime lastOrder = barcodeOrderTimeMapper.findLastOrder(memberId, venueId);
|
BarcodeOrderTime lastOrder = barcodeOrderTimeMapper.findLastOrder(memberId, venueId);
|
||||||
|
|||||||
Reference in New Issue
Block a user