1.官方手册是这样介绍的:
想要在一个数据库事务中运行一连串操作,可以使用DB
门面的transaction
方法,如果事务闭包中抛出异常,事务将会自动回滚。如果闭包执行成功,事务将会自动提交。使用transaction
方法时不需要担心手动回滚或提交:
DB::transaction(function () {
DB::table('users')->update(['votes' => 1]);
DB::table('posts')->delete();
});
手动使用事务
如果你想要手动开始事务从而对回滚和提交有一个完整的控制,可以使用DB
门面的beginTransaction
方法:
DB::beginTransaction();
你可以通过rollBack
方法回滚事务:
DB::rollBack();
最后,你可以通过commit
方法提交事务:
DB::commit();
注意:使用DB
门面的事务方法还可以用于控制查询构建器和 Eloquent ORM 的事务。
2.具体是这样使用的:
use Illuminate\Support\Facades\DB;
//开启事务
DB::beginTransaction();
try{
//中间逻辑代码 DB::commit();
}catch (\Exception $e) {
//接收异常处理并回滚 DB::rollBack();
}
处理方法:
方法一、
try {
DB::beginTransaction();
$order = new PaymentOrderModel();
$order->sn = createOrderSn(OrderTypeConst::PAY, $data['user_id']);
$order->channel = $data['channel'];
$order->channel_sn = $data['sn'];
$order->city_id = $data['city_id'];
$order->dispatch_way = $data['dispatch_way'];
$order->ext = isset($data['ext']) ? $data['ext'] : '';
$order->start_pay_time = (new Carbon("now"))->toDateTimeString();
$order->expired_at = $data['expired_at'];
$flag = $order->save();
if (!$flag) throw new LogicException(1031007, ['data' => $data]);
//保存订单商品数据
$flag = $this->createOrderGoods($order);
if (!$flag) throw new LogicException(1031008, ['data' => $data['product']]);
$this->addStatusLog(
[
'order_sn' => $order->sn,
'origin_status' => 0,
'current_status' => PaymentOrderStatusConst::UNPAY,
'user_id' => $order->user_id,
'user_type' => OpUserTypeConst::FRONTEND_USER_TYPE,
'action' => PaymentOrderStatusLogConst::ACTION_TYPE_OF_CREATE,
]
);
DB::commit();
return $order->sn;
} catch (LogicException $e) {
DB::rollBack();
}
方法二、
if( CodeCodeStatusConst::STATUS_SOLD == $codeCodeModel->status && $now->gt($startDate) && $now->lt($endDate) ) {
if($isExchange == true) { //验证电子码时,需要更新状态
//开启事务
CodeCodeModel::query()->getQuery()->getConnection()->beginTransaction();
PaymentSettlementModel::query()->getQuery()->getConnection()->beginTransaction();
$codeCodeModel->status = CodeCodeStatusConst::STATUS_EXCHANGE; // 修改状态
$codeCodeModel->save();
// 记录log
$codeCodeValidationModel = new CodeCodeValidationLogModel();
$codeCodeValidationModel->user_id = 0;
$codeCodeValidationModel->validate_time = Carbon::now()->toDateTimeString();
$codeCodeValidationModel->code_code_id = $codeCodeModel->id;
$codeCodeValidationModel->salesroom_id = $salesroomModel->id;
$codeCodeValidationModel->save();
// 生成付款结算单
$order = $codeCodeModel->codeGoodsOrder;
$data['settlement_merchant_id'] = $salesroomModel->settlement_merchant_id;
$data['order_sn'] = $order->sn;
$data['vendor_sn'] = '';
$data['express'] = 0;
$data['num'] = 1;
$data['detail'] = $order->code_goods_name . ' x 1, ' . //商品名名称x数量,
'券号: ' . $codeCodeModel->code . ', ' . //券号123456
'结算总额: ' . FormatMoney::fen2yuan($data['total']) . '元' . //结算总额20元
'消费场馆: ' . $salesroomModel->name;//消费场馆:xxx场馆
(new SettlementBls())->createPaymentSettlement($data);
CodeCodeModel::query()->getQuery()->getConnection()->commit();
PaymentSettlementModel::query()->getQuery()->getConnection()->commit();
}
// 验证成功
return CodeValidatorConst::SUCCESS;
}