我在在线支付应用中使用laravel5.5的数据库事务。我有一个company_account表来记录每一笔付款(类型,金额,创建时间,总收入)。当创建新记录时,我需要访问上一条记录的总收入(gross_income)。因此,我需要在事务中使用读写表锁(lock),以避免多笔付款同时进行。
我参考了laravel的文档,但不确定事务是否会锁定表。如果事务将锁定表格,那么锁定类型是什么(读锁、写锁或两者都有)?
我参考了laravel的文档,但不确定事务是否会锁定表。如果事务将锁定表格,那么锁定类型是什么(读锁、写锁或两者都有)?
DB::transaction(function () {
// create company_account record
// create use_account record
}, 5);
代码:
DB::transaction(function ($model) use($model) {
$model = Model::find($order->product_id);
$user = $model->user;
// **update** use_account record
try {
$user_account = User_account::find($user->id);
} catch (Exception $e){
$user_account = new User_account;
$user_account->user_id = $user->id;
$user_account->earnings = 0;
$user_account->balance = 0;
}
$user_account->earnings += $order->fee * self::USER_COMMISION_RATIO;
$user_account->balance += $order->fee * self::USER_COMMISION_RATIO;
$user_account->save();
// **create** company_account record
$old_tiger_account = Tiger_account::latest('id')->first();
$tiger_account = new Tiger_account;
$tiger_account->type = 'model';
$tiger_account->order_id = $order->id;
$tiger_account->user_id = $user->id;
$tiger_account->profit = $order->fee;
$tiger_account->payment = 0;
$tiger_account->gross_income = $old_tiger_account-> gross_income + $order->fee;
$tiger_account->save();
}, 5);