From 11769ed15c10ad95eff8a16ac6a9e72bb4285882 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 29 Apr 2016 14:41:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E4=B8=BB=E4=BB=8E=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E7=9A=84=E4=BA=8B=E5=8A=A1=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Connection.php | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 69cd7a9d..51919fbb 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -35,6 +35,8 @@ abstract class Connection protected $lastInsID; // 返回或者影响记录数 protected $numRows = 0; + // 事务的数据库连接 + protected $transPDO; // 事务指令数 protected $transTimes = 0; // 事务标识 @@ -450,6 +452,9 @@ abstract class Connection if (0 == $this->transTimes) { $this->transLabel = $label; $this->linkID->beginTransaction(); + if (1 == $this->config['deploy']) { + $this->transPDO = $this->linkID; + } } $this->transTimes++; return; @@ -467,6 +472,9 @@ abstract class Connection try { $this->linkID->commit(); $this->transTimes = 0; + if (1 == $this->config['deploy']) { + $this->transPDO = null; + } } catch (\PDOException $e) { throw new PDOException($e, $this->config, $this->queryStr); } @@ -485,6 +493,9 @@ abstract class Connection try { $this->linkID->rollback(); $this->transTimes = 0; + if (1 == $this->config['deploy']) { + $this->transPDO = null; + } } catch (\PDOException $e) { throw new PDOException($e, $this->config, $this->queryStr); } @@ -505,13 +516,13 @@ abstract class Connection return false; } // 自动启动事务支持 - $this->startTrans(); + $this->startTrans(NOW_TIME); try { foreach ($sql as $_sql) { $result = $this->execute($_sql); } // 提交事务 - $this->commit(); + $this->commit(NOW_TIME); } catch (\PDOException $e) { $this->rollback(); return false; @@ -689,8 +700,13 @@ abstract class Connection protected function initConnect($master = true) { if (!empty($this->config['deploy'])) { - // 采用分布式数据库 - $this->linkID = $this->multiConnect($master); + if ($this->transPDO) { + // 使用事务连接 + $this->linkID = $this->transPDO; + } else { + // 采用分布式数据库 + $this->linkID = $this->multiConnect($master); + } } elseif (!$this->linkID) { // 默认单数据库 $this->linkID = $this->connect();