use Illuminate\Support\Facades\DB;
try{
DB::transaction(function () use ($data) {
# 获取用户信息,加悲观锁,其他的不可读写
$userInfo = UserProfile::query()->where(['uid' => $data['apprentice_uid']])->lockForUpdate()->first(); # 加锁
# 第一个sql操作
User::query()->where(['id' => 1])->increment('exchange_coupon', 10);
throw new \Exception('异常,回退');
# 第二个sql操作
$TradeHistory = new TradeHistory();
$TradeHistory->is_pay = Config::$TRUE;
$TradeHistory->save();
# 第三个sql操作
$Apprentice = new Apprentice();
$Apprentice->is_send = $data['is_send'];
$Apprentice->save();
return true;
});
}catch (\Throwable $ex){
return false;
}
声明:
本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。