在TransactionScope中,Serializable隔离级别某些情况不起作用

1.在TransactionScope中,Serializable隔离级别某些情况不起作用
2.测试代码
使用可重复读隔离级别没什么问题,但是用默认即序列化时出现问题。
即如果先执行scoreRepository.Add(userId, scoreCount);新增数据,后进行总和增加时事物没起作用。
执行顺序反过来就有作用

    public static void AddScore(object data)
    {
        var dataTest = (TestData)data;
        try
        {
            //using (var tran = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted }))
            //using (var tran = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.RepeatableRead }))
            //using (var tran = new TransactionScope())
            using (var tran = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.Serializable }))
            {
                var userId = 3;
                var scoreCount = dataTest.Random.Next(1,100);
                dataTest.Message = dataTest.Message + "分数:"+ scoreCount;
                var userInfoRepository = new UserInfoRepository();
                var scoreRepository = new ScoreRepository();
                scoreRepository.Add(userId, scoreCount);
                userInfoRepository.AddSocre(userId, scoreCount);
                dataTest.Message = dataTest.Message + "成功";
                tran.Complete();
            }
        }
        catch (Exception ex)
        {
            dataTest.Message = dataTest.Message + "异常" + ex.Message;
        }
        finally
        {
            dataTest.mutipleThreadResetEvent.SetOne();
        }
    }
阅读 2.7k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进