mysql pdo 事务嵌套

来源:互联网 发布:wephone创始人 知乎 编辑:程序博客网 时间:2024/06/01 10:01

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

<?phpclass 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;    }}
原创粉丝点击