I inherited a samopisny project.
If you increase the number of visits, you create too many connections to the database and it hangs up other sites on the Mysql server (there are several of them). The max_connections connection limit was previously increased to 200 (default 151). The same guys used to log connections, which suggests that the problem lies precisely here. At peak times, up to 200 new connections per minute are created in the log (connection logging code is below). The pieces of code responsible for connecting to the database:
private static $_connectionLink = null; private static $_connectionErrors = null; public function __construct() { if (empty(self::$_connectionLink)) { self::connect(); $handle = fopen('connect.log','a'); fputs($handle,"----------- NEW connect ".date('Ymd H:i')."----------- \n"); fclose($handle); } } private static function connect() { $host = 'localhost'; $user = 'user'; $password = 'pass'; $dbName = 'dbname'; self::$_connectionLink = @mysqli_connect($host,$user,$password,$dbName); if (!mysqli_connect_errno()) { mysqli_set_charset(self::$_connectionLink, 'utf8'); } else { } }
Special attention to the line: if (empty(self::$_connectionLink))
-
Question: Is it correct in this case to check the presence of an already open connection using empty ()?
Maybe there is an error here and in the case of using the self :: $ _ connectionLink construction empty () still returns TRUE even when the variable is not empty and then a new connection is created even if there is an active unclosed? Maybe you need to do something like if( null === self::$_connectionLink )
or if (!self::$_connectionLink)
? Or what else could be the reason for the large number of open connections in this case?
p:
when specifying the host to open a permanent connection? php.net/manual/ru/mysqli.construct.php - naym