From 03ebce77aed95c01777d15121aae84804ae2a9a5 Mon Sep 17 00:00:00 2001 From: You Ming Date: Tue, 19 Apr 2016 16:22:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A9=E6=95=B0=E6=8D=AE=E5=BA=93=E4=BA=8B?= =?UTF-8?q?=E5=8A=A1=E6=93=8D=E4=BD=9C=E6=9B=B4=E5=87=86=E7=A1=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 使用事务时只选取一个数据库连接 2. 嵌套事务只在最外层提交 --- ThinkPHP/Library/Think/Db/Driver.class.php | 20 +++++++++++++++--- ThinkPHP/Library/Think/Db/Lite.class.php | 24 +++++++++++++++++----- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/ThinkPHP/Library/Think/Db/Driver.class.php b/ThinkPHP/Library/Think/Db/Driver.class.php index 761a08e77..dc45c1162 100644 --- a/ThinkPHP/Library/Think/Db/Driver.class.php +++ b/ThinkPHP/Library/Think/Db/Driver.class.php @@ -28,6 +28,8 @@ abstract class Driver protected $lastInsID = null; // 返回或者影响记录数 protected $numRows = 0; + // 事物操作PDO实例 + protected $transPDO = null; // 事务指令数 protected $transTimes = 0; // 错误信息 @@ -276,6 +278,8 @@ public function startTrans() //数据rollback 支持 if (0 == $this->transTimes) { + // 记录当前操作PDO + $this->transPdo = $this->_linkID; $this->_linkID->beginTransaction(); } $this->transTimes++; @@ -289,13 +293,17 @@ public function startTrans() */ public function commit() { - if ($this->transTimes > 0) { - $result = $this->_linkID->commit(); + if ($this->transTimes == 1) { + // 由嵌套事物的最外层进行提交 + $result = $this->_linkID->commit(); $this->transTimes = 0; + $this->transPdo = null; if (!$result) { $this->error(); return false; } + } else { + $this->transTimes--; } return true; } @@ -308,8 +316,9 @@ public function commit() public function rollback() { if ($this->transTimes > 0) { - $result = $this->_linkID->rollback(); + $result = $this->_linkID->rollback(); $this->transTimes = 0; + $this->transPdo = null; if (!$result) { $this->error(); return false; @@ -1188,6 +1197,11 @@ protected function debug($start) */ protected function initConnect($master = true) { + // 开启事物时用同一个连接进行操作 + if ($this->transPDO) { + return $this->transPDO; + } + if (!empty($this->config['deploy'])) // 采用分布式数据库 { diff --git a/ThinkPHP/Library/Think/Db/Lite.class.php b/ThinkPHP/Library/Think/Db/Lite.class.php index ad6ef16ed..8afc496c6 100644 --- a/ThinkPHP/Library/Think/Db/Lite.class.php +++ b/ThinkPHP/Library/Think/Db/Lite.class.php @@ -28,6 +28,8 @@ class Lite protected $lastInsID = null; // 返回或者影响记录数 protected $numRows = 0; + // 事物操作PDO实例 + protected $transPDO = null; // 事务指令数 protected $transTimes = 0; // 错误信息 @@ -248,6 +250,8 @@ public function startTrans() //数据rollback 支持 if (0 == $this->transTimes) { + // 记录当前操作PDO + $this->transPdo = $this->_linkID; $this->_linkID->beginTransaction(); } $this->transTimes++; @@ -261,13 +265,17 @@ public function startTrans() */ public function commit() { - if ($this->transTimes > 0) { - $result = $this->_linkID->commit(); + if ($this->transTimes == 1) { + // 由嵌套事物的最外层进行提交 + $result = $this->_linkID->commit(); $this->transTimes = 0; + $this->transPdo = null; if (!$result) { $this->error(); return false; } + } else { + $this->transTimes--; } return true; } @@ -280,8 +288,9 @@ public function commit() public function rollback() { if ($this->transTimes > 0) { - $result = $this->_linkID->rollback(); + $result = $this->_linkID->rollback(); $this->transTimes = 0; + $this->transPdo = null; if (!$result) { $this->error(); return false; @@ -353,7 +362,7 @@ public function error() // 记录错误日志 trace($this->error, '', 'ERR'); if ($this->config['debug']) { -// 开启数据库调试模式 + // 开启数据库调试模式 E($this->error); } else { return $this->error; @@ -421,7 +430,7 @@ public function setModel($model) protected function debug($start) { if ($this->config['debug']) { -// 开启数据库调试模式 + // 开启数据库调试模式 if ($start) { G('queryStartTime'); } else { @@ -442,6 +451,11 @@ protected function debug($start) */ protected function initConnect($master = true) { + // 开启事物时用同一个连接进行操作 + if ($this->transPDO) { + return $this->transPDO; + } + if (!empty($this->config['deploy'])) // 采用分布式数据库 {