note 119975 added to pdo.begintransaction
| From: | cristian at crishk dot com | Date: | Sat, 01 Oct 2016 22:12:48 +0000 |
| Subject: | note 119975 added to pdo.begintransaction | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-206716@lists.php.net to get a copy of this message | ||
OK I'm finding a solution for "NESTED" transactions in MySQL, and as you know in the
MySQL documentation says that it's not possible to have transactions within transactions. I was
trying to use the Database class propossed here in http://php.net/manual/en/pdo.begintransaction.php
but unfortunately that's wrong for many things related to the control flow that I have been
solved with the following code (LOOK THE EXAMPLE AT THE END, CarOwner)
<?php
class TransactionController extends \\PDO {
public static $warn_rollback_was_thrown = false;
public static $transaction_rollbacked = false;
public function __construct()
{
parent :: __construct( ... connection info ... );
}
public static $nest = 0;
public function reset()
{
TransactionController :: $transaction_rollbacked = false;
TransactionController :: $warn_rollback_was_thrown = false;
TransactionController :: $nest = 0;
}
function beginTransaction()
{
$result = null;
if (TransactionController :: $nest == 0) {
$this->reset();
$result = $this->beginTransaction();
}
TransactionController :: $nest++;
return $result;
}
public function commit()
{
$result = null;
if (TransactionController :: $nest == 0 &&
!TransactionController :: $transaction_rollbacked &&
!TransactionController :: $warn_rollback_was_thrown) {
$result = parent :: commit();
}
TransactionController :: $nest--;
return $result;
}
public function rollback()
{
$result = null;
if (TransactionController :: $nest >= 0) {
if (TransactionController :: $nest == 0) {
$result = parent :: rollback();
TransactionController :: $transaction_rollbacked = true;
}
else {
TransactionController :: $warn_rollback_was_thrown = true;
}
}
TransactionController :: $nest--;
return $result;
}
public function transactionFailed()
{
return TransactionController :: $warn_rollback_was_thrown === true;
}
// to force rollback you can only do it from $nest = 0
public function forceRollback()
{
if (TransactionController :: $nest === 0) {
throws new \PDOException();
}
}
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 190.9.208.253
----
Manual Page -- http://php.net/manual/en/pdo.begintransaction.php
Edit -- https://master.php.net/note/edit/119975
Del: integrated -- https://master.php.net/note/delete/119975/integrated
Del: useless -- https://master.php.net/note/delete/119975/useless
Del: bad code -- https://master.php.net/note/delete/119975/bad+code
Del: spam -- https://master.php.net/note/delete/119975/spam
Del: non-english -- https://master.php.net/note/delete/119975/non-english
Del: in docs -- https://master.php.net/note/delete/119975/in+docs
Del: other reasons-- https://master.php.net/note/delete/119975
Reject -- https://master.php.net/note/reject/119975
Search -- https://master.php.net/manage/user-notes.php