';脏读取';在使用readcommitted隔离级别设置事务作用域期间不工作

本文关键字:作用域 事务 工作 设置 readcommitted 读取 脏读 隔离级 | 更新日期: 2023-09-27 18:22:11

我有一个代码如下:

using (TransactionScope scope = 
   new TransactionScope(TransactionScopeOption.Required), new TransactionOptions)
{ 
    IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
    // "dirty" read  for test purposes , to see if my 
    // select will actually select the information I just inserted
    actual = target.GetCostCentersRates(accountId);
}

这不起作用,我已经测试了查询,当提交数据时,它们可以有效地工作,但当未提交数据时会出现不允许我检查脏读的问题,即使隔离级别设置为readcommitted。我想弄清楚为什么我不能访问这些信息,因为我无论如何都不能将信息提交到我们的数据库中,因为这是一种测试方法。
谢谢!

这是的全部内容

 public void GetCostCentersRatesTest()
    {
        using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommited }))
        {
            //Arrange
            BRL.AdministrativeArea.SystemClientBusinessRole systemClient = new BRL.AdministrativeArea.SystemClientBusinessRole();
            int systemClientId = systemClient.InsertSystemClient(systemClientInfo).systemClientId;
            BRL.BRLProperties.systemClientId = systemClientId;
            employeeInfo.multiCompaniesInfo.systemClientId = systemClientId;
            int stateId = 1;
            int cityId = 1;
            int functionId = 1;
            employeeInfo.stateId = stateId;
            employeeInfo.cityId = cityId;
            employeeInfo.functionId = functionId;
            int employeeId = employees.InsertEmployeers(employeeInfo);
            BRL.BRLProperties.employeeId = employeeId;
            IActionReturnInfo actionAccount = (accounts.InsertAccountPlan(accountPlanInfo));
            int accountId = Convert.ToInt32(actionAccount.UpdateDataSourceList[0].ToString());
            clientInfo.stateId = stateId;
            clientInfo.cityId = cityId;
            clientInfo.stateIdCorrespondency = stateId;
            clientInfo.cityIdCorrespondency = cityId;
            clientInfo.stateIdDelivery = stateId;
            clientInfo.cityIdDelivery = cityId;
            clientInfo.multiCompaniesInfo.systemClientId = systemClientId;
            clientInfo.multiCompaniesInfo.employeeId = employeeId;
            int clientId;
            clients.InsertClient(clientInfo, out clientId);
            centerCostInfo.systemClientId = systemClientId;
            centerCostInfo.clientId = clientId;
            centerCostInfo.employeeId = employeeId;
            centerCostInfo.directorID = employeeId;
            centerCostInfo.managerID = employeeId;
            centerCostInfo.multiCompaniesInfo.systemClientId = systemClientId;
            centerCostInfo.multiCompaniesInfo.employeeId = employeeId;
            IActionReturnInfo action = new CenterCostsBusinessRole().InsertCostCenter(centerCostInfo);
            int centerCostId = Convert.ToInt32(action.UpdateDataSourceList[0].ToString());
            rate.accountId = accountId;
            rate.centerCostId = centerCostId;
            costCenterRates.Add(rate);
            int costCenterRateId;
            AccountBusinessRole target = new AccountBusinessRole();
            DataSet actual;
             IActionReturnInfo costCenterRateAction = accounts.InsertCenterCostRates(costCenterRates);
                costCenterRateId = Convert.ToInt32(costCenterRateAction.UpdateDataSourceList[0].ToString());
                //Act
                actual = target.GetCostCentersRates(accountId);

            //Assert
            Assert.IsTrue(FindInDataset(costCenterRateId, actual, "ACCOUNTID"));
        }
    }

';脏读取';在使用readcommitted隔离级别设置事务作用域期间不工作

让脏读取工作。您启动事务(显式)将更改推送到数据库

在另一个带有readcommitted的事务中选择数据,您也会得到未提交的内容。

然后,您要么回滚,要么提交您在中进行更改的事务。

sql server manager就是这么说的启动查询

Start Transaction
Insert SomeTable(500)

启动另一个查询

Select * from SomeTable With(READUNCOMMITTED)

你会看到500张唱片。

老实说,ADO.Net使用的断开连接的模型试图弄清楚你为什么要这么做,除了分布式事务之外,这是不必要的,你不会这样测试它们。如果你测试的只是插入,那么就这么做吧,听起来就像你在测试实时数据库,这是一件非常糟糕的事情