“脏读”在设置为未提交隔离级别的事务范围内不工作
我有一个类似这样的代码:
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);
}
这不起作用,我已经测试了查询,它们在提交数据时有效地工作,但是当未提交数据时,它会出现不允许我脏读的问题check ,即使隔离级别设置为 readuncommited 也是如此。 我想弄清楚为什么我无法访问这些信息,因为我无论如何都无法将信息提交到我们的数据库中,因为这是一种测试方法。
谢谢!
这是整个事情的经过
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"));
}
}
......
I have a code that goes something like this:
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);
}
this does not work, i have tested the queries and they work effectively when data is commited, but when it is not committed it presents the problem of not allowing me the dirty read to check , even when the isolation level is set to readuncommitted.
I would like to just figure out why i cant access the information, for I cannot by any means commit the information to our database, since this is a test method.
thank you!
Here is the whole thing
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"));
}
}
.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让脏读发挥作用。
您(明确)开始交易
将更改推送到数据库
在另一个事务中,未提交
选择数据,您也会得到未提交的内容。
然后,您可以回滚或提交您进行更改的事务。
因此,从 sql server manager 来看
启动一个查询
启动另一个查询,
您将看到 500 条记录。
老实说,试图弄清楚为什么要这样做,ADO.Net 使用的断开连接模型使得除了分布式事务之外没有必要这样做,并且您不会像这样测试它们。如果您要测试的只是插入,那么就这样做,听起来几乎就像您在实时数据库上进行测试一样,这是一件非常糟糕的事情。
For dirty reads to work.
You start a transaction (explicitly)
push the changes to the db
In another tranasaction with readuncommitted
select the data, you'll get uncommitted stuff as well.
Then you'd either roll back or commit the transaction you did the changes in.
So from say sql server manager
Start up a query
Start up another query
you'll see the 500 records.
Trying to figure out why you are doing this to be honest, the disconnected model ADO.Net uses, makes it unnecessary except for distributed transactions and you wouldn't test them like this. If all you are testing is the inserts, just do that, sounds almost as though you are tesing on the live db, which qualifies are seriously bad thing to do.