Anderskj1 wrote:
If I have a static method like
static public InsertUser(string name)
{
/// LLBL code to insert a user
}
Sine the class is in the same AppDomin (ASP.NET) i guess all request will use the same class instance of this method so im worried about concurrency issues. Should I?
Is there any thing i should do like lock { // LLBLCODE } or is LLBL threadsafe?
Thanks in regards
Anders
I am not a threading expert by anymeans, but I think this example will suffice. Tak your method a bit further:
1: static public InsertUser(string name)
2: {
3: UserEntity newUser = new UserEntity();
4: newUser.IsNew = true
5: newUser.Name = name;
6: using (DataAccessAdapter adapter = new DataAccessAdapater)
7: {
8: adapter.SaveEntity(newUser)
9: }
10:}
Lets say that thread A enters the code first passing name = "Bob" and gets down to line 7. Then thread B comes into the same method passing name="Joe" and thread B makes it to line 6 before thread A gets to line 8. Then for whatever reason, .NET decides to switch control back to thread A. When thread A runs line 8 a new user will be created whose name is "Joe", and then thread B would also do the same thing (providing no other threads modify the entity)
This is my theory as to what could happen. You could use locks, mutexes or other threading mechanisms.
Anyone else please feel free to give this a thumbs up or down.