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

Commit 03ebce7

Browse files
author
You Ming
committed
让数据库事务操作更准确
1. 使用事务时只选取一个数据库连接 2. 嵌套事务只在最外层提交
1 parent e1934a8 commit 03ebce7

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

ThinkPHP/Library/Think/Db/Driver.class.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ abstract class Driver
2828
protected $lastInsID = null;
2929
// 返回或者影响记录数
3030
protected $numRows = 0;
31+
// 事物操作PDO实例
32+
protected $transPDO = null;
3133
// 事务指令数
3234
protected $transTimes = 0;
3335
// 错误信息
@@ -276,6 +278,8 @@ public function startTrans()
276278

277279
//数据rollback 支持
278280
if (0 == $this->transTimes) {
281+
// 记录当前操作PDO
282+
$this->transPdo = $this->_linkID;
279283
$this->_linkID->beginTransaction();
280284
}
281285
$this->transTimes++;
@@ -289,13 +293,17 @@ public function startTrans()
289293
*/
290294
public function commit()
291295
{
292-
if ($this->transTimes > 0) {
293-
$result = $this->_linkID->commit();
296+
if ($this->transTimes == 1) {
297+
// 由嵌套事物的最外层进行提交
298+
$result = $this->_linkID->commit();
294299
$this->transTimes = 0;
300+
$this->transPdo = null;
295301
if (!$result) {
296302
$this->error();
297303
return false;
298304
}
305+
} else {
306+
$this->transTimes--;
299307
}
300308
return true;
301309
}
@@ -308,8 +316,9 @@ public function commit()
308316
public function rollback()
309317
{
310318
if ($this->transTimes > 0) {
311-
$result = $this->_linkID->rollback();
319+
$result = $this->_linkID->rollback();
312320
$this->transTimes = 0;
321+
$this->transPdo = null;
313322
if (!$result) {
314323
$this->error();
315324
return false;
@@ -1188,6 +1197,11 @@ protected function debug($start)
11881197
*/
11891198
protected function initConnect($master = true)
11901199
{
1200+
// 开启事物时用同一个连接进行操作
1201+
if ($this->transPDO) {
1202+
return $this->transPDO;
1203+
}
1204+
11911205
if (!empty($this->config['deploy']))
11921206
// 采用分布式数据库
11931207
{

ThinkPHP/Library/Think/Db/Lite.class.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Lite
2828
protected $lastInsID = null;
2929
// 返回或者影响记录数
3030
protected $numRows = 0;
31+
// 事物操作PDO实例
32+
protected $transPDO = null;
3133
// 事务指令数
3234
protected $transTimes = 0;
3335
// 错误信息
@@ -248,6 +250,8 @@ public function startTrans()
248250

249251
//数据rollback 支持
250252
if (0 == $this->transTimes) {
253+
// 记录当前操作PDO
254+
$this->transPdo = $this->_linkID;
251255
$this->_linkID->beginTransaction();
252256
}
253257
$this->transTimes++;
@@ -261,13 +265,17 @@ public function startTrans()
261265
*/
262266
public function commit()
263267
{
264-
if ($this->transTimes > 0) {
265-
$result = $this->_linkID->commit();
268+
if ($this->transTimes == 1) {
269+
// 由嵌套事物的最外层进行提交
270+
$result = $this->_linkID->commit();
266271
$this->transTimes = 0;
272+
$this->transPdo = null;
267273
if (!$result) {
268274
$this->error();
269275
return false;
270276
}
277+
} else {
278+
$this->transTimes--;
271279
}
272280
return true;
273281
}
@@ -280,8 +288,9 @@ public function commit()
280288
public function rollback()
281289
{
282290
if ($this->transTimes > 0) {
283-
$result = $this->_linkID->rollback();
291+
$result = $this->_linkID->rollback();
284292
$this->transTimes = 0;
293+
$this->transPdo = null;
285294
if (!$result) {
286295
$this->error();
287296
return false;
@@ -353,7 +362,7 @@ public function error()
353362
// 记录错误日志
354363
trace($this->error, '', 'ERR');
355364
if ($this->config['debug']) {
356-
// 开启数据库调试模式
365+
// 开启数据库调试模式
357366
E($this->error);
358367
} else {
359368
return $this->error;
@@ -421,7 +430,7 @@ public function setModel($model)
421430
protected function debug($start)
422431
{
423432
if ($this->config['debug']) {
424-
// 开启数据库调试模式
433+
// 开启数据库调试模式
425434
if ($start) {
426435
G('queryStartTime');
427436
} else {
@@ -442,6 +451,11 @@ protected function debug($start)
442451
*/
443452
protected function initConnect($master = true)
444453
{
454+
// 开启事物时用同一个连接进行操作
455+
if ($this->transPDO) {
456+
return $this->transPDO;
457+
}
458+
445459
if (!empty($this->config['deploy']))
446460
// 采用分布式数据库
447461
{

0 commit comments

Comments
 (0)