如題:ThinkPHP v3.2.3 數據庫讀寫分離,開啟事務時報錯:
ERR: There is no active transaction
剛開始以為是數據表引擎不對造成的,因為 有幾張表的引擎是 MyISAM,后來調整過來了,改為 InnoDB,還是報錯。應該是TP 底層連數據庫時,切換數據庫連接時出了點問題,解決方案如下:
打開文件 ThinkPHP/Library/Think/Db/Driver.class.php,調整內容如下:
/** * 啟動事務 * * @access public * @return void */ public function startTrans() { $this->initConnect(true); if ( !$this->_linkID ) return false; // 數據rollback 支持 if ($this->transTimes == 0) { //$this->_linkID->beginTransaction(); // by 52php.cnblogs.com foreach ($this->linkID as $_linkId) { $_linkId->beginTransaction(); } } $this->transTimes++; return ; } /** * 用於非自動提交狀態下面的查詢提交 * * @access public * @return boolean */ public function commit() { if ($this->transTimes > 0) { //$result = $this->_linkID->commit(); // by 52php.cnblogs.com foreach ($this->linkID as $_linkId) { $result = $_linkId->commit(); } $this->transTimes = 0; if(!$result){ $this->error(); return false; } } return true; } /** * 事務回滾 * * @access public * @return boolean */ public function rollback() { if ($this->transTimes > 0) { //$result = $this->_linkID->rollback(); // by 52php.cnblogs.com foreach ($this->linkID as $_linkId) { $result = $_linkId->rollback(); } $this->transTimes = 0; if(!$result){ $this->error(); return false; } } return true; }
問題解決!