mysql pdo 事务嵌套

经常会在处理事务的时候出现多个事务嵌套的情况,这个时候最好有一个处理嵌套事务的封装类来处理,就不需要去修改业务代码来兼容这样的问题了。

<?php

class database extends PDO
{
    protected $_transTimes = 0;


    public function beginTransaction()  
    {
        ++$this->_transTimes;
        if ($this->_transTimes == 1) {
            return parent::beginTransaction();
        }

        $this->exec('SAVEPOINT trans' . $this->_transTimes);
        return $this->_transTimes >= 0;
    }

    public function commit()
    {
        --$this->_transTimes;
        if ($this->_transTimes == 0) {
            return parent::commit();
        }

        return $this->_transTimes >= 0;
    }

    public function rollback()
    {
        --$this->_transTimes;
        if ($this->_transTimes == 0) {
            return parent::rollback();
        }

        $this->exec('ROLLBACK TO trans' .$this->_transTimes + 1);
        return true;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值