由于第一次用PHP和ADODB,感觉怕有问题,结果测试后,果然有问题。不知道是不是我哪个环境变量没有设置。
解决方案 »
- [探讨]PHP中foreach与for的对比以及语句块带来的性能消耗
- /ShowKey?ecms 请问这个地址代表什么意思,有什么用,谢谢了
- js 二个input 如果一个有值另一个输入值时提示错误
- 求教一个不知道怎么解决的问题!
- Google.cn 的ICP证?
- 页面返回遇到的问题
- 新闻系统生成一个新的静态html文章后,其文章目录是否自动重新生成还是手动重新生成?
- 新手来讨论一下,高手来帮一下。这样的问题不知道大家碰到过没有?
- 请问哪里有JSP和PHP的留言版下载啊?知道的朋友请告诉我,谢谢!!!!
- 如何实现这种避免表单反复提交的方法?
- 请问数组如何保存到数据库中??????????
- 图片问题
$conn->Execute($sql);
$conn->Execute($Sql2);
$conn->FailTrans();
exit(); ********注意此处 我没你的程序,不知道你如何处理,你一看应该会明白 $conn->CompleteTrans();
但是官方文档明明白白的写到
CompleteTrans() detects when an SQL error occurs, and will Rollback/Commit as appropriate. To specificly force a rollback even if no error occured, use FailTrans(). Note that the rollback is done in CompleteTrans(), and not in FailTrans().
也就是说,如果强制事务失败,那么无须退出,可以回滚事务。
可怜我写了一堆代码,测试事务时才发现有问题,现在得改回用语句方式开始,结束事务。这样自己放心一点
你的方式我想肯定行。不过我怀疑是不是要设置相关的环境变量原因。如果这么大的问题都是稳定的库,那我想没有人敢用了。
BeginTrans( )
Begin a transaction. Turns off autoCommit. Returns true if successful. Some databases will always return false if transaction support is not available. Among the databases that support transactions are Oracle, PostgreSQL, Interbase, MSSQL, certain versions of MySQL, DB2, Informix, Sybase, etc. Any open transactions will be rolled back when the connection is closed.The following is the wrong way to use transactions: $DB->BeginTrans();
$DB->Execute("update table1 set val=$val1 where id=$id");
$DB->Execute("update table2 set val=$val2 where id=$id");
$DB->CommitTrans(); because you perform no error checking. It is possible to update table1 and for the update on table2 to fail. Here is a better way: $DB->BeginTrans();
$ok = $DB->Execute("update table1 set val=$val1 where id=$id");
if ($ok) $ok = $DB->Execute("update table2 set val=$val2 where id=$id");
if ($ok) $DB->CommitTrans();
else $DB->RollbackTrans(); Another way is (since ADOdb 2.0): $DB->BeginTrans();
$ok = $DB->Execute("update table1 set val=$val1 where id=$id");
if ($ok) $ok = $DB->Execute("update table2 set val=$val2 where id=$id");
$DB->CommitTrans($ok); You can also use the ADOdb error handler to die and rollback your transactions for you transparently. Some buggy database extensions are known to commit all outstanding tranasactions, so you might want to explicitly do a $DB->RollbackTrans() in your error handler for safety.
Smart Transactions
The old way of doing transactions required you to use $conn->BeginTrans();
$ok = $conn->Execute($sql);
if ($ok) $ok = $conn->Execute($sql2);
if (!$ok) $conn->RollbackTrans();
else $conn->CommitTrans(); This is very complicated for large projects because you have to track the error status. Smart Transactions is much simpler. You start a smart transaction by calling StartTrans(): $conn->StartTrans();
$conn->Execute($sql);
$conn->Execute($Sql2);
$conn->CompleteTrans(); CompleteTrans() detects when an SQL error occurs, and will Rollback/Commit as appropriate. To specificly force a rollback even if no error occured, use FailTrans(). Note that the rollback is done in CompleteTrans(), and not in FailTrans().
You can also check if a transaction has failed, using HasFailedTrans(), which returns true if FailTrans() was called, or there was an error in the SQL execution. Make sure you call HasFailedTrans() before you call CompleteTrans(), as it is only works between StartTrans/CompleteTrans. $conn->StartTrans();
$conn->Execute($sql);
if (!CheckRecords()) $conn->FailTrans();
$conn->Execute($Sql2);
$conn->CompleteTrans(); Lastly, StartTrans/CompleteTrans is nestable, and only the outermost block is executed. In contrast, BeginTrans/CommitTrans/RollbackTrans is NOT nestable. $conn->StartTrans();
$conn->Execute($sql);
$conn->StartTrans(); # ignored
if (!CheckRecords()) $conn->FailTrans();
$conn->CompleteTrans(); # ignored
$conn->Execute($Sql2);
$conn->CompleteTrans(); Note: Savepoints are currently not supported.
$conn->Execute('begin');
if($conn->Execute($sql1))print '成功1';
if($conn->Execute($sql2))print '成功2';
$conn->Execute('rollback');
这样做,就执行回滚了,而且是正确的,
但是使用
$conn->BeginTrans();
$ok = $conn->Execute("update table1 set val=$val1 where id=$id");
if ($ok) $ok = $conn->Execute("update table2 set val=$val2 where id=$id");
$conn->CommitTrans($ok); 方式执行,肯定不对。