note 21019 added to ref.mysql

From: Date: Thu, 25 Apr 2002 13:23:51 +0000
Subject: note 21019 added to ref.mysql
Groups: php.notes 
Request: Send a blank email to php-notes+get-29835@lists.php.net to get a copy of this message
MySQL transactions MySQL supports transactions on tables that are of type InnoDB. I have noticed a behaviour which is puzzling me when using transactions. If I establish two connections within the same PHP page, start a transaction in the first connection and execute an INSERT query in the second one, and rollback the transaction in the first connection, the INSERT query in the second connection is also rolled-back. I am assuming that a MySQL transaction is not bound by the connection within which it is set up, but rather by the PHP process that sets it up. This is a very useful "mis-feature" (bug?) because it allows you to create something like this: class Transaction { var $dbh; function Transaction($host, $username, $password) { $this->dbh = mysql_connect($host, $username, $password); } function _Transaction() { mysql_disconnect($this->dbh); } function begin() { mysql_query("BEGIN", $this->dbh); } function rollback() { mysql_query("ROLLBACK", $this->dbh); } function commit() { mysql_query("COMMIT", $this->dbh); } } which you could use to wrap around transactional statements like this: $tx =& new Transaction("localhost", "username", "password"); $tx->begin(); $dbh = mysql_connect("localhost", "username", "password"); $result = mysql_query("INSERT ..."); if (!$result) { $tx->rollback(); } else { $tx->commit(); } mysql_disconnect($dbh); unset($tx); The benefit of such a Transaction class is that it is generic and can wrap around any of your MySQL statements. -- http://www.php.net/manual/en/ref.mysql.php http://master.php.net/manage/user-notes.php?action=edit+21019 http://master.php.net/manage/user-notes.php?action=delete+21019 http://master.php.net/manage/user-notes.php?action=reject+21019

« previous php.notes (#29835) next »