ConFoo Montreal 2026: Call for Papers

Voting

: eight minus zero?
(Example: nine)

The Note You're Voting On

bitluni
12 years ago
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:

<?php
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;
}
//...
}
?>

<< Back to user notes page

To Top