Home  >  Article  >  Backend Development  >  Solving problems encountered in php database transactions

Solving problems encountered in php database transactions

不言
不言forward
2019-04-13 11:38:052822browse

The content of this article is about solving problems encountered in PHP database transactions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

A problem discovered when using the PDO extension of php. After the transaction is opened, if the connection between php and mysql is disconnected, it will cause php to directly record a warning exception instead of throwing it directly. The process of throwing an Exception is as follows:

/**
 * 一个用户财产变更的场景下
 */

try {
    // 1. 开启事务

    /**
     * 2. 变更用户财产,增加财产变更的流水记录
     */

    // 3. 提交事务
} catch (\Exception $e) {
    // 4. 回滚事务

    // 5. 记错误日志

    // 6. 抛出异常
}

// 7. 发布用户财产变更的广播

The above operations can be simply divided into five categories. In my previous understanding, the general process of operating transactions is as above, without exceptions thrown. The transaction is submitted successfully

But until one day the database is abnormal, a transaction has been started, and during the process of 1-2 above, the database hangs directly, then when the transaction is submitted in step 3 A

warning
level error appears directly, "SQLSTATE[HY000]: General error: 2006 MySQL server has gone away",No exception is caughtSo in the follow-up to step 7 During the step, other business parties obtained the unsubmitted transaction ID and made statistics, but in fact the user's property did not increase. This led to the problem
When I was puzzled, I looked at the documentation and found a bug that has been left for a long time: https://bugs.php.net/bug.php?...

Later we solved the problem by temporarily configuring

set_error_handler

at the location of the transaction

The above is the detailed content of Solving problems encountered in php database transactions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete