码迷,mamicode.com
首页 > 其他好文 > 详细

重复插入数据导致select one but find two的问题

时间:2015-05-25 00:53:36      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

 1 public Map<String,Object> getDormitoryStorageListByAgentId(int agentId) {
 2         Map<String,Object> resultMap = new HashMap<String, Object>();
 3         //forDormitoryAgent
 4         List<DormitoryStorage> storageList = storageMapper.getDormitoryStorageListByAgentId(agentId);
 5         DormitoryStock stock = stockMapper.getCurrentDormitoryStockByAgentId(agentId);
 6         List<StorageModelForDormitory> storageModelList = new ArrayList<StorageModelForDormitory>();
 7         Iterator<DormitoryStorage> storageIt = storageList.iterator();
 8         while(storageIt.hasNext()) {
 9             StorageModelForDormitory storageModel = new StorageModelForDormitory();
10             DormitoryStorage storage = storageIt.next();
11             storageModel.setStorageId(storage.getId());
12             storageModel.setStoreLeft(storage.getNumber());
13             storageModel.setSnackName(storage.getSnack().getName());
14             
15             int snackPlus = 0;
16             DormitoryStockSnack stockSnack = stockSnackMapper.getStockSnackByStorageId(stock.getId(), storage.getId());
17             if(stockSnack != null) {
18                 snackPlus = stockSnack.getNumber();
19             }
20             storageModel.setSnackPlus(snackPlus);
21             //尝试更新昨日销量
22             Date today = new Date();        //当前日期
23             Date lastStatisticsDay = storage.getStatisticsDay();
24             SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
25             Date yestoday = new Date(new Date().getTime()-24*60*60*1000);
26             Integer oneDaySale = 0;
27             //如果最后购买日期不为空,为空的话
28             if(lastStatisticsDay != null) {
29                 //如果最后购买日期是今天,从统计表拿昨天。
30                 if(dateFormat.format(lastStatisticsDay).equals(dateFormat.format(today))) {
31                     StorageSaleStatistics statistics = new StorageSaleStatistics();
32                     statistics.setDay(yestoday);
33                     statistics.setStorageId(storage.getId());
34                     oneDaySale = statisticsMapper.getStorageOneDaySale(statistics);
35                     if(oneDaySale==null) {
36                         oneDaySale = 0;
37                     }
38                 }else {
39                     //如果最后购买不是今天,加入统计表,获取昨天销量,将最后购买日期(操作日期)设置为今天,购买量为0.
40                     //初始化最后一日销量,某个商品某日的销量是多少
41                     StorageSaleStatistics statistics = new StorageSaleStatistics();
42                     statistics.setStorageId(storage.getId());
43                     statistics.setOneDaySale(storage.getOneDaySale());
44                     statistics.setDay(storage.getStatisticsDay());
45                     //向统计表插入最后一日销量
46                     statisticsMapper.addStorageSaleStatistics(statistics);
47                     if(dateFormat.format(lastStatisticsDay).equals(dateFormat.format(yestoday))) {
48                         oneDaySale = storage.getOneDaySale();
49                     }
50                     //该商品当日当前未被购买过,销量为0
51                     storage.setOneDaySale(0);
52                     //更新统计日期
53                     storage.setStatisticsDay(today);
54                     //保存商品当日销量信息
55                     storageMapper.setStorageSnackOneDaySale(storage);
56                 }
57                 
58             }
59             storageModel.setDaySale(oneDaySale);
60             //设置之前七天销量
61             List<Integer> lastSevenDaySaleList = statisticsMapper.getStorageLastSeventDaySale(storage.getId());
62             int lastSevenDaySale = 0;
63             for(Integer thisOneDaySale : lastSevenDaySaleList) {
64                 if(thisOneDaySale != null) {
65                     lastSevenDaySale = lastSevenDaySale+thisOneDaySale;
66                 }
67             }
68             storageModel.setWeekSale(lastSevenDaySale);
69             storageModelList.add(storageModel);
70         }
71         resultMap.put("storeSnacks", storageModelList);
72         resultMap.put("stockStatus", stock.getStatus());
73         return resultMap;
74     }

这是一个获取库存商品的方法,当用户短时间内在客户端连续点击刷新按钮时,这个方法会被执行两次,从而导致第46行代码被重复执行两次。从而,数据库中有两条一样的记录。当获取该商品的昨日销量时,会出现want to select one but find two的报错。

自己目前写的这个系统内,几乎所有的bug都是这样的原因导致的。

这是一个高并发导致的问题。

逻辑是,每天第一次统计时,要把之前的销售量作为单日销售量保存到统计表。获取商品时对商品销售量进行统计。并发的情况下,会出现,第一次获取商品时发现商品的最后统计日期不是今天,所以会把之前的销量插入到统计表(同时将最后统计日期改成今天),但还未来得及将最后统计日期改成今天时,已经并发执行第二次获取商品的请求了,这是,发现最后统计日期不是今天,所以会把之前的销量再次插入到统计表。那么,在获取该商品的昨日销量时,就会出现select one but find two 。。

重复插入数据导致select one but find two的问题

标签:

原文地址:http://www.cnblogs.com/henrier/p/4526895.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!