2011-01-25 68 views
1

我有一個代碼,在一個應用程序中,我在回滾時遇到問題。即使我'2'返回虛假回滾不發生,即表'產品'正在下降。 任何人都可以解釋爲什麼它不工作,或者我應該如何改變它。 注:表是Innodb的engine..I使用MySQL 5.0+Mysql中的MySQL事務+ PHP問題

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'); 
    } 

回答

2

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'; 
+0

重命名也nt工作兄弟! :-(重命名也發出隱式提交 – Libu 2011-01-25 07:11:14

1

,因爲它們會導致implicit commit不能回滾DROP TABLERENAME TABLE聲明。

0
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'); 
     }