views:

49

answers:

1

I am using LINQ select statement wrapped in a TransactionScope (to change the locking) but according to SQL Profiler, it doesn't seem to be working. My code looks like:

using (var ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted} ))
{
   using (myDBDataContext dbPKC = new myDBDataContext(conn))
   {
      ...query...
      ts.Complete();
      return xmlMachine;
   }
}

Now I would expect SQL Profiler to show BatchStarting and BatchComplete for my select statement. But it shows RPC:Completed. Why? when I run this code:

using (SqlConnection conn1 = new SqlConnection())
    {
      conn1.ConnectionString = WebConfigurationManager.ConnectionStrings["myConnectionString"].ToString(); ;
      conn1.Open();
      using (SqlTransaction trans1 = conn1.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
      {
        SqlCommand cmd = new SqlCommand("select * from Machines where pkID = 5");
        cmd.Connection = conn1;
        cmd.Transaction = trans1;
        SqlDataReader reader = cmd.ExecuteReader(); // just execute something
      }
    }

It shows BatchStarting and BatchComplete. Why doesn't LINQ seem to "see" the TransactionScope?

Also is there a way to confirm that my isolationlevel is correct through Profiler? I can only see the initial connection's isolation level through Audit Login. No "update" is displayed to show that it was changed or what each isolationlevel each query is using.

Any help would be wonderful!

Also, this code is running in a WCF (3.5) service connecting to SQL Server 2008

+1  A: 

UPDATED:

Try something like this to check isolation level:

using(TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, options))
{
    //Verify Scope using DBCC USEROPTIONS
    SqlCommand cmd = (SqlCommand)ctxt.Connection.CreateCommand();
    cmd.CommandText = "DBCC USEROPTIONS";
    SqlDataReader r = cmd.ExecuteReader();
    while (r.Read())
    {
        Console.WriteLine(r.GetValue(0) + ":" + r.GetValue(1));
    }
}   

ADDED:

Look for SET TRANSACTION ISOLATION LEVEL

igor
switching the order (instantiate db then transcope) still shows RPC:Completed :(
BabelFish
thank you.. appears that transcope is working.. the profiler needs to show SP statements because linq executes the sql through sq_executesql and show TM statements to display that transactions are being done. your code also helped show that the isolation level is correct.textsize:-1language:us_englishdateformat:mdydatefirst:7lock_timeout:-1quoted_identifier:SETansi_null_dflt_on:SETansi_warnings:SETansi_padding:SETansi_nulls:SETconcat_null_yields_null:SETisolation level:read uncommitted
BabelFish
This leads to my next questions. This select statement is blocked by another transaction that is using readcommited. why would this select statement get blocked? the two calls are dealing with the same tables but different rows. both calls do have large where clauses if that makes a difference.
BabelFish
@BabelFish Only one of the isolation level options can be set at a time, and it remains set for that connection until it is explicitly changed. All read operations performed within the transaction operate under the rules for the specified isolation level unless a table hint in the FROM clause of a statement specifies different locking or versioning behavior for a table. http://msdn.microsoft.com/en-us/library/ms173763%28SQL.90%29.aspx
igor
yes, to clarify. their two different transactions. What appears to be happening is that tran1 does a long process updating a row from table X which blocks tran2's select statement to table X. Not sure why since tran2's select statement is basically TableX.Single(x => x.pkID = 14); Should not that statement find the exact row with no problems if row 14 is NOT being touched by tran1? changing isolation to readuncommited for tran2 resolves the one issue but why is readcommitted not sufficient if my select is based off of primary key? tran1 uses readcommitted. thanks again!
BabelFish