重大调整:除了订单更新采用乐观锁更新,其他表是普通更新

This commit is contained in:
Junling Bu
2018-10-26 21:33:04 +08:00
parent e400733864
commit b95e4ca716
30 changed files with 65 additions and 57 deletions

View File

@@ -94,7 +94,7 @@ public class AdminAdController {
return error;
}
if(adService.updateById(ad) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok(ad);

View File

@@ -149,7 +149,7 @@ public class AdminAdminController {
admin.setPassword(encodedPassword);
if(adminService.updateById(admin) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok(admin);

View File

@@ -102,7 +102,7 @@ public class AdminBrandController {
return error;
}
if(brandService.updateById(brand) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok(brand);
}

View File

@@ -107,7 +107,7 @@ public class AdminCategoryController {
}
if(categoryService.updateById(category) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok();
}

View File

@@ -136,7 +136,7 @@ public class AdminGrouponController {
grouponRules.setPicUrl(goods.getPicUrl());
if(rulesService.updateById(grouponRules) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok();

View File

@@ -94,7 +94,7 @@ public class AdminIssueController {
return error;
}
if(issueService.updateById(issue) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok(issue);

View File

@@ -94,7 +94,7 @@ public class AdminKeywordController {
return error;
}
if(keywordService.updateById(keywords) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok(keywords);
}

View File

@@ -127,6 +127,8 @@ public class AdminOrderController {
return ResponseUtil.fail(403, "订单不能确认收货");
}
Integer version = order.getVersion();
// 开启事务管理
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
@@ -134,7 +136,7 @@ public class AdminOrderController {
try {
// 设置订单取消状态
order.setOrderStatus(OrderUtil.STATUS_REFUND_CONFIRM);
if(orderService.updateById(order) == 0) {
if(orderService.updateByIdWithVersion(version, order) == 0) {
throw new Exception("跟新数据已失效");
}
@@ -199,6 +201,8 @@ public class AdminOrderController {
return ResponseUtil.badArgument();
}
Integer version = order.getVersion();
// 如果订单不是已付款状态,则不能发货
if (!order.getOrderStatus().equals(OrderUtil.STATUS_PAY)) {
return ResponseUtil.fail(403, "订单不能确认收货");
@@ -208,7 +212,7 @@ public class AdminOrderController {
order.setShipSn(shipSn);
order.setShipChannel(shipChannel);
order.setShipTime(LocalDateTime.now());
if(orderService.updateById(order) == 0){
if(orderService.updateByIdWithVersion(version, order) == 0){
return ResponseUtil.updatedDateExpired();
}
@@ -243,6 +247,8 @@ public class AdminOrderController {
continue;
}
Integer version = order.getVersion();
// 开启事务管理
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
@@ -251,7 +257,7 @@ public class AdminOrderController {
// 设置订单已取消状态
order.setOrderStatus(OrderUtil.STATUS_AUTO_CANCEL);
order.setEndTime(LocalDateTime.now());
if(orderService.updateById(order) == 0){
if(orderService.updateByIdWithVersion(version, order) == 0){
throw new Exception("跟新数据已失效");
}
@@ -264,12 +270,12 @@ public class AdminOrderController {
Integer number = product.getNumber() + orderGoods.getNumber();
product.setNumber(number);
if(productService.updateById(product) == 0){
throw new Exception("跟新数据已失效");
throw new Exception("跟新数据失败");
}
}
} catch (Exception ex) {
txManager.rollback(status);
logger.info("订单 ID=" + order.getId() + " 数据已经更新,放弃自动确认收货");
logger.info("订单 ID=" + order.getId() + " 数据更新失败,放弃自动确认收货");
return;
}
txManager.commit(status);
@@ -306,10 +312,13 @@ public class AdminOrderController {
if (expired.isAfter(now)) {
continue;
}
Integer version = order.getVersion();
// 设置订单已取消状态
order.setOrderStatus(OrderUtil.STATUS_AUTO_CONFIRM);
order.setConfirmTime(now);
if(orderService.updateById(order) == 0){
if(orderService.updateByIdWithVersion(version, order) == 0){
logger.info("订单 ID=" + order.getId() + " 数据已经更新,放弃自动确认收货");
}
else{

View File

@@ -75,7 +75,7 @@ public class AdminStorageController {
return ResponseUtil.unlogin();
}
if(litemallStorageService.update(litemallStorage) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok(litemallStorage);
}

View File

@@ -99,7 +99,7 @@ public class AdminTopicController {
return error;
}
if(topicService.updateById(topic) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok(topic);
}

View File

@@ -134,7 +134,7 @@ public class AdminUserController {
user.setPassword(encodedPassword);
if(userService.updateById(user) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok(user);
}

View File

@@ -57,7 +57,7 @@ public class LitemallAdService {
}
public int updateById(LitemallAd ad) {
return adMapper.updateWithVersionByPrimaryKeySelective(ad.getVersion(), ad);
return adMapper.updateByPrimaryKeySelective(ad);
}
public void deleteById(Integer id) {

View File

@@ -53,7 +53,7 @@ public class LitemallAdminService {
}
public int updateById(LitemallAdmin admin) {
return adminMapper.updateWithVersionByPrimaryKeySelective(admin.getVersion(), admin);
return adminMapper.updateByPrimaryKeySelective(admin);
}
public void deleteById(Integer id) {

View File

@@ -77,7 +77,7 @@ public class LitemallBrandService {
}
public int updateById(LitemallBrand brand) {
return brandMapper.updateWithVersionByPrimaryKeySelective(brand.getVersion(), brand);
return brandMapper.updateByPrimaryKeySelective(brand);
}
public void deleteById(Integer id) {

View File

@@ -26,7 +26,7 @@ public class LitemallCartService {
}
public int updateById(LitemallCart cart) {
return cartMapper.updateWithVersionByPrimaryKeySelective(cart.getVersion(), cart);
return cartMapper.updateByPrimaryKeySelective(cart);
}
public List<LitemallCart> queryByUid(int userId) {

View File

@@ -87,7 +87,7 @@ public class LitemallCategoryService {
}
public int updateById(LitemallCategory category) {
return categoryMapper.updateWithVersionByPrimaryKeySelective(category.getVersion(), category);
return categoryMapper.updateByPrimaryKeySelective(category);
}
public void deleteById(Integer id) {

View File

@@ -223,7 +223,7 @@ public class LitemallGoodsService {
}
public int updateById(LitemallGoods goods) {
return goodsMapper.updateWithVersionByPrimaryKeySelective(goods.getVersion(), goods);
return goodsMapper.updateByPrimaryKeySelective(goods);
}
public void deleteById(Integer id) {

View File

@@ -108,6 +108,6 @@ public class LitemallGrouponRulesService {
}
public int updateById(LitemallGrouponRules grouponRules) {
return mapper.updateWithVersionByPrimaryKeySelective(grouponRules.getVersion(), grouponRules);
return mapper.updateByPrimaryKeySelective(grouponRules);
}
}

View File

@@ -91,7 +91,7 @@ public class LitemallGrouponService {
}
public int updateById(LitemallGroupon groupon) {
return mapper.updateWithVersionByPrimaryKeySelective(groupon.getVersion(), groupon);
return mapper.updateByPrimaryKeySelective(groupon);
}
/**

View File

@@ -59,7 +59,7 @@ public class LitemallIssueService {
}
public int updateById(LitemallIssue issue) {
return issueMapper.updateWithVersionByPrimaryKeySelective(issue.getVersion(), issue);
return issueMapper.updateByPrimaryKeySelective(issue);
}
public LitemallIssue findById(Integer id) {

View File

@@ -80,7 +80,7 @@ public class LitemallKeywordService {
}
public int updateById(LitemallKeyword keywords) {
return keywordsMapper.updateWithVersionByPrimaryKeySelective(keywords.getVersion(), keywords);
return keywordsMapper.updateByPrimaryKeySelective(keywords);
}
public void deleteById(Integer id) {

View File

@@ -25,12 +25,6 @@ public class LitemallOrderService {
return orderMapper.insertSelective(order);
}
public List<LitemallOrder> query(Integer userId) {
LitemallOrderExample example = new LitemallOrderExample();
example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
return orderMapper.selectByExample(example);
}
public int count(Integer userId) {
LitemallOrderExample example = new LitemallOrderExample();
example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
@@ -52,12 +46,6 @@ public class LitemallOrderService {
return sb.toString();
}
public LitemallOrder queryByOrderSn(Integer userId, String orderSn){
LitemallOrderExample example = new LitemallOrderExample();
example.or().andUserIdEqualTo(userId).andOrderSnEqualTo(orderSn).andDeletedEqualTo(false);
return orderMapper.selectOneByExample(example);
}
public int countByOrderSn(Integer userId, String orderSn){
LitemallOrderExample example = new LitemallOrderExample();
example.or().andUserIdEqualTo(userId).andOrderSnEqualTo(orderSn).andDeletedEqualTo(false);
@@ -136,8 +124,8 @@ public class LitemallOrderService {
return (int)orderMapper.countByExample(example);
}
public int updateById(LitemallOrder order) {
return orderMapper.updateWithVersionByPrimaryKeySelective(order.getVersion(), order);
public int updateByIdWithVersion(Integer version, LitemallOrder order) {
return orderMapper.updateWithVersionByPrimaryKeySelective(version, order);
}
public void deleteById(Integer id) {

View File

@@ -25,7 +25,7 @@ public class LitemallProductService {
}
public int updateById(LitemallProduct product) {
return productMapper.updateWithVersionByPrimaryKeySelective(product.getVersion(), product);
return productMapper.updateByPrimaryKeySelective(product);
}
public void deleteById(Integer id) {

View File

@@ -32,7 +32,7 @@ public class LitemallStorageService {
}
public int update(LitemallStorage storageInfo) {
return storageMapper.updateWithVersionByPrimaryKeySelective(storageInfo.getVersion(), storageInfo);
return storageMapper.updateByPrimaryKeySelective(storageInfo);
}
public LitemallStorage findById(Integer id) {

View File

@@ -94,7 +94,7 @@ public class LitemallTopicService {
public int updateById(LitemallTopic topic) {
LitemallTopicExample example = new LitemallTopicExample();
example.or().andIdEqualTo(topic.getId());
return topicMapper.updateWithVersionByExampleWithBLOBs(topic.getVersion(), topic, example);
return topicMapper.updateByExampleWithBLOBs(topic, example);
}
public void deleteById(Integer id) {

View File

@@ -35,7 +35,7 @@ public class LitemallUserFormIdService {
//更新或者删除缓存
if (userFormid.getIsprepay() && userFormid.getUseamount() > 1) {
userFormid.setUseamount(userFormid.getUseamount() - 1);
return formidMapper.updateWithVersionByPrimaryKey(userFormid.getVersion(), userFormid);
return formidMapper.updateByPrimaryKey(userFormid);
} else {
return formidMapper.deleteByPrimaryKey(userFormid.getId());
}

View File

@@ -40,7 +40,7 @@ public class LitemallUserService {
}
public int updateById(LitemallUser user) {
return userMapper.updateWithVersionByPrimaryKeySelective(user.getVersion(), user);
return userMapper.updateByPrimaryKeySelective(user);
}
public List<LitemallUser> querySelective(String username, String mobile, Integer page, Integer size, String sort, String order) {

View File

@@ -167,7 +167,7 @@ public class WxAuthController {
user.setLastLoginTime(LocalDateTime.now());
user.setLastLoginIp(IpUtil.client(request));
if(userService.updateById(user) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
}
@@ -336,7 +336,7 @@ public class WxAuthController {
user.setPassword(encodedPassword);
if(userService.updateById(user) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok();
@@ -352,7 +352,7 @@ public class WxAuthController {
LitemallUser user = userService.findById(userId);
user.setMobile(phone);
if(userService.updateById(user) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok();
}

View File

@@ -151,7 +151,7 @@ public class WxCartController {
}
existCart.setNumber((short) num);
if(cartService.updateById(existCart) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
}
@@ -224,7 +224,7 @@ public class WxCartController {
}
existCart.setNumber((short) num);
if(cartService.updateById(existCart) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
}
@@ -286,7 +286,7 @@ public class WxCartController {
existCart.setNumber(number.shortValue());
if(cartService.updateById(existCart) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok();
}

View File

@@ -499,6 +499,8 @@ public class WxOrderController {
return ResponseUtil.badArgumentValue();
}
Integer version = order.getVersion();
// 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isCancel()) {
@@ -513,7 +515,7 @@ public class WxOrderController {
// 设置订单已取消状态
order.setOrderStatus(OrderUtil.STATUS_CANCEL);
order.setEndTime(LocalDateTime.now());
if(orderService.updateById(order) == 0){
if(orderService.updateByIdWithVersion(version, order) == 0){
throw new Exception("更新数据已失效");
}
@@ -569,6 +571,8 @@ public class WxOrderController {
return ResponseUtil.badArgumentValue();
}
Integer version = order.getVersion();
// 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isPay()) {
@@ -611,7 +615,7 @@ public class WxOrderController {
return ResponseUtil.fail(403, "订单不能支付");
}
if(orderService.updateById(order) == 0){
if(orderService.updateByIdWithVersion(version, order) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok(result);
@@ -662,6 +666,8 @@ public class WxOrderController {
return WxPayNotifyResponse.fail("订单不存在 sn=" + orderSn);
}
Integer version = order.getVersion();
// 检查这个订单是否已经处理过
if (OrderUtil.isPayStatus(order) && order.getPayId() != null) {
return WxPayNotifyResponse.success("订单已经处理成功!");
@@ -675,18 +681,19 @@ public class WxOrderController {
order.setPayId(payId);
order.setPayTime(LocalDateTime.now());
order.setOrderStatus(OrderUtil.STATUS_PAY);
if (orderService.updateById(order) == 0) {
if (orderService.updateByIdWithVersion(version, order) == 0) {
// 这里可能存在这样一个问题,用户支付和系统自动取消订单发生在同时
// 如果数据库首先因为系统自动取消订单而更新了订单状态;
// 此时用户支付完成回调这里也要更新数据库,而由于乐观锁机制这里的更新会失败
// 因此,这里会重新读取数据库检查状态是否是订单自动取消,如果是则更新成支付状态。
order = orderService.findBySn(orderSn);
version = order.getVersion();
int updated = 0;
if(OrderUtil.isAutoCancelStatus(order)){
order.setPayId(payId);
order.setPayTime(LocalDateTime.now());
order.setOrderStatus(OrderUtil.STATUS_PAY);
updated = orderService.updateById(order);
updated = orderService.updateByIdWithVersion(version, order);
}
// 如果updated是0那么数据库更新失败
@@ -761,6 +768,8 @@ public class WxOrderController {
return ResponseUtil.badArgumentValue();
}
Integer version = order.getVersion();
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isRefund()) {
return ResponseUtil.fail(403, "订单不能取消");
@@ -768,7 +777,7 @@ public class WxOrderController {
// 设置订单申请退款状态
order.setOrderStatus(OrderUtil.STATUS_REFUND);
if(orderService.updateById(order) == 0){
if(orderService.updateByIdWithVersion(version, order) == 0){
return ResponseUtil.updatedDateExpired();
}
@@ -808,6 +817,8 @@ public class WxOrderController {
return ResponseUtil.badArgumentValue();
}
Integer version = order.getVersion();
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isConfirm()) {
return ResponseUtil.fail(403, "订单不能确认收货");
@@ -815,7 +826,7 @@ public class WxOrderController {
order.setOrderStatus(OrderUtil.STATUS_CONFIRM);
order.setConfirmTime(LocalDateTime.now());
if(orderService.updateById(order) == 0){
if(orderService.updateByIdWithVersion(version, order) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok();