Browse Source

处理批量导入会因为锁报错的问题。

yuj 7 years ago
parent
commit
66f7dcbb18

+ 2 - 37
src/main/java/com/uas/platform/b2c/prod/commodity/service/impl/ReleaseProductByBatchServiceImpl.java

@@ -1887,7 +1887,6 @@ public class ReleaseProductByBatchServiceImpl implements ReleaseProductByBatchSe
 	 */
 	public Integer releaseToGoods(List<ReleaseProductByBatch> list, Integer ignoreImport, Set<Long> idSet) {
 		if (CollectionUtils.isNotEmpty(list)) {
-			List<Goods> goodses = new ArrayList<>();
 			Goods goods = null;
 			TradeDeliveryDelayTime delayTime = getDelayTime(list.get(0));
 			List<Goods> goodlist = null;
@@ -1927,19 +1926,16 @@ public class ReleaseProductByBatchServiceImpl implements ReleaseProductByBatchSe
 					goods = new Goods();
 					goods.setGoodsByReleaseProductByBatch(releaseProductByBatch, delayTime);
 					goodsHistoryList.add(goodsHistoryService.converTGoodsHist(goods, GoodsHistory.OperateType.Publish.getPhrase(), false));
-					goodses.add(goods);
+					updateGoods.add(goods);
 				}
 			}
-			if (CollectionUtils.isNotEmpty(goodses)) {
-				commonDao.save(goodses, Goods.class);
-			}
 			if (CollectionUtils.isNotEmpty(updateGoods)) {
                 goodsDao.save(updateGoods);
             }
             if (CollectionUtils.isNotEmpty(goodsHistoryList)) {
 				goodsHistoryService.save(goodsHistoryList);
 			}
-			return (goodses.size() + updateGoods.size());
+			return updateGoods.size();
 		} else {
 			return 0;
 		}
@@ -2178,41 +2174,10 @@ public class ReleaseProductByBatchServiceImpl implements ReleaseProductByBatchSe
         final Runnable afterPublishToGoodsRunnable = new Runnable() {
 			@Override
 			public void run() {
-
-				//创建历史库存
-//				List<Goods> goodses1 = goodsService.findByBatchId(batchid);
-//				List<GoodsHistory> list = new ArrayList<>();
-//				for (Goods goods : goodses1) {
-//					GoodsHistory goodsHistory = goodsHistoryService.converTGoodsHist(goods, GoodsHistory.OperateType.Publish.getPhrase(), false);
-//					list.add(goodsHistory);
-//				}
-//				if (CollectionUtils.isNotEmpty(list)) {
-//					goodsHistoryService.save(list);
-//				}
 				//创建物料的私有信息
 				productPrivateService.newProductPrivateIfNotExist(productIds);
 				//添加个人关系
                 restTemplate.postForEntity(productServiceIp + "/product/assign/batch?userUU=" + userUU, productIds, String.class).getBody();
-
-//				if ((reIds != null) && (reIds.size() != 0)) {
-//					List<ReleaseProductByBatch> productByBatches = releaseProductByBatchDao.findAll(reIds);
-//					List<Long> prIds = new ArrayList<>();
-//					for (ReleaseProductByBatch productByBatch : productByBatches) {
-//						if (productByBatch.getProductid() != null) {
-//							prIds.add(productByBatch.getProductid());
-//						}
-//					}
-//					List<Product> products = productService.findByProductId(prIds);
-//					for (Product product : products) {
-//						for (ReleaseProductByBatch productByBatch : productByBatches) {
-//							if ((productByBatch.getProductid() != null) && (product.getId().longValue() == productByBatch.getProductid().longValue())) {
-//								product.setSpec(productByBatch.getSpec());
-//							}
-//						}
-//					}
-//					List<Goods> goodses = goodsDao.findByProductIds(prIds);
-//					goodsService.updateSpecByProducts(products, goodses);
-//				}
             }
 		};
 		try {

+ 11 - 0
src/test/java/com/uas/platform/b2c/Test.java

@@ -39,4 +39,15 @@ public class Test {
         UUID uuid = UUID.randomUUID();
         System.out.println(uuid);
     }
+
+    @org.junit.Test
+    public void testCopy() {
+        List<Integer> list = new ArrayList<>();
+        list.add(1);
+        list.add(2);
+        list.add(3);
+        Integer[] list2 = new Integer[list.size()];
+        System.arraycopy(list.toArray(), 0, list2, 0, list.size());
+        System.out.println(list2);
+    }
 }

+ 52 - 0
src/test/java/com/uas/platform/b2c/common/base/LockTest.java

@@ -0,0 +1,52 @@
+package com.uas.platform.b2c.common.base;
+
+import com.uas.platform.b2c.BaseJunitTest;
+import com.uas.platform.b2c.common.base.dao.CommonDao;
+import com.uas.platform.b2c.prod.commodity.dao.GoodsDao;
+import com.uas.platform.b2c.prod.commodity.model.Goods;
+import com.uas.platform.b2c.prod.product.common.service.CreateNumberService;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * @author yuj
+ * @date 2018/10/30 21:08
+ */
+public class LockTest extends BaseJunitTest {
+
+    @Autowired
+    private GoodsDao goodsDao;
+
+    @Autowired
+    private CommonDao commonDao;
+
+    @Autowired
+    private CreateNumberService createNumberService;
+
+    /**
+     * 通过commmonDao批量插入信息和goodsDao批量更新信息,查看是否能导致死锁
+     */
+    @Test
+    public void testSaveLock() {
+        // 2811条数据
+        List<Goods> goods = goodsDao.findByEnUU(10043516l);
+        Goods[] goodsList = new Goods[goods.size()];
+        System.arraycopy(goods.toArray(), 0 , goodsList, 0, goods.size());
+        for (Goods good : goods) {
+            String code = createNumberService.getTimeNumber("trade$goods", 8);
+            good.setBatchCode(code);
+            good.setSampleAppliedQty(1d);
+        }
+        for (Goods goods1 : goodsList) {
+            String code = createNumberService.getTimeNumber("trade$goods", 8);
+            goods1.setBatchCode(code);
+            goods1.setId(null);
+        }
+        List<Goods> list = Arrays.asList(goodsList);
+        commonDao.save(list, Goods.class);
+        goodsDao.save(goods);
+    }
+}