dougou8639 2017-02-08 15:08
浏览 19

刷新失败后恢复Doctrine连接

When a entityManager does a flush and this flush throws an exception. The doctrine entity manager will catch this exception clear the unit of work and mark the connection as closed and then doing the rollback (https://github.com/doctrine/doctrine2/blob/v2.5.6/lib/Doctrine/ORM/UnitOfWork.php#L412-L417).

I need to restore the connection but as there is no method with does $this->closed = false in the entityManager I'm confused how to handle this that after a failed flush I can flush another entity to mark this to mark in the database a specific task as failed! Also I can not run multiple tasks when a previous one failed the other can not run when when using the entityManager.

TL;DR So what is the best practice to restore the connection after a failed flush? As I see it is just marked as closed and is still connected but I dont want to use something dirty like Reflection or an custom entity manager to restore my connection.

  • 写回答

2条回答 默认 最新

  • duankan6894 2017-02-08 15:17
    关注

    You can use the Function resetManager Like This $this->getDoctrine()->resetManager()
    more detail in this link

    edit

    lets say Exemple its a table has a date field As DateTime and not null

        $Exemple = new Exemple();
        $Exemple->setDate(null);
        $em = $this->getDoctrine()->getManager();
        try {
            $em->persist($Exemple);
            $em->flush();
        } catch (\Exception $ex) {
            $em = $this->getDoctrine()->resetManager();
        }
        $Exemple->setDate(new \DateTime());
        $em->persist($Exemple);
        $em->flush();
    

    whith $this->getDoctrine()->resetManager(); the data its flushed in database
    without $this->getDoctrine()->resetManager(); we get The EntityManager is closed.

    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口