记TP3.2.5 BUG:There is no active transaction

最近有个项目用到了最新的TP3.2.5,发现有个报错:There is no active transaction,后来定位分析到是TP代码bug,由于官方已经不维护且无法提交issue,故做此记录。

 

测试代码:

 1 try {
 2     $uModel = M('Users');
 3     $uModel->startTrans();
 4     $userInfo = $uModel->where(['user_id' => 0])->find();
 5     if (empty($userInfo)) {
 6         throw new \Exception('用户不存在');
 7     }
 8 } catch (\Exception $e) {
 9     $uModel->rollback();
10     var_dump($e->getMessage());
11 }

BUG分析:

在 /ThinkPHP/Library/Think/Db/Driver.class.php 里,startTrans 是把 $this->_linkID 存到 $this->transPdo,而 query 调用 initConnect 判断事务连接却是使用 $this->transPDO,导致 $this->_linkID 是新连接丢失了事务,一 rollback 就报错。(注意大小写

 

posted @ 2020-03-25 14:05  andy_chan  阅读(628)  评论(0编辑  收藏  举报