XA的性能很低,但是没得选的时候,也是个方案
<?PHP
$dbtest1 = new mysqli("127.0.0.1","public","public","dbtest1")or die("dbtest1 连接失败");
$dbtest2 = new mysqli("127.0.0.1public","public","dbtest2")or die("dbtest2 连接失败");
//为XA事务指定一个id,xid 必须是一个唯一值。
$xid = uniqid("");
//两个库指定同一个事务id,表明这两个库的操作处于同一事务中
$dbtest1->query("XA START '$xid'");//准备事务1
$dbtest2->query("XA START '$xid'");//准备事务2
try {
//$dbtest1
$return = $dbtest1->query("UPDATE member SET name='张三' WHERE id=1") ;
if($return == false) {
throw new Exception("库[email protected]执行update member操作失败!");
}
//$dbtest2
$return = $dbtest2->query("UPDATE memberpoints SET point=point+10 WHERE memberid=1") ;
if($return == false) {
throw new Exception("库[email protected]执行update memberpoints操作失败!");
}
//阶段1:$dbtest1提交准备就绪
$dbtest1->query("XA END '$xid'");
$dbtest1->query("XA PREPARE '$xid'");
//阶段1:$dbtest2提交准备就绪
$dbtest2->query("XA END '$xid'");
$dbtest2->query("XA PREPARE '$xid'");
//阶段2:提交两个库
$dbtest1->query("XA COMMIT '$xid'");
$dbtest2->query("XA COMMIT '$xid'");
}
catch (Exception $e) {
//阶段2:回滚
$dbtest1->query("XA ROLLBACK '$xid'");
$dbtest2->query("XA ROLLBACK '$xid'");
die($e->getMessage());
}
$dbtest1->close();
$dbtest2->close();
?>
标签:xid,跨库,XA,dbtest1,dbtest2,return,mysql,query
From: https://www.cnblogs.com/qcy-blog/p/17833323.html