Doctrine Laravel not closing connections after trying to close them
So there is (or at least was, but the issue for me persists) o problem with amount of connections to MySql DB when running tests that use DB or many php processes (workers that have several connections to DB).
After a process is finished it leaves some hanging connections to DB and soon you hit Too many connections exception.
There is one main solution – to close them manully in the code like so:
$doctrine = $this->getDoctrine();
foreach ($doctrine->getManagers() as $name => $entityManager) {
$entityManager->close();
$connection->close();
}
Well in my case, since i use Laravel-Doctrine ORM:
foreach ($this->managerRegistry->getManagers() as $manager) {
$manager->getConnection()->close();
$manager->close();
}
However my code snipped just simply does not work. When i check the amount of connections through MySql client SHOW PROCESSLIST; the connections are still growing.
I even tried to put “closing actions” in a recursive function to exit only when the connection is really closed. Yes, i am totally aware that this approach is hacky and dumb but im trying everything to close those connections.
Heres the snippet:
private function terminate() {
echo "Amount of managers: " . count($this->managerRegistry->getManagers());
foreach ($this->managerRegistry->getManagers() as $manager) {
$manager->getConnection()->close();
$manager->close();
}
echo "Amount of connections: " . count($this->managerRegistry->getConnections());
foreach ($this->managerRegistry->getConnections() as $connection) {
$connection->close();
}
gc_collect_cycles();
if (count($this->managerRegistry->getManagers()) > 0 || count($this->managerRegistry->getConnections()) > 0) {
sleep (1);
$this->terminate();
}
}
What is going on? What do i do?
from Laravel Questions and Answers https://laravelquestions.com/php/doctrine-laravel-not-closing-connections-after-trying-to-close-them/
via Lzo Media
No comments:
Post a Comment