Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - LINQ to SQL in multi-threaded environments

Reply
 
Old 08-22-2008   #1 (permalink)
Eric Falsken


 
 

LINQ to SQL in multi-threaded environments

*background*
I've got an application that I'm creating that is using LINQ to SQL. We have
a nice DataContext that is able to do lots of nice updating. But we are using
something like an MVC model where the controllers are doing the updating.
Since objects have to be updated/deleted to the same CataContainer that they
were retreived from. So to solve "attach" and "detach" issues, we just make
sure that the "Controller" in our setup is static. (using the Provider model)

*question*
But now that the Context is shared among all our requests, calling
DataContext.Submit() will submit ALL changes to ALL objects that came from
the DataContext, even if the changes are incomplete or in the process of
being made still. Heck, some changes might not even get past validation. How
can I call Submit() to apply some changes but not others? Is there some way
to do this with transaction sopes? e.g.

MyObject obja = context.Table.First();
MyObject objb = context.Table.Second();
objb.Name = "name2"; //could be happening in another thread
using (TransactionSope ts = new TransactionScope()){
obja.Name = "name1";
context.Submit(); //objb should not be updated here
}


--
Eric Falsken
Technical Evangelist: db4o
http://www.db4o.com

My System SpecsSystem Spec
Old 08-25-2008   #2 (permalink)
Steven Cheng [MSFT]


 
 

RE: LINQ to SQL in multi-threaded environments

Hi Eric,

For your scenario, I think the problem is how to make each "submit"
operation to associate with its own DataContext data. If you're using a
shared single Datacontext instance, I'm afraid that will be very
inconvenient and inefficient. That means in multi-threading environment(if
multiple thread can access it), you will need to perform locking in order
to serialize all the data operations. For example, you can define a helper
class which expose a static method that will accept the parameters and call
the single DataContext to do the submit operation. In that function, the
code is somewhat like:

lock ( a private object reference)
{
//code here to do submit via the shared DataContext
}

Such code will be overkill if there are many concurrent thread requesting
for such submit operation.

BTW, I don't think TransactionScope will help much here since
TransactionScope is mainly used for group multip operations to be finished
as atomic task suite instead of ensuring thread-safety or method reenter.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxx.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
Quote:

>From: =?Utf-8?B?RXJpYyBGYWxza2Vu?= <efalsken@xxxxxx>
>Subject: LINQ to SQL in multi-threaded environments
>Date: Fri, 22 Aug 2008 10:47:00 -0700
Quote:

>
>*background*
>I've got an application that I'm creating that is using LINQ to SQL. We
have
Quote:

>a nice DataContext that is able to do lots of nice updating. But we are
using
Quote:

>something like an MVC model where the controllers are doing the updating.
>Since objects have to be updated/deleted to the same CataContainer that
they
Quote:

>were retreived from. So to solve "attach" and "detach" issues, we just
make
Quote:

>sure that the "Controller" in our setup is static. (using the Provider
model)
Quote:

>
>*question*
>But now that the Context is shared among all our requests, calling
>DataContext.Submit() will submit ALL changes to ALL objects that came from
>the DataContext, even if the changes are incomplete or in the process of
>being made still. Heck, some changes might not even get past validation.
How
Quote:

>can I call Submit() to apply some changes but not others? Is there some
way
Quote:

>to do this with transaction sopes? e.g.
>
>MyObject obja = context.Table.First();
>MyObject objb = context.Table.Second();
>objb.Name = "name2"; //could be happening in another thread
>using (TransactionSope ts = new TransactionScope()){
> obja.Name = "name1";
> context.Submit(); //objb should not be updated here
>}
>
>
>--
>Eric Falsken
>Technical Evangelist: db4o
>http://www.db4o.com
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
VPC 2007 single-threaded? Virtual PC
Problem creating an BitmapSource from an HBitmap in threaded code .NET General
Setting environments variables PowerShell
Impossible to use Vista in Large Environments Vista General
Multi-Threaded Programming Without the Pain Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46