4

I cannot find APIs like XA in Laravel, and seems there's no implementation in Eloquent or DB facades.

If I want to use XA or other way to make a MySQL distributed transaction in Laravel, how should I do?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
chenxinlong
  • 1,677
  • 2
  • 15
  • 30

1 Answers1

1

If you don't need to use distributed transactions, don't use it.

Try to considerate that the design of your database structure is right?

If you still need it.

I think you need to write it by yourself;

Step 1: for the first database, you need to generate a unique id to its XA transaction. and try to execute the sql, run XA PREPARE your_xa_id if success;

Step 2: for another database, you need to do the same things like the first step;

Step 3: check if the first step and second step is success, commit the XA transaction, or rollback all.

Somecode looks like this.

$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'");
}
TsaiKoga
  • 12,914
  • 2
  • 19
  • 28