多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
事务不生效,检查步骤: 1.在事务里操作的表是否都是InnnoDb引擎 2.数据库是否默认连接【多库】 3.是否调用DB的beginTransaction commit rollBack等方法,默认模型是不支持事务的 我这里就是因为出现多库,导致事务不生效,因为以上选项3的方法,默认都是连接config/database.php的mysql默认配置。索引所以如果多库项目,这里三个方法都要显示连接当前事务所在的库,如下 ``` public static function taskSuccess($taskId, $totalNums=0, $successNums=0, $note=''){ DB::connection(self::$connectionSelf)->beginTransaction(); $currentDate = date('Y-m-d H:i:s'); try{ # 主表更新 $where = [ ['id', '=', $taskId] ]; $update = [ 'status'=> self::STATUS['success'], 'update_time'=> $currentDate, 'total_nums'=> $totalNums, 'success_nums'=> $successNums, 'note'=> $note, ]; $update = self::where($where)->update($update); if(!$update){ throw new \Exception('任务执行成功,但更新主表状态失败', ErrorCode::DB_UPDATE); } # 从表更新 $where = [ ['last_task_id', '=', $taskId] ]; $update = [ 'status'=> self::STATUS['success'], 'update_time'=> $currentDate, 'note'=> $note, ]; $update = SyncAccountQueue::where($where)->update($update); if(!$update){ throw new \Exception('任务执行成功,但更新从表状态失败', ErrorCode::DB_UPDATE); } DB::connection(self::$connectionSelf)->commit(); return true; }catch(\Exception $e){ DB::connection(self::$connectionSelf)->rollBack(); throw new \Exception($e->getMessage(), $e->getCode()); } } ```