catch回滚中没有活动的事务 [英] there is no active transaction in catch rollback

查看:137
本文介绍了catch回滚中没有活动的事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过,我应该使用方法Commit before persist,但是没有解决我的问题。我也尝试在回滚之前使用取消EntityManager,但是还没有解决我的问题...

  $ this - > EM->的getConnection() - >的BeginTransaction(); // suspend auto-commit 
if($ out = $ this-> em-> getConnection() - > isTransactionActive()){
var_dump($ out);
}
尝试{
// ...做一些工作
$ 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();
}

在try block isTransactionActive()返回给我之前,但在catch块中方法把我弄错了
如何解决?

解决方案

我解决了我的问题:

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


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?

解决方案

I solved my problem:

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

这篇关于catch回滚中没有活动的事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆