在繁忙的网站上使用单例连接到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; 
}

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 !

Even if you write that, each PHP request will still be a different connection. Which is what you want.

It doesn't really matter from a MySQL performance standpoint whether or not you use singletons in this case as each request to that page will create its own object (singleton or not) and connection.