【发布时间】:2014-06-07 20:37:30
【问题描述】:
我需要将多个值放入 2 个数据库中。问题是,如果其中一个 INSERTS 失败,我需要所有其他的来回滚。
问题
是否可以同时进行两个事务,将一些值插入数据库,然后Commit 或rollback 两者都进行?
守则
$res = new ResultSet(); //class connecting and letting me query first database
$res2 = new ResultSet2(); //the other database
$res->query("BEGIN");
$res2->query("BEGIN");
try
{
$res->query("INSERT xxx~~") or wyjatek('rollback'); //wyjatek is throwing exception if query fails
$res2->query("INSERT yyy~~")or wyjatek('rollback');
......
//if everything goes well
$res->query("COMMIT");
$res2->query("COMMIT");
//SHOW some GREEN text saying its done.
}
catch(Exception $e)
{
//if wyjatek throws an exception
$res->query("ROLLBACK");
$res2->query("ROLLBACK");
//SHOW some RED text, saying it failed
}
总结 那么它是正确的方法,还是会起作用?
感谢所有提示。
【问题讨论】:
标签: php postgresql transactions try-catch