laravel数据库事务会锁定表吗?
我使用laravel5.5的数据库交易进行在线支付应用。我有一个company_account表来记录每笔付款(,,,)。。我需要在创建新记录时访问最后一条记录。所以我需要在交易时锁定表,用读写表锁,避免多次支付同时进行。type
amount
create_at
gross_income
gross_income
我已经参考了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);