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;
    }
}

猜你喜欢

转载自blog.csdn.net/b1303110335/article/details/77980289