我将一个类从非框架的php库移植到Zend Framework 2.该类具有在多个数据库和多个服务器上运行输入查询的方法。我们有5台服务器,每台服务器有5到100多个PostgreSQL数据库。它适用于低级postgres调用,但使用\ Zend \ Db \ Adapter \ Adapter似乎导致问题,因为连接没有被释放。这是代码:
foreach($params['databases'] as $database):
$db_conn['database'] = trim($database);
$dsn = "pgsql:dbname=" . $db_conn['database'] .
";host=" . $db_conn['hostname'] .
";port=" . $db_conn['port'];
$db = array(
'driver' => 'Pdo',
'dsn' => $dsn,
'username' => $db_conn['username'],
'password' => $db_conn['password'],
);
$adapter = new \Zend\Db\Adapter\Adapter($db);
$connection = null;
$connect_count = 0;
$connected = false;
while (!$connected && $connect_count < 5) {
try {
$connect_count ++;
$connection = $adapter->getDriver()->getConnection();
$connection->connect();
$connected = true;
} catch (RuntimeException $e) {
sleep(2);
echo("\n<br>RuntimeException {$connect_count}: {$db_conn['database']} {$e->getCode()} {$e->getMessage()}");
}
}
try {
if (self::checkTablesExist($args['tables_exist'], $connection)) {
$results[trim($database)] = @$connection->execute(self::$arguments['query']);
} else {
log::error(__FUNCTION__." Missing tables", $db_conn['hostname']." : ".$db_conn['database']." : ".join(",", $args['tables_exist']));
}
} catch (PDOException $e) {
echo('\n<br>PDOException : ' . $e->getCode() . " " . $e->getMessage());
} catch (RuntimeException $e) {
echo("\n<br>RuntimeException : {$db_conn['database']} {$e->getCode()} {$e->getMessage()}");
} catch (Exception $e) {
echo('\n<br>Exception : ' . $e->getCode() . " " . $e->getMessage());
}
$connection->disconnect();
endforeach;
一切正常,直到第50个数据库,然后我开始
&#34; RuntimeException:7连接错误:SQLSTATE [08006] [7]致命:抱歉,已有太多客户&#34;
我使用sleep()
语句添加了循环,这有点帮助。现在它已经达到了抛出异常的程度,而前几个它可能只会经历2到3次,但之后它又回到了没有为下几个数据库建立任何连接。
所以似乎$connection->disconnect()
根本没有工作,连接仍然没有被释放,至少没有及时发布。我可以在代码的那一部分中跳过使用Zend Framework,它可以工作,但我想这样做&#34;规范&#34;,所以它不会像那样凌乱。