Let me help you to see how database transactions are optimized? -Php Tutorial

Source: Internet
Author: User
It is very convenient for many ORM to capture exceptions and roll back, but I don't think the fundamental problem has been solved yet. do you have to determine whether the orm operation is successful for each SQL statement, in addition, the ORM is not standard. sometimes the execution fails and an error is thrown, and sometimes false is returned. I really don't understand it. every step must be...

It is very convenient for many ORM to capture exceptions and roll back, but I don't think the fundamental problem has been solved yet. do you have to determine whether the orm operation is successful for each SQL statement, in addition, the ORM is not standard. sometimes the execution fails and an error is thrown, and sometimes false is returned. I really don't understand it. I have to judge the rollback in every step. it's really a crash, every step of the operation should judge the result and roll back. it's really a crash!

Reply content:

It is very convenient for many ORM to capture exceptions and roll back, but I don't think the fundamental problem has been solved yet. do you have to determine whether the orm operation is successful for each SQL statement, in addition, the ORM is not standard. sometimes the execution fails and an error is thrown, and sometimes false is returned. I really don't understand it. I have to judge the rollback in every step. it's really a crash, every step of the operation should judge the result and roll back. it's really a crash!

I think it's your problem...M('Shop')->find($final_shop_id))Is rollback required for such statements? The query does not modify the data, right? if you do not execute rollback, Will dirty data be generated?
So the correct method is for you$this->create || !$this->saveI started the transaction before, and then verified whether it was successful to consider whether to trigger rollback. is it instantly simplified?
In addition, in terms of core logic, as long as you do not execute commit, nothing will change after the php process ends and it will automatically roll back, but the plaintext rollback is a good habit, it can improve the readability of the code and should be changed to persistence.

Supplementary answer:
The query statement and database structure modification language do not accept transaction management. at the same time, when the transaction is enabled, the current mysql process is locked. Therefore, you should enable the transaction as soon as possible and end the transaction as soon as possible, especially when persistent connections or connection pools are used.

Comments:
I usually do this:

Class Model {/*** prompt for the last error */protected $ _ error = false; protected function _ setError ($ error) {$ this-> _ error = $ error; return false;} public function getError ($ emptyError = true) {$ error = $ this-> _ error; if ($ emptyError) $ this-> _ error = false; return $ error;} public function example (param1, param2 ...) {if (false) {return $ this-> _ setError ('your message or translate key ');}... // Core logic if all parameters is valid $ this-> beginTrans (); try {// update, insert, delete data ...} catch (\ Exception $ e) {$ this-> rollback (); return $ this-> _ setError ($ e-> getError ());} return $ this-> commit ();}}

In fact, you can usetry catchThe code is as follows:

public function final_shop_order($final_shop_id, $order_id){    try {        $this->startTrans();                if (!$final_shop_info = M('Shop')->find($final_shop_id)) {            throw new \Exception('Your are message');            }                if (.....) {            throw new \Exception('Your are message');         }                ......                $this->commit();        return true;        } catch (\Exception $e) {        $this->rollback();        return false;    }    }

The code style is problematic. you can use do... while (0) to set the flag and errstr and break when an error occurs in the middle. then, based on the returned value, you can determine whether it is commit or rollback.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.