0

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"));
        }
    }

.....

virginia_c
  • 3
  • 2
  • 9
  • Is this hitting some sort of a database back end? If so - which one? – RQDQ Feb 02 '12 at 17:26
  • I tried re-formatting your code for readability, and in doing so, I discovered that it doesn't make any sense. You're setting `IsolationLevel ` and not using it. You've declared `scope` but are not using it either. Can you copy the code directly out of your IDE, and paste it into the question. Then highlight it and press the "code" button. That way, SO will prettify your code exactly the way it came out of your IDE. – Chase Florell Feb 02 '12 at 17:29
  • I set the isolation level on the TransactionScope constructor. – virginia_c Feb 02 '12 at 17:36
  • I do not use the variable scope because I used "using" , however, i will copy my whole method to show. thanks1 – virginia_c Feb 02 '12 at 17:37
  • What do you mean a database back end? The only thing that happens is that, when I attempt the dirty read, the data that I inserted in the transaction does not appear, Im not sure I understand the question! – virginia_c Feb 02 '12 at 17:38
  • Database backend meaning.. where is the data being stored or retrieved from i.e. SQL Server, Oracle, Access...etc..??? – MethodMan Feb 02 '12 at 17:43
  • Yes, sorry! I am simulating an insertion on SQL 2008, I have even tried using the CHAOS isolation level, because I thought that maybe the Table where i inserted data wasnt being blocked, but the row that i inserted was, thus causing my inability to read it. I dont know what to do! All of these insertion methods trigger sql stored procedures. – virginia_c Feb 02 '12 at 17:45

1 Answers1

2

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 Transaction
Insert SomeTable(500)

Start up another query

Select * from SomeTable With(READUNCOMMITTED)

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.

Shoter
  • 976
  • 11
  • 23
Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • We just wanted to use something as a way of Testing all of our methods, because our methods treat the data, and we need to know in this case, for example, if the select filters the information correctly. To execute the select, we must simulate the insertion to guarantee that actual data exists in the database. This will only be done in our development(test) database – virginia_c Feb 02 '12 at 17:50
  • Okay why can't you just commit the data to the test database. By the time you've messed around with this you aren't testing what you are using anyway. Oh and readuncommitted is a hint, the dbms might ignore it. – Tony Hopkinson Feb 02 '12 at 18:07
  • Oh, that may be the problem. Well, the thing is im just following orders, i think they eventually will put the test up on our final version and use it to make sure that, when compiling that the version is fully functional. This is why we are using transaction scope, so we dont commit anything. For now, we dont need it since were using test database, but i guess in the future...who knows. Boss' orders ! – virginia_c Feb 02 '12 at 18:28
  • Well, problem solved! Someone altered one of the stored procedures and i was getting some data inconsistency, it wasnt a problem with my isolation level! thanks! – virginia_c Feb 02 '12 at 19:18
  • I can't come up with an explanation as to why your boss is making you jump through this set of hoops. When I'm that position, I ask and I insist on a sensible answer, otherwise I can't do my job right. PS I've never been a big believer in boss knows best, rarely true in my experience. – Tony Hopkinson Feb 04 '12 at 15:33