【问题标题】:How to make a distributed transactions in Laravel Eloquent orm or DB facades?如何在 Laravel Eloquent orm 或 DB facades 中进行分布式事务?
【发布时间】:2017-07-12 02:24:14
【问题描述】:

我在 Laravel 中找不到像 XA 这样的 API,而且似乎在 Eloquent 或 DB 外观中没有实现。

如果我想在 Laravel 中使用 XA 或其他方式进行 MySQL 分布式事务,我该怎么做?

【问题讨论】:

    标签: php mysql laravel transactions xa


    【解决方案1】:

    如果你不需要使用分布式事务,就不要使用它。

    试着考虑一下你的数据库结构设计是否正确?

    如果你还需要它。

    我觉得你需要自己写;

    第 1 步: 对于第一个数据库,您需要为其 XA 事务生成一个唯一 ID。并尝试执行sql,成功则运行XA PREPARE your_xa_id

    第 2 步:对于另一个数据库,您需要执行与第一步相同的操作;

    第三步:检查第一步和第二步是否成功,提交XA事务,或者全部回滚。

    一些代码看起来像这样。

    $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']);
    }
    
    // Insert into Database
    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);
        }
    }
    
    // Update Database1
    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);
       }
    }
    
    // Commit
    public function commitdb($xa){
        return $this->_db->query("XA COMMIT '$xa'");
    }
    
    public function commitdb1($xa){
        return $this->db1->query("XA COMMIT '$xa'");
    }
    
    // Rollback
    public function rollback($xa){
        return $this->db->query("XA ROLLBACK '$xa'");
    }
    
    public function rollbackdb1($xa){
         return $this->db1->query("XA ROLLBACK '$xa'");
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 2017-12-07
      • 2017-07-17
      • 2012-09-24
      • 1970-01-01
      相关资源
      最近更新 更多