【问题标题】:MySQL Transaction+ PHP issue in MysqlMysql 中的 MySQL Transaction+ PHP 问题
【发布时间】:2011-06-15 00:03:07
【问题描述】:

我有一个代码用于我在回滚时遇到问题的应用程序中。即使我 's2' 返回错误的回滚也不会发生,即表 'products' 正在被删除。 谁能解释为什么它不起作用或我应该如何更改它。 注意:表是 Innodb 引擎的..我使用 mysql 5.0+

    mysql_query('SET AUTOCOMMIT=0;');
    mysql_query('START TRANSACTION;');
    $sql = 'DROP TABLE '.$this->Product->tablePrefix.'products';
    $s1 = mysql_query($sql);
    $sql = 'RENAME TABLE '.$this->Product->tablePrefix.'temp12212 TO '.$this->Product->tablePrefix.'products'; 
    $s2 =mysql_query($sql);
    if($s1 && $s2){
        mysql_query('COMMIT;');
        $this->Session->setFlash('Commit Successful to Database');
    }else{
        mysql_query('ROLLBACK;');
        $this->Session->setFlash('Commit failed due to some errors<br> auto-rollbacked to previous state');
    }

【问题讨论】:

    标签: php mysql transactions commit rollback


    【解决方案1】:

    DROP TABLE 是 MySql 中导致隐式提交的命令之一。

    http://dev.mysql.com/doc/refman/5.1/en/implicit-commit.html

    改用这个:

    'RENAME TABLE '.$this->Product->tablePrefix.'products TO backup_table
    , '.$this->Product->tablePrefix.'temp12212 TO '.$this->Product->tablePrefix.'products';
    

    【讨论】:

    • rename也是nt工作的兄弟!!! :-(.. rename 也会发出隐式提交。
    • 我知道,使用RENAME 自动重命名两个表,而不是事务。
    • 工作:-) 感谢您的回复!!! 'RENAME' 不能在 'START TRANSACTION' 和 'COMMIT' 中使用,但由于它的原子性属性,它可以代替它使用...
    【解决方案2】:

    您不能回滚 DROP TABLERENAME TABLE 语句,因为它们会导致 implicit commit

    【讨论】:

      【解决方案3】:
      I sorted the problem this way instead!!! thanks all for your reply :-)  
      
      
       $sql = 'DROP TABLE IF EXISTS '.$this->Product->tablePrefix.'temp_backup';
              mysql_query($sql);
              $sql = 'RENAME TABLE '.$this->Product->tablePrefix.'products TO '.$this->Product->tablePrefix.'temp_backup, '.$this->Product->tablePrefix.'temp TO '.$this->Product->tablePrefix.'products'; 
              $status =mysql_query($sql);
              if($status){
                  $sql = 'DROP TABLE '.$this->Product->tablePrefix.'temp_backup';
                  mysql_query($sql);
                  $this->Session->setFlash('Commit Successful to Database');
              }else{              
                  $this->Session->setFlash('Commit failed due to some errors<br> auto-rollbacked to previous state');
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-29
        • 2010-10-26
        • 2010-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多