改进Connection类

This commit is contained in:
thinkphp
2016-05-31 17:37:04 +08:00
parent 923c6657ad
commit bf59fd8097

View File

@@ -38,8 +38,6 @@ abstract class Connection
protected $lastInsID; protected $lastInsID;
// 返回或者影响记录数 // 返回或者影响记录数
protected $numRows = 0; protected $numRows = 0;
// 事务的数据库连接
protected $transPDO;
// 事务指令数 // 事务指令数
protected $transTimes = 0; protected $transTimes = 0;
// 事务标识 // 事务标识
@@ -52,6 +50,8 @@ abstract class Connection
/** @var PDO 当前连接ID */ /** @var PDO 当前连接ID */
protected $linkID; protected $linkID;
protected $linkRead;
protected $linkWrite;
// 查询结果类型 // 查询结果类型
protected $resultSetType = Db::RESULTSET_ARRAY; protected $resultSetType = Db::RESULTSET_ARRAY;
@@ -568,9 +568,6 @@ abstract class Connection
if (0 == $this->transTimes) { if (0 == $this->transTimes) {
$this->transLabel = $label; $this->transLabel = $label;
$this->linkID->beginTransaction(); $this->linkID->beginTransaction();
if (1 == $this->config['deploy']) {
$this->transPDO = $this->linkID;
}
} }
$this->transTimes++; $this->transTimes++;
return null; return null;
@@ -585,13 +582,11 @@ abstract class Connection
*/ */
public function commit($label = '') public function commit($label = '')
{ {
$this->initConnect(true);
if ($this->transTimes > 0 && $label == $this->transLabel) { if ($this->transTimes > 0 && $label == $this->transLabel) {
try { try {
$this->linkID->commit(); $this->linkID->commit();
$this->transTimes = 0; $this->transTimes = 0;
if (1 == $this->config['deploy']) {
$this->transPDO = null;
}
} catch (\PDOException $e) { } catch (\PDOException $e) {
throw new PDOException($e, $this->config, $this->queryStr); throw new PDOException($e, $this->config, $this->queryStr);
} }
@@ -607,13 +602,11 @@ abstract class Connection
*/ */
public function rollback() public function rollback()
{ {
$this->initConnect(true);
if ($this->transTimes > 0) { if ($this->transTimes > 0) {
try { try {
$this->linkID->rollback(); $this->linkID->rollback();
$this->transTimes = 0; $this->transTimes = 0;
if (1 == $this->config['deploy']) {
$this->transPDO = null;
}
} catch (\PDOException $e) { } catch (\PDOException $e) {
throw new PDOException($e, $this->config, $this->queryStr); throw new PDOException($e, $this->config, $this->queryStr);
} }
@@ -820,12 +813,17 @@ abstract class Connection
protected function initConnect($master = true) protected function initConnect($master = true)
{ {
if (!empty($this->config['deploy'])) { if (!empty($this->config['deploy'])) {
if ($this->transPDO) { // 采用分布式数据库
// 使用事务连接 if ($master) {
$this->linkID = $this->transPDO; if (!$this->linkWrite) {
$this->linkWrite = $this->multiConnect(true);
}
$this->linkID = $this->linkWrite;
} else { } else {
// 采用分布式数据库 if (!$this->linkRead) {
$this->linkID = $this->multiConnect($master); $this->linkRead = $this->multiConnect(false);
}
$this->linkID = $this->linkRead;
} }
} elseif (!$this->linkID) { } elseif (!$this->linkID) {
// 默认单数据库 // 默认单数据库