private void DeEnqueueRedis() { try { //连接字符串 string stq = "Server=47.92.198.126;Database=ESHOP;user id=eshop;password=eshop123456;MultipleActiveResultSets=true"; var optionsBuilder = new DbContextOptionsBuilder <ESHOPContext>(); optionsBuilder.UseSqlServer(stq); var db = new ESHOPContext(optionsBuilder.Options); bool isdbDispose = false; //手动记录是否释放db上下文 int dequeueNum = 0; while (true) { if (isdbDispose) { db = new ESHOPContext(optionsBuilder.Options); isdbDispose = false; } ModelQueueParam data; var saved = false;//处理并发字段 lock (_lockerRedisEnqueue) { string redisValue = _redisdb.ListRightPop("stock-y").ToString(); if (redisValue == null) { data = null; } else { data = JsonHelp.ToObject <ModelQueueParam>(redisValue); } } //存在下单人 string userId = ""; string seckillArticleId = ""; if (data != null) { userId = data.userId; seckillArticleId = data.seckillArticleId; int goodQuantity = data.goodQuantity; //存在库存就进行消费库存 dequeueNum++; //同时添加订单信息 string id = Guid.NewGuid().ToString("N"); DateTime nowTime = DateTime.Now; TOrder order = new TOrder(); order.Id = id; order.UserId = userId; order.ArticleId = seckillArticleId; order.OrderNum = id; order.OrderTotalPrice = goodQuantity; order.AddTime = nowTime; order.UpdateTime = nowTime; order.PayTime = nowTime; order.DeliverTime = nowTime; order.FinishTime = nowTime; order.OrderStatus = 3; order.OrderPhone = "12451554"; order.OrderAddress = "地址地址地址"; order.OrderRemark = "订单备注"; order.DelFlag = 1; //进行数据库真实操作 db.TOrder.Attach(order); db.TOrder.Add(order); } else { //是否处理过数据 if (dequeueNum > 0) { var articleInfo = db.TSeckillArticle.FirstOrDefault(u => u.Id == "y01"); articleInfo.ArticleStockNum -= dequeueNum; db.TSeckillArticle.Update(articleInfo); db.SaveChanges(); saved = true; // articleInfo = null; isdbDispose = true; dequeueNum = 0; //处理并发 } //库存没有 等待信号 _whRedis.WaitOne(); } } } catch (Exception ex) { } }
private void DeEnqueueRedis() { try { //连接字符串 string stq = "Server=47.92.198.126;Database=ESHOP;user id=eshop;password=eshop123456;MultipleActiveResultSets=true"; var optionsBuilder = new DbContextOptionsBuilder <ESHOPContext>(); optionsBuilder.UseSqlServer(stq); //using (var db = new ESHOPContext(optionsBuilder.Options)) //{ var db = new ESHOPContext(optionsBuilder.Options); bool isdbDispose = false; //手动记录是否释放db上下文 int dequeueNum = 0; while (true) { if (isdbDispose) { db = new ESHOPContext(optionsBuilder.Options); isdbDispose = false; } ModelQueueParam data; var saved = false;//处理并发字段 lock (_lockerRedisEnqueue) { string redisValue = _redisdb.ListRightPop("order-key").ToString(); if (redisValue == null) { data = null; } else { data = JsonHelp.ToObject <ModelQueueParam>(redisValue); } } //存在下单人 if (data != null) { string userId = data.userId; string seckillArticleId = data.seckillArticleId; int goodQuantity = data.goodQuantity; //存在库存就进行消费库存 articleInfo.ArticleStockNum -= 1; dequeueNum++; //同时添加订单信息 string id = Guid.NewGuid().ToString("N"); DateTime nowTime = DateTime.Now; TOrder order = new TOrder(); order.Id = id; order.UserId = userId; order.ArticleId = seckillArticleId; order.OrderNum = id; order.OrderTotalPrice = goodQuantity; order.AddTime = nowTime; order.UpdateTime = nowTime; order.PayTime = nowTime; order.DeliverTime = nowTime; order.FinishTime = nowTime; order.OrderStatus = 3; order.OrderPhone = "12451554"; order.OrderAddress = "地址地址地址"; order.OrderRemark = "订单备注"; order.DelFlag = 1; //进行数据库真实操作 db.TOrder.Attach(order); db.TOrder.Add(order); } else { //是否处理过数据 if (articleInfo != null) { //articleInfo.ArticleStockNum -= dequeueNum; //db.Entry(articleInfo).State = EntityState.Modified; db.TSeckillArticle.Update(articleInfo); //处理并发 while (!saved) { try { db.SaveChanges(); saved = true; // articleInfo = null; isdbDispose = true; dequeueNum = 0; } catch (DbUpdateConcurrencyException ex) { foreach (var entry in ex.Entries) { if (entry.Entity is TSeckillArticle) { var proposedValues = entry.CurrentValues; //当前需要写入数据库的值 var databaseValues = entry.GetDatabaseValues(); //数据库值 entry.OriginalValues.SetValues(databaseValues); } } } } } //库存没有 等待信号 _whRedis.WaitOne(); } } //} } catch (Exception ex) { } }