同時操作不同庫裡表,支援事物嗎,mysql解決辦法

來源:互聯網
上載者:User
同時操作不同庫裡表,支援事物嗎,mysql
MYSQL資料庫

有資料庫A和資料庫B,同時操作A中兩張表和B中兩張表,支援事物復原麼,都是innodb。。

分兩種情況
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 "
";
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/
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.