views:

838

answers:

8

I've seen a lot of discussion about this subject on here.

If i have a static class w/ static methods that connects to a database or a server, is it a bad idea to use this in a multi-user environment (like a web page)? Would this make a new user's tread wait for previous users' threads to finish their calls before accepting a new one?

What would be the implications of this with multi-threading, also?

Thx!

A: 

A little weirded out by this question. As to why you have so much static going on.

But I think you're asking about threading issues, so I would say go check out some of the docs on threading http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.80).aspx

Chad Grant
+5  A: 

If each static method is fully responsible for acquiring its resources and then disposing its resources within the scope of the method call (no shared state), then you shouldn't have any problem with threading that you wouldn't have using instance classes. I would suggest, however, that the bigger problem is that a reliance on public static methods (in static or non-static classes) creates many other design problems down the road.

  • First of all, you're binding very tightly to an implementation, which is always bad.
  • Second, testing all of the classes that depend on your static methods becomes very difficult to do, because you're locked to a single implementation.
  • Third, it does become very easy to create non-thread safe methods since static methods can only have static state (which is shared across all method calls).
Michael Meadows
A: 

Static is only defining the scope where the method is defined, and how it is bound / called. It has nothing to do with multi threading.

You need to be careful with static fields. They are shared by all threads. Threads are not waiting for each other, but you need locks to make it work.

But if your application is a bit more complex than Hello World, you should consider to have you methods not static but to use object oriented patterns.

Stefan Steinegger
+3  A: 

Static methods do not have any special behaviour in respect to multithreading. That is, you can expect several "copies" of the method running at the same time. The same goes for static variables - different threads can access them all at once, there is no waiting there. And unless you're careful, this can create chaos.

Vilx-
+1  A: 

Yes it's a bad idea.

When you use one connection for all your users if someone performs an action that requires, lets say 15 seconds, just for database access, all other users will have to wait in order to connect to the database

Sergio
The bad idea here is the use of one connection, not the static methods, IMO.
çağdaş
I agree with you
Sergio
A: 

If you use one static connection to access the database, you will have to synchronize method calls. Multiple threads asking the database for data over a single connection will ... ehhmmm ... mess things up. So you are serializing all threads' data access and this will have a large impact on the performance.

If every call opens its own connection, you do not need to serialize all threads because there is no shared connection. Creating a connection per request is still an expensive design.

If you use a static connection pool you will reduce this performance impact because you only need to serialize the access to the connection pool.

Further, statics are in general not a good design decission - they make unit testing very complicated. You should consider using the Singleton or Monostate pattern.

Daniel Brückner
A: 

If you do it right, it won't be a problem. If you do it wrong, it has the potential force sequential access to the resource.

Sometimes the difference between right and wrong can be very subtle and hard to spot, but the main thing is that no method should rely on or lock any "state" (members) of the class.

Joel Coehoorn
A: 

I use static method for lookup objects. I can manage all lookups objects in one place (using caching) for the asp.net application and all methods call it by using static method.

By this way, I do not need to instantiate lookups objects everytime I need it and it reduce the need to call DB for performance enhancement.

RocketsLee