I have a small blog with more than 400 online visitors per minutes.
Since there is many connection request needed I'm using a persistent connection to reuse it when it's possible, here is my connection class:
<?php
class DatabaseConnection {
var $currCon;
public function connect() {
require_once '/config.php';
try {
$this->currCon = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_DATABASE, DB_USER, DB_PASSWORD, array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::ATTR_PERSISTENT => true
));
} catch (Exception $e) {
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 300');
die("Can't connect to MySQL database<br />: " . $e);
}
}
public function disconnect() {
// Unused for PERSISTENT connections
// $this->currCon = null;
// unset($this->currCon);
}
public function getDatabaseConnection() {
return $this->currCon;
}
}
?>
The problem is that I got many errors which said Too many connection and That's interesting because I'm using persistent connection model!
Also I changed my MySQL
service config and increasing max_connection
value from 150 to 500 but the problem still persist!
Any ideas to how avoid from this error!?