ladar
ladar

Reputation: 5876

beginTransaction() and commit() for prepared and nonprepared statements

I have following code:

try{
 $db->beginTransaction();
 $handler = $db->prepare(...);
 $handler->execute()
 $query2 = "INSERT INTO...";
 $db->exec($query2);
 $db->commit();
}catch (PDOException $e) {
 try { $db->rollBack(); } catch (Exception $e2) {}
}

My question is, does the rollBack() rollbacks all changes caused by both, execute() and exec()? The reason for using exec() is that I have to dynamically create the $query2 and this way it is much easier for me.

Upvotes: 0

Views: 2467

Answers (1)

Marc B
Marc B

Reputation: 360562

Any operations performed between the start of a transaction and the point you do the rollback are reversed. Doesn't matter HOW you did those operations - they'll be rolled back.

Of course, this assumes you're using transaction-capable databases/tables. For instance, if your exec() was done on a MyISAM table in MySQL, and the execute() on an InnODB table, then InnoDB operation will get rolled back, but you're stuck on the MyISAM one.

Upvotes: 1

Related Questions