【发布时间】:2010-08-10 11:01:49
【问题描述】:
我有一个关于在事务中测试查询的问题。我使用 MySQL 事务已经有一段时间了,每次我这样做时,我都会使用类似的东西:
$doCommit = true;
$error = "";
mysql_query("BEGIN");
/* repeat this part with the different queries in the transaction
this often involves updating of and inserting in multiple tables */
$query = "SELECT, UPDATE, INSERT, etc";
$result = mysql_query($query);
if(!$result){
$error .= mysql_error() . " in " . $query . "<BR>";
$doCommit = false;
}
/* end of repeating part */
if($doCommit){
mysql_query("COMMIT");
} else {
echo $error;
mysql_query("ROLLBACK");
}
现在,经常发生我想测试我的事务,所以我将mysql_query("COMMIT"); 更改为mysql_query("ROLLBACK");,但我可以想象这不是测试这类东西的好方法。将每个表复制到 temp_table 并更新并插入这些表并在之后删除它们通常是不可行的(例如,因为表可能非常大)。当然,当代码投入生产时,相关的错误处理(而不仅仅是打印错误)就会到位。
做这样的事情最好的方法是什么?
【问题讨论】:
-
天哪...刚发现 PHP 不支持
finally或ensure之类的东西。 -
是的,我知道... PHP 很烂,我用它...
-
现在支持
finally,从 PHP 5.5 开始
标签: mysql debugging transactions