由于第一次用PHP和ADODB,感觉怕有问题,结果测试后,果然有问题。不知道是不是我哪个环境变量没有设置。

解决方案 »

  1.   

    你在回滚之后就要马上进行终止程序运行。$conn->StartTrans(); 
        $conn->Execute($sql); 
        $conn->Execute($Sql2);
        $conn->FailTrans(); 
    exit(); ********注意此处 我没你的程序,不知道你如何处理,你一看应该会明白     $conn->CompleteTrans(); 
      

  2.   

    那如此说来,官方文档写得是有错误了,使用exit(); 是强制程序断开连接,那么连接断开事务没有提交当然也就回滚了。
    但是官方文档明明白白的写到
       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(). 
    也就是说,如果强制事务失败,那么无须退出,可以回滚事务。
    可怜我写了一堆代码,测试事务时才发现有问题,现在得改回用语句方式开始,结束事务。这样自己放心一点
      你的方式我想肯定行。不过我怀疑是不是要设置相关的环境变量原因。如果这么大的问题都是稳定的库,那我想没有人敢用了。
      

  3.   

    严重关注...ADODB的事务,等我改好代码,去ADODB的官方网站投诉,这样的做法对用户太不负责了。
      

  4.   

    晕....................去好好看看他的手册--------------------------------------------------
    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. 
      

  5.   

    我晕,上面的方式我都试过,如果不是我的环境有问题,就是ADODB有问题
    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.
      

  6.   

    我用的mysql数据库,使用的是innodb表
      

  7.   

    如果我通过单独执行
    $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); 方式执行,肯定不对。
      

  8.   

    严重关注,这几天我被这个东东稿的头痛,同样的代码,在oracle下就可以,在mysql下就不行,表的类型也设为innodb,我想可能是adodb有问题,不过明明它的文档中都说明可以
      

  9.   

    楼主,不知你现在搞清了没有,可以,请告诉一声:[email protected]
      

  10.   

    哈哈,我稿清了,是我们自己没有理解清楚,adodb是可以支持msql的事务管理,不过它说明书是说支持mysqlt或maxsql,所以我们只要把连接数据库的类型改为:mysqlt或maxsql即可,而不是mysql.在window状态下mysql和mysqlt\maxsql是一样的,当然版本要高此,我用是4.0以上的,不过adodb是分别处理的.不知adodb新的版本会不会自动判断处理.