there is no active transaction in catch rollback

2019-06-13 08:07发布

I have read that I should use method Commit before persist, but that didn't solved my problem. I also try to use cancel EntityManager before rollback as well, but that didn't solved my problem too...

$this->em->getConnection()->beginTransaction(); // suspend auto-commit
    if($out = $this->em->getConnection()->isTransactionActive()) {
        var_dump($out);
    }
    try {
        //... do some work
        $history = new PromocodeHistory();
        $history->setCode($code);
        $history->setUser($this->getUser());
        $this->em->persist($history);
        $this->em->getConnection()->commit();
        $this->em->flush();

        $user = $this->getUser();
        $updatedBalance = $user->getBalance() + $code->getSum() - $sum;
        $user->setBalance($updatedBalance);
        $this->em->persist($user);
        $this->em->getConnection()->commit();
        $this->em->flush();

        $payment = new Payment();
        $payment->setSum($sum);
        $payment->setUser($this->getUser());
        $payment->setPromocode($code);

        ....
    } catch (\Exception $e) {
        if(!$out = $this->em->getConnection()->isTransactionActive()) {
            var_dump($out);
        }
        $this->em->getConnection()->rollBack();
    }

Before try block isTransactionActive() returned me true, but in the catch block this method retrurn me false. How to fix it?

1条回答
一夜七次
2楼-- · 2019-06-13 08:46

I solved my problem:

$this->em->getConnection()->beginTransaction(); // suspend auto-commit
$this->em->getConnection()->setAutoCommit(false);
查看更多
登录 后发表回答