Problem when running multiple LINQ queries in a single transaction scope

by Lixin 20. December 2008 17:34

I developed an application which was doing two LINQ queries in a single transaction scope and it worked very well in a local area network (LAN) physically. I am using LINQ to Entities with ADO.NET Entity Framework to access my database.

 

The code is shown as follows,

 

using (var t = new TransactionScope(TransactionScopeOption.Required,

new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))

{

packets2 = DataContext.RawDataPackets

            .Where(p => p.ServerTime >= start

                                    && p.ServerTime < end

                  && p.StreamNumber == streamNumberMeeting

                        && p.DataSource == dataSourceNumber

                  )

                  .OrderBy(p => p.ClientTime)

                  .OrderBy(p => p.ServerTime)

                  .ToList();

packets.AddRange(packets2);

 

      packets2 = DataContext.RawDataPackets

            .Where(p => p.ServerTime >= start

                                    && p.ServerTime < end

                        && p.StreamNumber == streamNumberMeeting

                        && p.DataSource == dataSourceNumber

                  )

                  .OrderBy(p => p.ClientTime)

                  .OrderBy(p => p.ServerTime)

                  .ToList();

packets.AddRange(packets2);

 

      t.Complete();

}

 

Recently I deployed the same application to another computer which only has access to the database server through a VPN connection and it threw exception after starting, which said that it cannot connect to the remote server. I was misled by the message for a while and went to check my DTC (Distributed Transaction Coordinator) configuaration and all settings looked okay to me.

 

I rethought around the problem and finally came to a guess that it could be the DTC which is unable to handle multiple queries in a single transaction scope on access across physically remote network very well.

 

Then I modified my code to the following,

 

using (var t = new TransactionScope(TransactionScopeOption.Required,

new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))

{

packets2 = DataContext.RawDataPackets

            .Where(p => p.ServerTime >= start

            && p.ServerTime < end

                  && p.StreamNumber == streamNumberMeeting

                        && p.DataSource == dataSourceNumber

            )

            .OrderBy(p => p.ClientTime)

            .OrderBy(p => p.ServerTime)

            .ToList();

t.Complete();

}

packets.AddRange(packets2);

 

using (var t = new TransactionScope(TransactionScopeOption.Required,

new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))

{

packets2 = DataContext.RawDataPackets

            .Where(p => p.ServerTime >= start

                        && p.ServerTime < end

                  && p.StreamNumber == streamNumberMeeting

                  && p.DataSource == dataSourceNumber

            )

            .OrderBy(p => p.ClientTime)

            .OrderBy(p => p.ServerTime)

            .ToList();

t.Complete();

}

packets.AddRange(packets2);

 

Notice I have wrapped those two queries into two separate transaction scope. Then I tested it. It just workedCool!

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , ,

Communication | Data

Comments

Comments are closed

Powered by BlogEngine.NET 1.4.5.0
Original themed by Mads Kristensen and customised by Lixin