[英]Too many connection while using a persistent connection
I have a small blog with more than 400 online visitors per minutes. 我有一个小型博客,每分钟有400多位在线访问者。
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! 另外,我更改了我的
MySQL
服务配置,并将max_connection
值从150增加到500,但问题仍然存在!
Any ideas to how avoid from this error!? 任何想法如何避免此错误!?
make it 做了
PDO::ATTR_PERSISTENT => FALSE
most likely this will be enough. 这很有可能就足够了。
Also make sure you are calling DatabaseConnection::connect() only ONCE 还要确保只在一次调用DatabaseConnection :: connect()
In such type of application the better approach would be not to use a persistent connection at all. 在这种类型的应用程序中,更好的方法是根本不使用持久连接。 If the MySQL server is on the same machine, the benefit of the persistent connection is minor.
如果MySQL服务器在同一台计算机上,则持久连接的好处很小。
This has been already discussed here https://stackoverflow.com/a/5323928/1800369 这已经在这里讨论过https://stackoverflow.com/a/5323928/1800369
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.