0

下面的代码是否存在如果两个人同时提交会检索到错误的 s.saleID 的风险?

        protected void submitSale(int paymentTypeID)
    {
        tadbDataContext tadb = new tadbDataContext();

        ta_sale s = new ta_sale();

        decimal total = decimal.Parse(lblTotal.Value);

        s.paymentTypeID = paymentTypeID;
        s.time = DateTime.Now;
        s.totalSale = total;
        tadb.ta_sales.InsertOnSubmit(s);
        tadb.SubmitChanges();

        char[] drinksSeparator = new char[] {'|'};
        char[] rowSeparator = new char[] { ':' };
        string drinkString = lblSummaryQty.Value.Substring(0, lblSummaryQty.Value.Length - 1);
        string[] arrDrinks = drinkString.Split(drinksSeparator);


        foreach (string row in arrDrinks)
        {
            string[] arrDrink = row.Split(rowSeparator);
            int rowID = Convert.ToInt16(arrDrink[0]);
            int rowQty = Convert.ToInt16(arrDrink[1]);

            ta_saleDetail sd = new ta_saleDetail();
            sd.drinkID = rowID;
            sd.quantity = rowQty;
            sd.saleID = s.saleID;
            tadb.ta_saleDetails.InsertOnSubmit(sd);
        }

       tadb.SubmitChanges();
    }
}

如果是这样,我应该怎么做才能确保它是原子的?(我认为应该没问题,但要仔细检查!)

4

1 回答 1

0

可以肯定的是,只需编写一个调用submitSale 2 次甚至更多次的测试并使其几乎同时提交更改即可。如果测试失败,请使用lock语句,但要小心,它会导致死锁。更改submitSale后,只需以高负载(大量同时调用)再次测试它。以此类推,直到您通过测试。

于 2012-06-11T16:04:46.537 回答