note 97247 added to function.PDO-beginTransaction

From: Date: Sat, 10 Apr 2010 02:05:53 +0000
Subject: note 97247 added to function.PDO-beginTransaction
Groups: php.notes 
Request: Send a blank email to php-notes+get-168097@lists.php.net to get a copy of this message
My case: ----------- I had a case where I needed to give "points" to user, then report the turn to the next day. I needed transaction for that because sometime, a second or third user was able to lauch the process between the time that the first user started, but before he completed the transaction. But in the process, the user(s) who receive point need to have a message added. The addMessage function use transaction, because I often have collision on the message table. Possible problematic transaction sheme: ---------- So, I was having this sheme: beginTransaction() //Give the "points" to all the selected users beginTransaction() //Add a message to user 1 commit() beginTransaction() //Add a message to user 2 commit() beginTransaction() //Add a message to user 3 commit() commit() The problem: ---------- So you might see: it's not working well, I have a double beginTransaction(). The solution: ---------- "drm at melp dot nl / 11-Feb-2008 09:37" gave me the idea. My solution is the same as him, but with a counter, so only the first beginTransaction() and the last commit() will be executed. class PDOPlus extends PDO { private $activeTransactionCount = 0; public function beginTransaction() { $this->activeTransactionCount++; if ( $this->activeTransactionCount > 1) return false; else return parent::beginTransaction (); } public function commit() { if($this->activeTransactionCount == 1) parent::commit (); $this->activeTransactionCount--; } public function rollback() { parent::rollback(); $this->activeTransactionCount = 0; } } I'm pretty sure it's not a good & recommended practice, so use it with caution, but I'm unable to figure out how to solve my specific problem in that specific case without doing complex modifications. ---- Server IP: 74.208.45.212 Probable Submitter: 173.178.143.205 (proxied: 173.178.143.205) ---- Manual Page -- http://www.php.net/manual/en/function.PDO-beginTransaction.php Edit -- https://master.php.net/note/edit/97247 Del: integrated -- https://master.php.net/note/delete/97247/integrated Del: useless -- https://master.php.net/note/delete/97247/useless Del: bad code -- https://master.php.net/note/delete/97247/bad+code Del: spam -- https://master.php.net/note/delete/97247/spam Del: non-english -- https://master.php.net/note/delete/97247/non-english Del: in docs -- https://master.php.net/note/delete/97247/in+docs Del: other reasons-- https://master.php.net/note/delete/97247 Reject -- https://master.php.net/note/reject/97247 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168097) next »