note 116669 added to pdo.begintransaction
| From: | steve at fancyguy dot com | Date: | Sun, 08 Feb 2015 08:34:53 +0000 |
| Subject: | note 116669 added to pdo.begintransaction | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201666@lists.php.net to get a copy of this message | ||
The nested transaction example here is great, but it's missing a key piece of the puzzle.
Commits will commit everything, I only wanted commits to actually commit when the outermost commit
has been completed. This can be done in InnoDB with savepoints.
<?php
class Database extends PDO
{
protected $transactionCount = 0;
public function beginTransaction()
{
if (!$this->transactionCounter++) {
return parent::beginTransaction();
}
$this->exec('SAVEPOINT trans'.$this->transactionCounter);
return $this->transactionCounter >= 0;
}
public function commit()
{
if (!--$this->transactionCounter) {
return parent::commit();
}
return $this->transactionCounter >= 0;
}
public function rollback()
{
if (--$this->transactionCounter) {
$this->exec('ROLLBACK TO trans'.$this->transactionCounter + 1);
return true;
}
return parent::rollback();
}
}
----
Server IP: 72.52.91.14
Probable Submitter: 24.131.220.250
----
Manual Page -- http://php.net/manual/en/pdo.begintransaction.php
Edit -- https://master.php.net/note/edit/116669
Del: integrated -- https://master.php.net/note/delete/116669/integrated
Del: useless -- https://master.php.net/note/delete/116669/useless
Del: bad code -- https://master.php.net/note/delete/116669/bad+code
Del: spam -- https://master.php.net/note/delete/116669/spam
Del: non-english -- https://master.php.net/note/delete/116669/non-english
Del: in docs -- https://master.php.net/note/delete/116669/in+docs
Del: other reasons-- https://master.php.net/note/delete/116669
Reject -- https://master.php.net/note/reject/116669
Search -- https://master.php.net/manage/user-notes.php