See we got bit in the butt using a static connection object. We
figured .Net would create a connection object per user, but that is
not the case. That static connection object was being shared by all
users that were logging in to the app.
Yes, a static connection object will cause problems, but that's
static *data*.
So all static funtions are
shared across the thread that IIS launches the app in.
Or, more specifically, all functions may be run concurrently in multiple
IIS threads. This is true of all functions, static or not. The big
difference is that instance methods get their own copy of class
variables.
A mutex is
absolutely need to prevent thread collisions. we had to remove the
static object and make it an instance object when we started getting
errors related to SqlDataReaders already being open on the connection
( this being tied to SQLDataReaders that other users were acessing).
Once we make the connection object an instance variable the errors
stopped.
Right, but again that's static data. As I said initially, you
definitely don't want to be using static/shared ADO.NET objects.
Static/shared functions are fine, static/shared objects are not
thread-safe (unless they're read-only, like a connection string). Let
me be a little more specific in code.
public static DataSet GetSomeData()
{
SqlConnection conn = new SqlConnection(conn_string);
SqlDataAdapter da = new SqlDataAdapter("SELECT...", conn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
That's a static function that doesn't need to be protected by a mutex.
Even if two threads call it at exactly the same time, each will get its
own copy of the connection, adapter, etc. But...
static SqlConnection conn;
public DataSet GetSomeData()
{
conn = new SqlConnection(conn_string);
... // just like before
}
That's asking for trouble, because there's only one connection object
shared between all callers. It doesn't matter whether the *function*
is static or not, it only matters whether the data is.
So, in answer to your original question, what does your code look like?
If you've got static functions, I personally think that's a fine design
(and the mutexes aren't needed). But if your data declarations are
static, then I think you have problems. If nothing else, the necessary
mutexes are going to cause a serious bottleneck in the app.