Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Yii2.0 merchant withdrawal function of MYSQL transaction course

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Preface

I am a half-way PHP programmer, so far, not counting the time I studied in the training class, I have been writing code for two years. Probably due to business reasons, I haven't used MySQL transactions in these two years. Just yesterday, a business about Alipay transfer had to use MySQL transactions to complete. Others said a lot, but they still didn't understand what MySQL transactions were, so they started a new round of make-up lessons and came out to muddle through, and the knowledge debt owed always had to be repaid.

Let's briefly talk about the business I encountered yesterday. I'm going to launch an Alipay cash withdrawal service on the mobile side. I wrote this business in three steps before:

Step one:

First of all, if I withdraw cash, I will first write a withdrawal record in the withdrawal form, and then update the user balance table. The records of these two pieces of data are both pre-withdrawal records, that is to say, their status is being processed.

Step 2:

Then request the third-party interface of Alipay.

Step 3:

If the interface request is successful and the user has received the money paid by Alipay, we are updating the status of the withdrawal record table and the data of the user balance table.

If the interface request fails and the user does not receive the money transferred to the user by Alipay, the status of the withdrawal record table is withdrawal failure.

However, if the user initiates the withdrawal, the user pre-withdrawal service has been carried out, but the request for the third-party Alipay interface fails, and the power is suddenly cut off at this time, then the database status cannot be changed, and then the result is not what we expected. At this point, MySQL transactions show their value in this kind of business.

MySQL transaction: a series of operations to be performed are called transactions, and transaction management is to manage these operations either completely or not, that is to say, all of the cash withdrawal business above me will be executed successfully or unsuccessfully, and there will be no half of the execution. The purpose of MySQL transaction is to ensure the integrity of data. But not all data engines in mysql support transaction management, only innodb supports transaction management.

Characteristics of transaction management:

1. Atomicity: the whole operation of a transaction is a whole and cannot be divided. Either all succeed or all fail.

two。 Consistency: the data in the data table does not change before and after the transaction operation.

3. Isolation: transaction operations are isolated from each other.

4. Persistence: once the data is submitted, it is immutable and permanently changes the data in the data table.

Transaction management operations:

Turn on transaction management: once turned on, the following sql statement is not immediately executed and the results are written to the table, but to the transaction log.

Start transaction

Fallback operation: fallback clears what is written to the transaction log after the start of transaction management, that is, before transaction management is started.

Syntax: rollback

Note: the fallback operation is only a fallback of the "write" content, not for ordinary meter reading select statements.

Transaction commit: writes the results of the sql statement to the data table.

Syntax: commit

Sample code:

/ * Notes: merchant Alipay withdrawal * @ Author: Dang Mengmeng * @ Date: 2019-7-9 0009 10:01 * @ param $params * @ return mixed * @ throws BadRequestHttpException * / public function storeWith ($params) {try {$payee_account = $params ['pay_account']; $amount = $params [' amount_of_acc']; $request = new AlipayFundTransToaccountTransferRequest (); $connection = Yii::$app- > db; $transaction = $connection- > beginTransaction () / / start transaction / / write initiating withdrawal record $RES = StoreWith::updateWithData ($x_id); $financeId = StoreWith::insertLog ($params); $params ['acc_id'] = $financeId; $paymentId = StoreWith::insertPaymentLog ($params); $request- > setBizContent ("{". "\" out_biz_no\ ": $orderId," "\" payee_type\ ":\" ALIPAY_LOGONID\ ",. "\" payee_account\ ": $payee_account," "\" amount\ ": $amount," "\" remark\ ":\" income withdrawal\ ". "}"); $result = $aop- > execute ($request); $responseNode = str_replace (".", "_", $request- > getApiMethodName ()). "_ response"; $resultCode = $result- > $responseNode- > code; if (! empty ($resultCode) & & $resultCode = = 10000) {/ / withdraw successfully $status = 1; / / 1. Update the status of the recorded fund data StoreWith::updateWithDataTwo ($xarchiidjournal status status);} else {/ / failed withdrawals $status = 2; StoreWith::updateWithDataTwo ($xSecretidjournal status status);} if ($resultCode = = 10000) {$transaction- > commit (); / / transaction commit} else {$transaction- > rollBack (); / / execution failed, transaction rollback} return $resultCode;} catch (\ Exception $exception) {throw new BadRequestHttpException ($exception- > getMessage ());}

The knowledge account owed is always repaid, it is better to know it later than to know it early!

It is better to learn sooner rather than later, and give you a song as soon as possible. Ha!

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.

Original link: https://www.cnblogs.com/honely/p/11156929.html

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report