开发者

Static method Issue in C#.net application

I'm using a static method in a class, this static method contains a connection string. I use this static method in all other methods to open a database connection and I dispose开发者_开发问答 the connection within this method.

After I deploy this application in IIS, when two users login to the application on two different machines and access the same page containing the same menu at a time, I'm getting the error as:

Cannot perform this operation for disposed connection.

Why is this happening?


I suspect you've actually got a static variable storing the connection, rather than having a connection per request. You shouldn't do that: each request should open the connection as it needs to, and dispose of the connection at the end... but you shouldn't be storing the connection in a field where other requests may use it.

Of course, if you could actually show us some sample code, it would be rather easier to diagnose...


You have a concurrency problem. You have to implement some form of thread safety to fix that. There are numerous articles on this, with pros and cons.


Since you dispose of the connection in the method it is created, is it possible you try to use this connection somewhere outside this method, where it's already disposed?


If in answering How to debug the application after deployed in IIs? I said it sounds the cause of the problem was that you were statically scoping your connection.

In answering Error when multiple users access my web app at the same time Marc Gravell said it sounded like a static resource, specifically the connection, and suggested you fix it by scoping them locally.

In response to him, you said it was not statically scoped.

Now, you are saying that you are manipulating your connections through a static method, and closing it in the same method. This makes it seem even more likely to be the case, and that's the suggestion Jon Skeet has made here.

Can you see how people are going to keep thinking your connection is static if you don't show the example code to show otherwise?

Maybe it's not, but it matches the symptoms you described perfectly in the beginning, and it still matches it now. This is why three different people in three different threads have offered the same conclusion. Asking a fourth time is likely just to have fourth person offer the same answer unless you demonstrate why it can't be so, because it does match the problem so very well.

"Insanity is repeating the same behaviour and expecting different results" - Rita Mae Brown.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜