A threading question

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
ashlar64
Posts: 75
Joined: Thu 04 May 2006 18:56

A threading question

Post by ashlar64 » Thu 17 May 2012 20:17

Could someone tell me if this would be safe to do?

If I had a context lets say M1. And I have two threads that will work on M1. One thread will only insert elelments into M1.TestTable1 and the other thread will only do Inserts on M1.TestTable2....will I have any issues doing this?

Zero-G.
Posts: 398
Joined: Sat 09 Dec 2006 12:20

Re: A threading question

Post by Zero-G. » Fri 18 May 2012 09:16

Hey

I am not a DevArt guy, but maybe my answer will help you.
The DataContext itself is not thread safe. - So, I think, this is not a good idea.
But 2 thoughtful inputs
1) Split your DataContext into 2 Context's
2) Use Asycn Queries.

Hope this will help - Have a nice day

ashlar64
Posts: 75
Joined: Thu 04 May 2006 18:56

Re: A threading question

Post by ashlar64 » Fri 18 May 2012 12:54

Hmm two DataContexts...interesting idea.
I was initially thinking if it wasn't thread safe then to make a bunch of Lists (each would represent a different table) and do a Insert all after all the threads were done.

But I think I like your idea with using multiple Context's and will give that a try.

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: A threading question

Post by MariiaI » Mon 21 May 2012 07:34

DataContext objects are not thread-safe, at least because the DataContext instance uses a single connection, which is not thread-safe itself.
You can use two DataContext instances, as Zero-G. has advised.
JIC: generally, it is recommended to use a new DataContext object per each unit of work instead of keeping it for a long time.

Post Reply