Re: Transactions in MD2

From: Date: Fri, 18 May 2007 12:02:45 +0000
Subject: Re: Transactions in MD2
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-27320@lists.php.net to get a copy of this message
Florian Schmitz wrote:
According to the manual, in MDB2 it works like this: $mdb2->beginTransaction(); $mdb2->query($sql); $mdb2->query($Sql2); [...] // determine if the commit or rollback if ($error_condition) { $res = $mdb2->rollback(); } else { $res = $mdb2->commit(); } Interestingly, the manual doesn't mention the most important point, that is, how to determine $error_condition and thus decide whether or not to rollback the changes. The only method I could think of is checking every single query manually for an error, but I certainly won't do this for obvious reasons (bloat, work, error prone, ...).
I don't have any MDB2 specific information for you, but I was interested in your comment about not checking that each query executes successfully. In a transaction, a failed query will cause all subsequent queries to fail also, so running those queries when you know the transaction has been aborted is a waste of processing time. I think that a better piece of code would be something like: $mdb2->beginTransaction(); $result = $mdb2->query($sql); if (PEAR::isError($result)) { $mdb2->rollback(); // Handle error in application. return; } $result = $mdb2->query($sql2); if (PEAR::isError($result)) { $mdb2->rollback(); // Handle error in application. return; } $mdb2->commit(); Of course, I have no idea what application you are writing, and it sounds to be fairly large, but I just thought I'd offer an opinion. I'm not sure what you are supposed to do in MDB2, but that is what I used to do with PEAR::DB. Greg

« previous php.pear.general (#27320) next »