您的位置:首页 > 数据库 > MySQL

mysql pdo 事务嵌套

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

<?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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: