I have two distinct modules that can be used independently, but Module2 is dependent on Module1.
Module2 has an operation that needs to be atomic, and it calls an operation in Module1 that also needs be atomic.
Assuming I have set PDO::ATTR_ERRMODE to PDO:ERRMODE_EXCEPTION, the following heavily genericised and snipped code yields this: PHP Fatal error: Uncaught exception 'PDOException' with message 'There is already an active transaction'
Module1:
<?php
class Module1
{
...
public function atomicOperation($stuff)
{
$this->pdo->beginTransaction();
try {
$stmt = $this->pdo->prepare(...);
...
$this->pdo->commit();
}
catch (Exception $ex) {
$this->pdo->rollBack();
throw $ex;
}
}
}
Module2:
<?php
class Module2
{
public $module1;
...
public function atomicOperation($stuff)
{
$this->pdo->beginTransaction();
try {
$stmt = $this->pdo->prepare(...);
...
$this->module1->atomicOperation($stuff);
...
$this->pdo->commit();
}
catch (Exception $ex) {
$this->pdo->rollBack();
throw $ex;
}
}
}
I'm not sure the best way to go about this - the nested operation will definitely be called independently and absolutely must be atomic when called in its own right. Placing the onus on the class' user to manage the transaction and preserve atomicity is not desirable as I am certain the users of the class will never enforce it.