PEAR::DB and transactions, PART II

From: Date: Mon, 14 Jun 2004 15:23:47 +0000
Subject: PEAR::DB and transactions, PART II
References: 1 2  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-13145@lists.php.net to get a copy of this message
Ok, next question is given that all my tables are MySQL INNODB and given this code snippet: // Begin a SQL transaction if we can if ($this->db->provides('transactions')) { print 'starting transaction...<br>'; $this->db->autoCommit(false); } <issue a single insert into a table> // Add password to history try { // this will issue another insert $this->someMethod(arg1, arg2); } catch (AESQLException $e) { // Rollback transaction if ($this->db->provides('transactions')) { print 'rolling back transaction...<br>'; $this->db->rollback(); } throw $e; } // must be ok, commit $this->db->commit(); $this->db->autoCommit(true); ---- Why do I get this output: starting transaction... rolling back transaction... Yet, when I check the tables the first insert is there when it should have been rolled back. Any idea why? --Tony On Mon, 2004-06-14 at 09:54, Torsten Roehr wrote: > "Tony Bibbs" <tony@tonybibbs.com> wrote in message > news:1087224740.2925.24.camel@linux.local... > > I have a program I'm writing that needs to use transactions if that > > feature is supported by the DBMS. In reading here: > > > > ᯱ•lÀ9r�ïî÷- > > http://pear.php.net/manual/en/package.database.db.db-common.provides.php > > > > I see I can check if the DBMS supports transaction which is great. > > However, I don't see anything here: > > > > http://pear.php.net/manual/en/package.database.php > > > > that suggests how to wrap things into a transaction. Obviously the fact > > provides() even exists tells me there is some methods in the PEAR::DB > > manual that isn't showing up that is probably here. So my question is, > > how to I start, stop and rollback transactions with PEAR::DB? > > You can do this (simplified): > > $errors = 0; > > // start transaction > $db->autocommit(false); > > // 1st query > $result = $db->query($query1); > if (DB::isError($result)) $errors++; > > // 2nd query > $result = $db->query($query2); > if (DB::isError($result)) $errors++; > > // and so on > > // rollback if errors occurred > if ($errors) { > $db->query('ROLLBACK'); > } else { > $db->query('COMMIT'); > } > > $db->autocommit(true); > > > Hope this helps. Regards, > > Torsten Roehr

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