2011-01-25 10 views
1

ロールバックに問題が発生しているアプリケーションで使用されていたコードがあります。たとえ私が'2 'が返っても、誤ったロールバックは起こっていません。つまり、テーブル' products 'がドロップされています。 誰かがなぜ動作しないのか、どうやって変更すればよいのか説明できる人はいませんか? 注:テーブルは5.0以上MySQL Transaction + PHPのMySQLの問題

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でのコマンドの一つであるInnoDBのengine..I使用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作業兄弟です! :-(.. renameも暗黙のコミットを発行します – Libu

+0

トランザクションの代わりに両方のテーブルの名前を自動的に変更するには 'RENAME'を使用してください –

+0

Worked :-)返信ありがとう!!! 'RENAME'は 'START TRANSACTION'& 'COMMIT'の内部では使用できませんが、そのアトミック性のために代わりに使用できます。 – Libu

1

を彼らはimplicit commitを引き起こすとしてあなたがDROP TABLEまたはRENAME 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'); 
     }