標籤:事務 php mysql 嵌套事務
一、問題起源
在MySQL的官方文檔中有明確的說明不支援嵌套事務:
1. Transactions cannot be nested. This is a consequence of the implicit commit performed for any current transaction when you issue a START TRANSACTION statement or one of its synonyms.
但是在我們開發一個複雜的系統時難免會無意中在事務中嵌套了事務,比如A函數調用了B函數,A函數使用了事務,並且是在事務中調用了B函數,B函數也有一個事務,這樣就出現了事務嵌套。這時候其實A的事務就意義不大了,為什麼呢?上面的文檔中就有提到,簡單的翻譯過來就是:
1. 當執行一個START TRANSACTION指令時,會隱式的執行一個commit操作。 所以我們就要在系統架構層面來支援事務的嵌套。
所幸的是在一些成熟的ORM架構中都做了對嵌套的支援,比如doctrine或者laravel。接下來我們就一起來看下這兩個架構是怎樣來實現的。友情提示,這兩個架構的函數和變數的命名都比較的直觀,雖然看起來很長,但是都是通過命名就能直接得知這個函數或者變數的意思,所以不要一看到那麼一大坨就被嚇到了 :)
二、doctrine的解決方案
首先來看下在doctrine中建立事務的代碼(幹掉了不相關的代碼):
/** * author http://www.lai18.com * date 2015-04-19 * version 1 **/ public function beginTransaction(){ ++$this->_transactionNestingLevel; if ($this->_transactionNestingLevel == 1) { $this->_conn->beginTransaction(); } else if ($this->_nestTransactionsWithSavepoints) { $this->createSavepoint($this->_getNestedTransactionSavePointName()); }}
這個函數的第一行用一個_transactionNestingLevel來標識當前嵌套的層級,如果是1,也就是還沒有嵌套,那就用預設的方法執行一下START TRANSACTION就ok了,如果大於1,也就是有嵌套的時候,她會幫我們建立一個savepoint,這個savepoint可以理解為一個事務記錄點,當需要復原時可以只復原到這個點。然後看下rollBack函數:
1. /** * author http://www.lai18.com * date 2015-04-19 * version 1 **/ public function rollBack(){ if ($this->_transactionNestingLevel == 0) { throw ConnectionException::noActiveTransaction(); } 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; }}
可以看到處理的方式也很簡單,如果level是1,直接rollback,否則就復原到前面的savepoint。然後我們繼續看下commit函數:
1. /** * author http://www.lai18.com * date 2015-04-19 * version 1 **/ public function commit(){ if ($this->_transactionNestingLevel == 0) { throw ConnectionException::noActiveTransaction(); } if ($this->_isRollbackOnly) { throw ConnectionException::commitFailedRollbackOnly(); } if ($this->_transactionNestingLevel == 1) { $this->_conn->commit(); } else if ($this->_nestTransactionsWithSavepoints) { $this->releaseSavepoint($this->_getNestedTransactionSavePointName()); } --$this->_transactionNestingLevel;}
算了,不費口舌解釋這段了吧 :)
三、laravel的解決方案laravel的處理方式相對簡單粗暴一些,我們先來看下建立事務的操作:
1. /** * author http://www.lai18.com * date 2015-04-19 * version 1 **/ public function beginTransaction(){ ++$this->transactions; if ($this->transactions == 1) { $this->pdo->beginTransaction(); }}
感覺如何?so easy吧?先判斷當前有幾個事務,如果是第一個,ok,事務開始,否則就啥都不做,那麼為啥是啥都不做呢?繼續往下看rollBack的操作:
1. /** * author http://www.lai18.com * date 2015-04-19 * version 1 **/ public function rollBack(){ if ($this->transactions == 1) { $this->transactions = 0; $this->pdo->rollBack(); } else { --$this->transactions; }}
明白了吧?只有噹噹前事務只有一個的時候才會真正的rollback,否則只是將計數做減一操作。這也就是為啥剛才說laravel的處理比較簡單粗暴一些,在嵌套的內層裡面實際上是木有真正的事務的,只有最外層一個整體的事務,雖然簡單粗暴,但是也解決了在內層建立一個事務時會造成commit的問題。原理就是這個樣子了,為了保持完整起見,把commit的代碼也copy過來吧!
public function commit(){ if ($this->transactions == 1) $this->pdo->commit(); --$this->transactions;}
PHP中實現MySQL嵌套事務的兩種解決方案