一、問題起源
在MySQL的官方文檔中有明確的說明不支持嵌套事務:
友情提示,這兩個框架的函數(shù)和變量的命名都比較的直觀,雖然看起來很長,但是都是通過命名就能直接得知這個函數(shù)或者變量的意思,所以不要一看到那么一大坨就被嚇到了 :)
二、doctrine的解決方案
首先來看下在doctrine中創(chuàng)建事務的代碼(干掉了不相關(guān)的代碼):
然后看下rollBack函數(shù):
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;
}
}
然后我們繼續(xù)看下commit函數(shù):
if ($this->_transactionNestingLevel == 1) {
$this->_conn->commit();
} else if ($this->_nestTransactionsWithSavepoints) {
$this->releaseSavepoint($this->_getNestedTransactionSavePointName());
}
--$this->_transactionNestingLevel;
}
三、laravel的解決方案
laravel的處理方式相對簡單粗暴一些,我們先來看下創(chuàng)建事務的操作:
if ($this->transactions == 1)
{
$this->pdo->beginTransaction();
}
}
$this->pdo->rollBack();
}
else
{
--$this->transactions;
}
}
--$this->transactions;
}
新聞熱點
疑難解答