Doctrine2 connection timeout in daemon

后端 未结 5 1549

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条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-30 02:47

    I had the same problem with a PHP Gearman worker and Doctrine 2.

    The cleanest solution that I came up with is: just close and reopen the connection at each job:

    entitymanager;
       $em->getConnection()->close();
       $em->getConnection()->connect();
    }
    

    Update

    The solution above doesn't cope with transaction status. That means the Doctrine\DBAL\Connection::close() method doesn't reset the $_transactionNestingLevel value, so if you don't commit a transaction, that will lead to Doctrine not being in sync on the translation status with the underlying DBMS. This could lead to Doctrine silently ignoring begin/commit/rollback statements and eventually to data not being committed to the DBMS.

    In other words: be sure to commit/rollback transactions if you use this method.

提交回复
热议问题