3

Hi I am developing a web site I have a class which is the connection to data base The class consists a methods that write and read from the data base Currently, the class is static and also its methods I call the class from web pages like that:

//mydbClass is the name of the class , not an object
mydbClass.getUserName(userID) 

The question is: Do I need to create a class object, so that each time user asking for a page a new object is created and it communicates with the data base , like that:

mydbClass mydb = new mydbClass();
mydb.getUserName(userID)

Because if I do not create a new object So all the users that read or write to the data base Will use the same static object, then it will be very busy and perhaps it will collapse I'd love an answer Thanks micha

1
  • 6
    what do you mean "it will be very busy and perhaps it will collapse"? Commented Mar 11, 2011 at 21:46

4 Answers 4

2

If you want to keep using your class a bit like a static class but with a state , you can implement the singleton pattern

http://en.wikipedia.org/wiki/Singleton_pattern

public class mydbClass{ 
    private static mydbClass  _current = new    mydbClass(); 
    public static mydbClass Current{
        get{
            return _current;
        }
    } 
    private mydbClass(){} 
    public User getUserName(userid){
    //be sure to create a new connection each times
    }
}

The advantage here is that you can simply implement interface in this class, break dependencies and then mock it for testing purpose.

Anyway, you'll always have to create a new connection at each request do not create a static connection object.

Sign up to request clarification or add additional context in comments.

Comments

1

You definitely should not use a static class for the connections. If you use a static class then you have to worry about being thread safe because of all the threads using the same class to talk to the database. Just create new database connections each time, and use connection pooling. Connection pooling will make creating new connections each time much faster.

1 Comment

I didn't downvote, but I'll take a guess as to why it happened. The only time you have to worry about whether the method is threadsafe is if it uses variables / objects not declared within the scope of the method itself. If you have a static method that creates and disposes of all the variables it uses (including the db connection) then there are no worries. Point is, if it doesn't maintain or use state at all then there are zero concerns.
1

It depends on what you're doing inside the methods.

If, within the method, you open a new connection, use it, and dispose of it, you're fine. No harm, no foul. So long as you're not maintaining state, you're good to go.

On the other hand, if you're maintaining state, you've got problems, as thread-safety enters the picture. In that case, you're much better off just creating a class that's designed to be instantiated.

3 Comments

Pay very close attention to the second paragraph of Mike's answer. When you declare a class as static in an ASP.NET it crosses ALL requests for ALL users throughout the lifetime of the application, not just your user's session. That's a wicked can of worms to open if you don't really need to...
Correct me if I'm wrong The problem is not the static method, but the static field If I call a static method, but it does not use any static object So there is no problem even if it calls by million Threads, and no bottleneck is created But if I write a static method that update a static field or object, so I have a problem because it's not threads safety By the way, also a non static method can update a static object In short, as long as the static method does not use a static object there is no problem using it on a Web site with lots of users Am I right? micha
Any update to a static object/property needs to be done via a lock, MSDN. If the static method only works with variables local to the method, you are safe.
0

I would advice static class, if you see small number of concurrent users querying the Static class.A static class can easily handle sufficient requests serially (say a 20 + in a second). The database routine is more likely bottleneck, depending on the query.

Take care of thread safety**

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.