PDO回滚不起作用

时间:2014-04-12 06:33:45

标签: php mysql pdo

$myServer='127.0.0.1';
$myDB='karma';
$myUser='root';
$myPass='wilian';


try {
    $connStr = "mysql:host=".$myServer.";dbname=".$myDB; 
    $dbh = new PDO($connStr,$myUser,$myPass);
} catch (Exception $e) {
    die("Unable to connect: " . $e->getMessage());
}

try {  
  $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $dbh->beginTransaction();
  $dbh->query("insert into mst_goods_category (id, GOODS_CATEGORY_CODE, GOODS_CATEGORY_NAME, ACTIVE_FLAG, CREATE_DATE, CREATE_TIME, CREATE_USER) values (12, '012', '333', 'Y', '20140702', '00:00:00', 'admin')");
  $dbh->query("insert into mst_goods_category (id, GOODS_CATEGORY_CODE, GOODS_CATEGORY_NAME, ACTIVE_FLAG, CREATE_DATE, CREATE_TIME, CREATE_USER) values (13, '012', '333', 'Y', '20140702', '00:00:00', 'admin')");
  $dbh->commit();

} catch (PDOException $e) {
  $dbh->rollBack();
  throw $e;
}

由于唯一约束,第二个查询是错误的.. 抛出$ e会出现,但它不会回滚,任何人都知道什么是错的?

1 个答案:

答案 0 :(得分:0)

您很可能使用的Myisam引擎不支持交易。将其更改为InnoDB

ALTER TABLE mst_goods_category ENGINE=InnoDB;