note 109753 added to pdo.begintransaction
| From: | bitluni | Date: | Thu, 16 Aug 2012 21:27:17 +0000 |
| Subject: | note 109753 added to pdo.begintransaction | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190105@lists.php.net to get a copy of this message | ||
You can generate problems with nested beginTransaction and commit calls.
example:
beginTransaction()
do imprortant stuff
call method
beginTransaction()
basic stuff 1
basic stuff 2
commit()
do most important stuff
commit()
Won't work and is dangerous since you could close your transaction too early with the nested
commit().
There is no need to mess you code and pass like a bool which indicate if transaction is already
running. You could just overload the beginTransaction() and commit() in your PDO wrapper like this:
class Database extends \PDO
{
protected $transactionCounter = 0;
function beginTransaction()
{
if(!$this->transactionCounter++)
return parent::beginTransaction();
return $this->transactionCounter >= 0;
}
function commit()
{
if(!--$this->transactionCounter)
return parent::commit();
return $this->transactionCounter >= 0;
}
function rollback()
{
if($this->transactionCounter >= 0)
{
$this->transactionCounter = 0;
return parent::rollback();
}
$this->transactionCounter = 0;
return false;
}
...
}
----
Server IP: 69.147.83.197
Probable Submitter: 77.1.88.97
----
Manual Page -- http://www.php.net/manual/en/pdo.begintransaction.php
Edit -- https://master.php.net/note/edit/109753
Del: integrated -- https://master.php.net/note/delete/109753/integrated
Del: useless -- https://master.php.net/note/delete/109753/useless
Del: bad code -- https://master.php.net/note/delete/109753/bad+code
Del: spam -- https://master.php.net/note/delete/109753/spam
Del: non-english -- https://master.php.net/note/delete/109753/non-english
Del: in docs -- https://master.php.net/note/delete/109753/in+docs
Del: other reasons-- https://master.php.net/note/delete/109753
Reject -- https://master.php.net/note/reject/109753
Search -- https://master.php.net/manage/user-notes.php