Doctrine2 connection timeout in daemon

后端 未结 5 1540

I have a long running daemon (Symfony2 Command) that gets work off a work queue in Redis, and performs those jobs and writes to the database using the orm.

I noticed

5条回答
  •  萌比男神i
    2020-12-30 02:58

    It appears that whenever there is any error/exception encountered by the EntityManager in Doctrine, the connection is closed and the EntityManager is dead.

    Since generally everything is wrapped in a transaction and that transaction is executed when $entityManager->flush() is called, you can try and catch the exception and attempt to re-excute or give up.

    You may wish to examine the exact nature of the exception with more specific catch on the type, whether PDOException or something else.

    For a MySQL has Gone Away exception, you can try to reconnect by resetting the EntityManager.

    $managerRegistry = $this->getContainer()->get('doctrine');
    $em = $managerRegistry->getEntityManager();
    $managerRegistry->resetEntityManager();
    

    This should make the $em usable again. Note that you would have to re-persist everything again, since this $em is new.

提交回复
热议问题