首页 > 解决方案 > 多重/连接交易原则 - Symfony

问题描述

我想在 Symfony 3 中使用 Doctrine 进行两个连续的事务。第一个事务没问题,但第二个事务似乎正在工作,因为 $insertedRows 总是得到 1 并且没有明显地进行回滚,但是在进行提交时似乎不是应用这最后一个插入。如果我搜索真实数据库中的行不存在。

我在这里留下一个我正在做的例子。

    $db->beginTransaction();
    $db->setAutoCommit(false);

    //the first transaction it's to update a register with another num
    try{
        $num = $this->getNum($params);
        if(!($num > 0)){ $db->rollBack();}

        $updatedRows = $this->updateNum($params);
        if($updatedRows === 0){ $db->rollBack();}

        $db->commit();

    } catch (\Exception $e) {
        $db->rollBack();
        throw $e;
    }
    //until here is ok. I can see the changes in the database


    //this second transatcion I don't know why is not working.
    $db->beginTransaction();
    $db->setAutoCommit(false);
    try{
        $insertedRows = $this->insertNewRegister($params);
        if($insertedRows === 0){ $db->rollBack();}
        //the insertedRows is equal to 1 if I do a dump here, so it seems is inserting this new row 
        //but after the commit if I check this new row in the database is not there

        $db->commit();

    } catch (\Exception $e) {
        $db->rollBack();
        throw $e;
    }

有人知道我在做什么错吗?也许任何人都可以给我看两个连续交易的例子。

标签: symfonytransactionsdoctrine

解决方案


您可以解决删除第二个电话的问题

$db->beginTransaction();

您的第二次调用有问题

$db->beginTransaction();

是在新交易自动打开setAutoCommit(false)后 设置的时候$db->commit();,所以你不需要打开新交易。

您可以阅读文档教义项目


推荐阅读