Skip to content
This repository has been archived by the owner on Jan 4, 2020. It is now read-only.

Commit

Permalink
让数据库事务操作更准确
Browse files Browse the repository at this point in the history
1. 使用事务时只选取一个数据库连接
2. 嵌套事务只在最外层提交
  • Loading branch information
You Ming committed Apr 19, 2016
1 parent e1934a8 commit 03ebce7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
20 changes: 17 additions & 3 deletions ThinkPHP/Library/Think/Db/Driver.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ abstract class Driver
protected $lastInsID = null;
// 返回或者影响记录数
protected $numRows = 0;
// 事物操作PDO实例
protected $transPDO = null;
// 事务指令数
protected $transTimes = 0;
// 错误信息
Expand Down Expand Up @@ -276,6 +278,8 @@ public function startTrans()

//数据rollback 支持
if (0 == $this->transTimes) {
// 记录当前操作PDO
$this->transPdo = $this->_linkID;
$this->_linkID->beginTransaction();
}
$this->transTimes++;
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -1188,6 +1197,11 @@ protected function debug($start)
*/
protected function initConnect($master = true)
{
// 开启事物时用同一个连接进行操作
if ($this->transPDO) {
return $this->transPDO;
}

if (!empty($this->config['deploy']))
// 采用分布式数据库
{
Expand Down
24 changes: 19 additions & 5 deletions ThinkPHP/Library/Think/Db/Lite.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Lite
protected $lastInsID = null;
// 返回或者影响记录数
protected $numRows = 0;
// 事物操作PDO实例
protected $transPDO = null;
// 事务指令数
protected $transTimes = 0;
// 错误信息
Expand Down Expand Up @@ -248,6 +250,8 @@ public function startTrans()

//数据rollback 支持
if (0 == $this->transTimes) {
// 记录当前操作PDO
$this->transPdo = $this->_linkID;
$this->_linkID->beginTransaction();
}
$this->transTimes++;
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -353,7 +362,7 @@ public function error()
// 记录错误日志
trace($this->error, '', 'ERR');
if ($this->config['debug']) {
// 开启数据库调试模式
// 开启数据库调试模式
E($this->error);
} else {
return $this->error;
Expand Down Expand Up @@ -421,7 +430,7 @@ public function setModel($model)
protected function debug($start)
{
if ($this->config['debug']) {
// 开启数据库调试模式
// 开启数据库调试模式
if ($start) {
G('queryStartTime');
} else {
Expand All @@ -442,6 +451,11 @@ protected function debug($start)
*/
protected function initConnect($master = true)
{
// 开启事物时用同一个连接进行操作
if ($this->transPDO) {
return $this->transPDO;
}

if (!empty($this->config['deploy']))
// 采用分布式数据库
{
Expand Down

1 comment on commit 03ebce7

@hcpzhe
Copy link

@hcpzhe hcpzhe commented on 03ebce7 Oct 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢, 早已发现此问题, 在使用中很麻烦; 一直没有时间去改写.
感谢更新

Please sign in to comment.