illuminate/database 事务不生效

714433615@qq.com

问题描述

事务不生效

程序代码或配置

       try {
            Db::beginTransaction();
            //入库
            $isUp = AppClienteleModel::whereId($id)->update($data);
            if (!$isUp) {
                throw new \Exception("档案修改失败!");
            }
            //contact insert
            $contactTemp = [];
            $date = date('YmdHis');
            foreach ($checkRes['ids'] as $contact) {
                if ($contact=="buffer"){
                    throw new \Exception("test手动触发回滚--!".$contact);
                }
                $contactTemp[] = [
                    'type'         => strpos($contact, '@') === false? 1 : 2,
                    'clientele_id' => $id,
                    'admin_id'     => $adminId,
                    'contact'      => $contact,
                    'created_at'   => $date,
                    'updated_at'   => $date,
                ];
            }
            if ($contactTemp) {
                $isInsert = AppClienteleContactModel::insert($contactTemp);
                if (!$isInsert) {
                    throw new \Exception("联系方式入库失败!");
                }
            }
            Db::commit();
        } catch (\Throwable $exception) {
            Db::rollBack();
            throw $exception;
        }

重现问题的步骤

这里写步骤

$checkRes['ids'] 存入一个测试值buffer 手动触发回滚结果并不会滚,修改内容还是入库了!

操作系统环境及workerman/webman等具体版本

"illuminate/database": "^8.83",
Workerman version:4.1.13
PHP version:7.3.33
Event-Loop:\Workerman\Events\Select

698 2 0
2个回答

咸鱼.php

INNODB?

walkor 打赏

如果你AppClienteleModel里指定了connection为xxx,开始事务的时候也要指定connection。
Db::connection(xxx)->beginTransaction();

×
🔝