Pdo事务澄清与提交


pdo transaction clarification with commit

刚开始使用事务,在网上找不到明确的答案。在本例中,我意识到query1和query2都必须成功,否则将运行catch…

在commit() (更多的代码在这个例子中)之后输入的代码是否只在事务发生时运行?

try {
    $db->beginTransaction();
    query1
    query2
    $db->commit();
    **more code here**
}
catch(Exception $e)
{   
    $db->rollback();
    some error message
}

如果抛出异常,try{}块中的剩余代码将不会执行

http://php.net/manual/en/language.exceptions.php

当抛出异常时,语句后面的代码将不被处理执行,PHP将尝试查找第一个匹配的catch块。