Laravel 框架下 DB 事务提交后, 数据丢失了怎么解决?
class PaymentController extends Controller
{
//....
public function store(CreateRequest $request)
{
$attributes = $request->input();
$paymentConfig = PaymentConfig::findOrFail($attributes['payment_config_id']);
$order = Order::byNumber($attributes['order_number'])->firstOrFail();
$content = \DB::transaction(function () use ($attributes, $paymentConfig, $order) {
$currency_code = 'CNY';
$amount = $attributes['amount'];
$paymentTransaction = new PaymentTransaction();
$paymentTransaction->payment_config_id = $paymentConfig->id;
$paymentTransaction->order_id = $order->id;
$paymentTransaction->currency_code = $currency_code;
$paymentTransaction->amount = $amount;
$paymentTransaction->details = [];
$paymentTransaction->state = PaymentTransaction::TRANSITION_REQUEST_PAYMENT;
if (!$paymentTransaction->save()) {
throw new HttpException(500, 'Save payment transaction failed.');
}
$response_content = $this->paymentManager
->setPayment($paymentConfig)
->orderRequest($order, $amount, $currency_code);
if (!$response_content) {
throw new HttpException(500, 'Request payment transaction failed.');
}
$details = ['response' => $response_content];
event(new Created($paymentTransaction, $details));
return $response_content;
});
return [
'data' => $content
];
}
}
以上是我在为订单创建支付方式的时候的代码.
客户端拿到了我的响应, 但是我数据库里面对应的记录居然不见了, 而且对应的自增主键也跳过了, 直接从415 变成了 417, 416的数据找不到, 但是客户端是正确获取到我的响应了.
整个事务里面就做了一个插入支付方式流水的操作, 中间有做锁表操作.
应用程序的log里面也没看到对应时间段内的异常
这种现象也不是平凡发生, 目前就碰到一次, 烦请有经验的朋友指点一下.
推荐文章: