views:

103

answers:

4

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?

+1  A: 

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...

Jon Skeet
public static ORADataFactory objFactory = new ORADataFactory(ConfigurationManager.ConnectionStrings["ORACONNECTION"].ConnectionString);
Ranga
ORADataFactory objFactory = UTL_DBHandler.objFactory; objFactory.CreateDBConnection(); The UTL_DBHandler is the class containing the Static method!!!!
Ranga
@Ranga: Please provide more code, and put it in the question. Does ORADataFactory has a static connection property, for example?
Jon Skeet
A: 

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.

jv42
A: 

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?

Joachim VR
A: 

If in answering http://stackoverflow.com/questions/3862379/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 http://stackoverflow.com/questions/3861298/error-when-multiple-users-access-my-web-app-at-the-same-timer 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.

Jon Hanna