Rohan Warwar
Rohan Warwar

Reputation: 850

there is no active transaction in catch rollback

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?

Upvotes: 0

Views: 4389

Answers (1)

Rohan Warwar
Rohan Warwar

Reputation: 850

I solved my problem:

$this->em->getConnection()->beginTransaction(); // suspend auto-commit
$this->em->getConnection()->setAutoCommit(false);

Upvotes: 4

Related Questions