简体   繁体   English

静态对象访问数据库 - asp.net

[英]static object to access database - asp.net

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 因为如果我不创建新对象所以读取或写入数据库的所有用户将使用相同的静态对象,那么它将非常繁忙,也许它会崩溃我会喜欢答案谢谢micha

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 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. 无论如何,您将始终必须在每个请求创建新连接时不要创建静态连接对象。

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. 连接池将使每次创建新连接的速度更快。

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. 在这种情况下,只需创建一个旨在实例化的类,你就会好得多。

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). 如果您看到少量并发用户查询Static类,我会建议静态类。静态类可以轻松地连续处理足够的请求(例如20 +秒)。 The database routine is more likely bottleneck, depending on the query. 数据库例程更可能是瓶颈,具体取决于查询。

Take care of thread safety** 照顾线程安全**

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM