dongle7637 2011-11-04 02:15
浏览 30
已采纳

在繁忙的网站上使用单例连接到MySql

I'm wondering if there are any negative performance issues associated with using a Singleton class to connect to MySQL database. In particular I'm worried in the amount of time it will take to obtain a connection when the website is busy. Can the singleton get "bogged down"?

public static function obtain($server=null, $user=null, $pass=null, $database=null){
   if (!self::$instance){ 
    self::$instance = new Database($server, $user, $pass, $database); 
  } 

    return self::$instance; 
}
  • 写回答

3条回答 默认 最新

  • dtef9322 2011-11-04 02:27
    关注

    You can use a singleton to handle your database connection because even in only one request, your app may send several database queries and you will loose performance if you re-open database connections each time.

    But keep in mind that you always have to write clever queries and request your database only for the data you need and as few times as possible. That will make it smooth !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?