0

Ok, so I came to this company that recalled its software from an offshore, no-longer-extant entity. We all know the drill.

In looking at the nuts and bolts, I come across the 'lock' keyword. Googling, I find that Entity Framework does not support multi-threading.

My question is: How can I be 100% certain that the application is attempting to run in multiple threads? Is the existence of the 'lock' keyword enough?

Thanks.

user426364
  • 160
  • 9
  • The [lock](http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx) keyword as per the MSDN documenation `ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released.` Doesn't help if you don't enforce it though and doesn't mean that you are running multiple threads. – Bob. Dec 16 '14 at 16:05
  • I very often use Multithreading in EF, for example I may have a list of employees that I want to update, so for each employee I'll spin off a thread to populate the changes. EF itself doesn't care who and when it's called it just does what it's told. In my situation; I'm always very careful to stay out of trouble by knowing the Unit of Work pattern which essentially says "A piece of work which is wholly contained can be split off onto a thread without impacting any other activity." I personally never use LOCKs with EF. – JWP Dec 16 '14 at 16:09
  • A nice alternative would be to count or enumerate the created managed threads in the application. If it's greater than 1, then it's multithreaded, but no one seems to be able to find a *programatically* feasible solution to do so. Please check: [How can I enumerate all managed threads in C#?](http://stackoverflow.com/questions/466799/how-can-i-enumerate-all-managed-threads-in-c) for more information. – Matias Cicero Dec 16 '14 at 16:39
  • Entity Framework, itself, may not be multi-threaded, in the sense that it does not fire off its own threads to run multiple queries at the same time or something. However, if you're using it within the confines of a website, it *is* essentially running multi-threaded, because a web server is multi-threaded. If two requests come in simultaneously and database queries are involved in handling the request, then you have distinct Entity Framework work being done in multiple threads simultaneously. – Chris Pratt Dec 16 '14 at 16:53

2 Answers2

1

If this is a ASP.NET/MVC web app and you have the lock keyword that is probably because the app is in IIS and IIS dispatches different user requests on different threads and therefore web app becomes multi-threaded.

In case of MVC - Controller is created per request and then it is processed on different thread. That leads to the need to lock something if two users at a time are going to access it.

If this is a desktop app and the lock is where data access happens it might be for similar purpose.

Ognyan Dimitrov
  • 6,026
  • 1
  • 48
  • 70
  • Yes, the lock is in the add or addRange section for dbContexts. Basically, looks like there are multiple dbContexts, and the lock is during the adding to the list (the class is DbContextList: List). – user426364 Dec 17 '14 at 14:10
0

The lock keyword alone is not enough, they could be using it incorrectly after all. lock will just prevent more than one thread from entering the protected area at any one time. What is being protected by the lock? Data stored in a static variable is available to all users (threads) using the app and so should have controlled access.

David Laughlin
  • 600
  • 1
  • 6
  • 11
  • The actual error message is "The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe." But I can't ask that question because I'm not sure how to phrase it, so I need to know if I'm in a multi-threaded context. If you add a comment on how to see if ... – user426364 Dec 16 '14 at 20:23
  • ... on how I can tell if I am or not multi-threaded, I can accept yours as an answer. Do I search the code for thread.start? (there are none). In fact, I'm going to accept it, you can add a comment if you like. I am just such an EF noob that I'm not sure how to ask the right question yet. – user426364 Dec 16 '14 at 20:24