开发者

Simultaneous LINQ data retrieval problem

开发者 https://www.devze.com 2023-01-26 16:24 出处:网络
I came across a very strange problem which never happened to me before. In the login code: var sqlLogin = db.LoginRetrieve(loginID, archived).SingleOrDefault();

I came across a very strange problem which never happened to me before.

In the login code:

var sqlLogin = db.LoginRetrieve(loginID, archived).SingleOrDefault();

//(db is the linq data context)

--problem:

If two users login 开发者_开发问答at the same time, this line of code will throw an exception which is "The required column 'UserLoginID' does not exist in the results." But if a single user logs in or two users don't click the button at the same time, it will have no exception.

Is there anyone can share some lights on this? Thanks in advance.

Han


I suspect that your DataContext is shared between requests.

Don't do that.

You should create a separate DataContext for each request.
Otherwise, you'll get nasty threading issues like this one. (DataContext isn't thread-safe)


In general, you should be very careful when sharing objects between requests (eg, statics or Application / Session state).
Unless you know specifically otherwise, you should assume that the object is not thread-safe and cannot be shared.

0

精彩评论

暂无评论...
验证码 换一张
取 消