Home >Backend Development >PHP Tutorial >同时操作不同库里表,支持事物吗,mysql

同时操作不同库里表,支持事物吗,mysql

WBOY
WBOYOriginal
2016-06-20 12:45:301115browse

MYSQL数据库

有数据库A和数据库B,同时操作A中两张表和B中两张表,支持事物回滚么,都是innodb。。

分两种情况
A库和B库在同一台服务器
A库和B库不在同一台服务器

请教大神


回复讨论(解决方案)

A库和B库在同一台服务器 应该是支持的

A库和B库不在同一台服务器 不可能同时访问到,自然也就没有事务

A库和B库在同一台服务器 应该是支持的

A库和B库不在同一台服务器 不可能同时访问到,自然也就没有事务

A库和B库在同一台服务器
A库和B库不在同一台服务器
两个都支持事务。

可以使用分布式事务

public function testAction(){        $goods_id=1;        $goods_name = "大西瓜";        $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 "<br />";        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 = false;            }        } 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 goods_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("status"=>"nosuccess","XA"=>$XA);         }        }    //提交事务!    public function commitdb($xa){        return $this->_db->query("XA COMMIT '$xa'");    }        //回滚事务    public function rollbackdb($xa){        return $this->_db->query("XA ROLLBACK '$xa'");    }        //提交事务!    public function commitdb1($xa){        return $this->db1->query("XA COMMIT '$xa'");    }        //回滚事务    public function rollbackdb1($xa){        return $this->db1->query("XA ROLLBACK '$xa'");    }

参考: http://javalifuqing.blog.163.com/blog/static/83699035201341645329839/

如果 最后提交阶段 A提交成功,B提交失败   怎么办 

回滚。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn