[英]Too many connection when extending Phalcon\Db\Adapter\Pdo\Mysql
我试图扩展Phalcon\\Db\\Adapter\\Pdo\\Mysql
以在每次查询函数返回false
时创建日志。
即使我没有创建任何新的连接,除了parent::__construct
我还是收到以下异常:
Fatal error: Uncaught PDOException: SQLSTATE[08004] [1040] Too many connections in ...Internal/Database/Mysql.php:14 Stack trace: #0 [internal function]: PDO->__construct('mysql:adapter=M...', '...', '...', Array) #1 [internal function]: Phalcon\Db\Adapter\Pdo->connect(Array) #2 .../Internal/Database/Mysql.php(14): Phalcon\Db\Adapter\Pdo->__construct(Array) #3 .../apps/bootstrap/app.php(378): Internal\Database\Mysql->__construct(Array) #4 [internal function]: Closure->{closure}() #5 [internal function]: Phalcon\Di\Service->resolve(NULL, Object(Phalcon\Di\FactoryDefault)) #6 .../apps/bootstrap/core_services.php(7): Phalcon\Di->get('logs') #7 [internal function]: Closure->{closure}() #8 [internal function]: Phalcon\Di\Service->resolve(NULL, Object(Phalcon\Di\FactoryDefault)) #9 .../apps/libs/Internal/Database/Mysql.php(15): Phalcon\Di->get('logger') #10 .../apps/bootstrap/app.php(37 in .../apps/libs/Internal/Database/Mysql.php on line 14
码:
namespace Internal\Database;
use Phalcon\Db\Adapter\Pdo\Mysql as PhalconMysql;
use Phalcon\Di;
class Mysql extends PhalconMysql
{
public $isLogger = false;
public function __construct(array $descriptor)
{
parent::__construct($descriptor);
$this->oLogger = Di::getDefault()->get('logger');
}
public function query($sqlStatement, $bindParams = null, $bindTypes = null)
{
$oResult = parent::query($sqlStatement, $bindParams, $bindTypes);
if ($oResult === false && $this->isLogger === false) {
$trace = debug_backtrace();
$aCaller = array_shift($trace);
$sFile = $aCaller['file'];
$sLine = $aCaller['line'];
$this->oLogger->error('MySQL query failed. File: ' . $sFile . ', Line: ' . $sLine, ['error' => $this->getErrorInfo()]);
}
return $oResult;
}
}
触发错误的行是parent::__construct($descriptor);
。
我所做的唯一其他更改是用Internal\\Database\\Mysql
替换了Phalcon\\Db\\Adapter\\Pdo\\Mysql
的用法。 所有连接的创建方式均与使用Phalcon\\Db\\Adapter\\Pdo\\Mysql
。
我看了Phalcon\\Db\\Adapter\\Pdo\\Mysql
的父类。 我看到创建的连接的唯一地方是在这里
非常感谢您在此问题上的帮助。
扩展phalcon中的类是不正确的,而phalcon实际上是与其他php扩展交互的。 很难学到它。
我建议的简单方法是将Mvc / Model类重写为例如。 ModelCommon或BaseModel,并在那里实现Model::initialize
和Model::afterFetch
。
但是正确的方法是使用在Phalcon中开发的事件机制。 诀窍是附加到beforeQuery
和/或afterQuery
事件。
快捷方式,在DI中完成:
$di->set('db', function() use ($di) {
$config = $di->getConfig();
$connection = new \Phalcon\Db\Adapter\Pdo\Mysql([
// ...
]);
$eventsManager->attach('db', function($event, $connection, $params) use ($config) {
if ($event->getType() == 'beforeQuery') {
// ...
}
if ($event->getType() == 'afterQuery') {
// ...
}
});
$connection->setEventsManager($eventsManager);
return $connection;
});
看起来模块正在尝试多次连接数据库。 也许要确保只在Module.php
初始化一次数据库服务
在$ di中声明db时,也许可以将db服务共享
$di->setShared('db', function() {
// return your db instance
});
问题解决了,尽管我仍然不明白为什么会首先发生。
记录器记录到数据库,但是由于它取自Di,因此我假设它会重用现有的连接。 从构造函数中删除该行,并在调用记录器后立即将其解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.