How does php+mysql solve the problem of getting stuck in the middle when operating on multiple tables?

system Information
php 7.1
MariaDB 10
Laravel 5.6
centos 7

when users place an order using the balance, they will have a series of table operations:
1, update the balance field of the user table
2, insert data
3 into the balance record table, call the third-party API to send the balance change notification (Wechat template notification)
4, create the order
5, and call the third-party API to send the order success notification (Wechat template notification)

has already used the database transaction of Laravel. After deducting the number of the balance field in step 1, the user gets stuck and then exits the program. The record in step 2 and the data behind are gone. How to solve this situation?

Mar.21,2021

if the program exits abnormally, there will be a log. Can you post it


I haven't used Laravel, but the solution is the same, bro. I'll give you a pseudo code:

$transaction = (new Transaction());

try {
    $setBalanceResult = UserModel::model()->set('balance',-100.00);
    
    if (!$setBalanceResult) throw new \Exception('');
    
    $createBalanceRecord = BalanceRecord::model()->create([
        'user_id' => $user_id,
        'content' => 'XXX100'
    ]);
    if (!$createBalanceRecord) throw new \Exception('');
    
    $order = Order::model()->create([
        // 
    ]);
    if (!$order) throw new \Exception('');
    
    //  start
    (new Queue())
        ->sendMsg('')
        ->sendMsg('');
    //  start 
    $transaction->commit(); //    
} catch(\Exception $e) {
    $transaction->rollback(); // 
    var_dump($e->getMessage()); // 
}
Menu