Operate tables in different databases at the same time. does it support transactions? mysql MYSQL database
There are two tables in database A and database B, and two tables in table A and B are operated at the same time. it is innodb that supports transaction rollback ..
There are two situations:
Database A and database B are on the same server
Database A and database B are not on the same server
Consult the gods
Reply to discussion (solution)
Database A and database B should be supported on the same server
Database A and database B cannot be accessed from the same server at the same time, so there is no transaction.
Database A and database B should be supported on the same server
Database A and database B cannot be accessed from the same server at the same time, so there is no transaction.
Database A and database B are on the same server
Database A and database B are not on the same server
Both support transactions.
You can use distributed transactions.
Public function testAction () {$ goods_id = 1; $ goods_name = "Big Watermelon"; $ num = 1; $ rs_order = $ this-> test-> createorder ($ goods_id, $ goods_name, $ num); $ rs_goods = $ this-> test-> deduction ($ goods_id, $ num ); if ($ rs_order ['status'] = "success" & $ rs_goods ['status'] = "success ") {$ this-> test-> commitdb ($ rs_order ['xa ']); $ this-> test-> commitdb1 ($ rs_goods ['xa']);} else {$ this-> test-> rollbackdb ($ rs_order ['xa ']); $ this-> test-> rollbackdb1 ($ rs_goods ['xa']);} print_r ($ rs_order); echo"
"; Print_r ($ rs_goods); die (" dddd ");} public function createorder ($ goods_id, $ goods_name, $ num) {$ XA = uniqid (""); $ this-> _ db-> query ("XA start' $ XA '"); $ _ rs = true; try {$ data = array (); $ data ['Order _ id'] = "V ". date ("YmdHis"); $ data ['goods _ name'] = $ goods_name; $ data ['goods _ num'] = $ num; $ this-> _ db-> insert ("temp_orders", $ data); $ rs = $ this-> _ db-> lastInsertId (); if ($ rs) {$ _ rs = true;} else {$ _ rs = fa Lse ;}} catch (Exception $ e) {$ _ rs = false;} $ this-> _ db-> query ("XA end' $ XA '"); if ($ _ rs) {$ this-> _ db-> query ("xa prepare '$ XA'"); return array ("status" => "success ", "XA" => $ XA);} else {return array ("status" => "nosuccess", "XA" => $ XA );}} public function deduction ($ id) {$ XA = uniqid (""); $ this-> db1-> query ("XA start' $ XA '"); $ last_rs = true; try {$ SQL = "select * from temp_goods where id = '$ ID' and g Oods_num> 0 "; $ rs = $ this-> db1-> fetchRow ($ SQL); if (! Empty ($ rs) {$ SQL = "update temp_goods set goods_num = goods_num-1 where id = '$ ID '"; $ rd = $ this-> db1-> query ($ SQL); if ($ rd) {$ last_rs = true;} else {$ last_rs = false ;}} else {$ last_rs = false ;;}} catch (Exception $ e) {$ last_rs = false ;;} $ this-> db1-> query ("xa end '$ XA'"); if ($ last_rs) {$ this-> db1-> query ("xa prepare '$ XA'"); return array ("status" => "success ", "XA" => $ XA);} else {return array ("sta Tus "=>" nosuccess "," XA "=> $ XA) ;}// submit a transaction! Public function commitdb ($ xa) {return $ this-> _ db-> query ("xa commit '$ xa '");} // ROLLBACK transaction public function rollbackdb ($ xa) {return $ this-> _ db-> query ("xa rollback '$ xa'");} // submit the transaction! Public function commitdb1 ($ xa) {return $ this-> db1-> query ("xa commit '$ xa '");} // ROLLBACK transaction public function rollbackdb1 ($ xa) {return $ this-> db1-> query ("xa rollback '$ xa '");}
Reference: http://javalifuqing.blog.163.com/blog/static/83699035201341645329839/
If the last submission phase A is submitted successfully, what should B do if the submission fails?
Rollback.