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->save
I 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
catch
The 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.