数据库事务方法添加标识 防止事务嵌套的时候 提前提交

This commit is contained in:
thinkphp
2016-04-28 17:53:20 +08:00
parent d8214cca48
commit d8ac3012fe

View File

@@ -37,6 +37,8 @@ abstract class Connection
protected $numRows = 0; protected $numRows = 0;
// 事务指令数 // 事务指令数
protected $transTimes = 0; protected $transTimes = 0;
// 事务标识
protected $transLabel = '';
// 错误信息 // 错误信息
protected $error = ''; protected $error = '';
// 数据库连接ID 支持多个连接 // 数据库连接ID 支持多个连接
@@ -414,12 +416,12 @@ abstract class Connection
*/ */
public function transaction($callback) public function transaction($callback)
{ {
$this->startTrans(); $this->startTrans(NOW_TIME);
try { try {
if (is_callable($callback)) { if (is_callable($callback)) {
$result = call_user_func_array($callback, []); $result = call_user_func_array($callback, []);
} }
$this->commit(); $this->commit(NOW_TIME);
return $result; return $result;
} catch (\PDOException $e) { } catch (\PDOException $e) {
$this->rollback(); $this->rollback();
@@ -430,9 +432,10 @@ abstract class Connection
/** /**
* 启动事务 * 启动事务
* @access public * @access public
* @param string $label 事务标识
* @return void * @return void
*/ */
public function startTrans() public function startTrans($label = '')
{ {
$this->initConnect(true); $this->initConnect(true);
if (!$this->linkID) { if (!$this->linkID) {
@@ -441,6 +444,7 @@ abstract class Connection
//数据rollback 支持 //数据rollback 支持
if (0 == $this->transTimes) { if (0 == $this->transTimes) {
$this->transLabel = $label;
$this->linkID->beginTransaction(); $this->linkID->beginTransaction();
} }
$this->transTimes++; $this->transTimes++;
@@ -450,11 +454,12 @@ abstract class Connection
/** /**
* 用于非自动提交状态下面的查询提交 * 用于非自动提交状态下面的查询提交
* @access public * @access public
* @param string $label 事务标识
* @return boolen * @return boolen
*/ */
public function commit() public function commit($label = '')
{ {
if ($this->transTimes > 0) { if ($this->transTimes > 0 && $label == $this->transLabel) {
try { try {
$this->linkID->commit(); $this->linkID->commit();
$this->transTimes = 0; $this->transTimes = 0;