一、问题起源
在MySQL的官方文档中有明确的说明不支持嵌套事务:
然后看下rollBack函数:
if ($this->_transactionNestingLevel == 1) {
$this->_transactionNestingLevel = 0;
$this->_conn->rollback();
$this->_isRollbackOnly = false;
} else if ($this->_nestTransactionsWithSavepoints) {
$this->rollbackSavepoint($this->_getNestedTransactionSavePointName());
--$this->_transactionNestingLevel;
} else {
$this->_isRollbackOnly = true;
--$this->_transactionNestingLevel;
}
}
然后我们继续看下commit函数:
if ($this->_transactionNestingLevel == 1) {
$this->_conn->commit();
} else if ($this->_nestTransactionsWithSavepoints) {
$this->releaseSavepoint($this->_getNestedTransactionSavePointName());
}
--$this->_transactionNestingLevel;
}
三、laravel的解决方案
laravel的处理方式相对简单粗暴一些,我们先来看下创建事务的操作:
if ($this->transactions == 1)
{
$this->pdo->beginTransaction();
}
}
$this->pdo->rollBack();
}
else
{
--$this->transactions;
}
}
--$this->transactions;
}