Actually about MySQL transaction (OST MySQL statement), I have mentioned in my blog (MySQL article classification)
Today look at the processing of ORM based on Laravel framework
Get ready:
The table must be a InnoDB engine
Db::BeginTransaction ();Try{ $name= ' abc '; $result 1= Test::create ([' name ' = = =$name]); if(!$result 1) { /** * The parameters received by the Exception class * $message = "", $code = 0, Exception $previous = null*/ Throw New\Exception("1"); } $result 2= Test::create ([' name ' = = =$name]); if(!$result 2) { Throw New\Exception("2"); } DB::commit ();} Catch(\Exception $e) {DB:: Rollback ();//Transaction Rollback Echo $e-GetMessage (); Echo $e-GetCode ();}
Note: If the ID is self-increment, the MySQL primary key is maintained in memory, and the transaction rollback is not a fallback ID, so there is a fault in the middle
Ext.: https://www.cnblogs.com/xj76149095/p/6222066.html
Laravel Road (transaction) MySQL transaction