I'm trying to rollBack a PDO transaction based on the results of another block of code. This works great if the DB insert fails but if the "other stuff" throws an exception, it's not rolling back the commit. What am I doing wrong?
PHP version 5.4, database is MS SQL
<?php
try {
$dbh = new PDO($dataSource);
$dbh->beginTransaction();
$sql = "INSERT INTO $table
(field1, field2, etc)
VALUES
(:field1, :field2, :etc)"
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':field1', $data["field1"]);
$stmt->bindParam(':field2', $data["field2"]);
$stmt->bindParam(':etc', $data["etc"]);
$stmt->execute();
$dbh->commit();
//do some other stuff which can throw an Exception
} catch (Exception $e) {
//make sure we have something to roll back
try { $dbh->rollBack(); } catch (Exception $e2) {}
$log->logFatal("Error: controller.inc.php: " . $e->getMessage());
}
?>