PHP中實現MySQL嵌套事務的兩種解決方案

來源:互聯網
上載者:User

標籤:

PHP中實現MySQL嵌套事務的兩種解決方案

 

一、問題起源

在MySQL的官方文檔中有明確的說明不支援嵌套事務:

[sql] view plaincopy 
  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的事務就意義不大了,為什麼呢?上面的文檔中就有提到,簡單的翻譯過來就是:

[sql] view plaincopy 
  1. 當執行一個START TRANSACTION指令時,會隱式的執行一個commit操作。  


所以我們就要在系統架構層面來支援事務的嵌套。所幸的是在一些成熟的ORM架構中都做了對嵌套的支援,比如doctrine或者laravel。接下來我們就一起來看下這兩個架構是怎樣來實現的。
友情提示,這兩個架構的函數和變數的命名都比較的直觀,雖然看起來很長,但是都是通過命名就能直接得知這個函數或者變數的意思,所以不要一看到那麼一大坨就被嚇到了 :)

二、doctrine的解決方案

首先來看下在doctrine中建立事務的代碼(幹掉了不相關的代碼):

[sql] view plaincopy 
  1. public function beginTransaction()  
  2.   
  3. {  
  4.   
  5.     ++$this->_transactionNestingLevel;  
  6.   
  7.     if ($this->_transactionNestingLevel == 1) {  
  8.   
  9.         $this->_conn->beginTransaction();  
  10.   
  11.     } else if ($this->_nestTransactionsWithSavepoints) {  
  12.   
  13.         $this->createSavepoint($this->_getNestedTransactionSavePointName());  
  14.   
  15.     }  
  16.   
  17. }  


這個函數的第一行用一個_transactionNestingLevel來標識當前嵌套的層級,如果是1,也就是還沒有嵌套,那就用預設的方法執行一下START TRANSACTION就ok了,如果大於1,也就是有嵌套的時候,她會幫我們建立一個savepoint,這個savepoint可以理解為一個事務記錄點,當需要復原時可以只復原到這個點。
然後看下rollBack函數:

[sql] view plaincopy 
  1. public function rollBack()  
  2.   
  3. {  
  4.   
  5.     if ($this->_transactionNestingLevel == 0) {  
  6.   
  7.         throw ConnectionException::noActiveTransaction();  
  8.   
  9.     }  
  10.     if ($this->_transactionNestingLevel == 1) {  
  11.   
  12.         $this->_transactionNestingLevel = 0;  
  13.   
  14.         $this->_conn->rollback();  
  15.   
  16.         $this->_isRollbackOnly = false;  
  17.   
  18.     } else if ($this->_nestTransactionsWithSavepoints) {  
  19.   
  20.         $this->rollbackSavepoint($this->_getNestedTransactionSavePointName());  
  21.   
  22.         --$this->_transactionNestingLevel;  
  23.   
  24.     } else {  
  25.   
  26.         $this->_isRollbackOnly = true;  
  27.   
  28.         --$this->_transactionNestingLevel;  
  29.   
  30.     }  
  31.   
  32. }  


可以看到處理的方式也很簡單,如果level是1,直接rollback,否則就復原到前面的savepoint。
然後我們繼續看下commit函數:

[sql] view plaincopy 
  1. public function commit()  
  2.   
  3. {  
  4.   
  5.     if ($this->_transactionNestingLevel == 0) {  
  6.   
  7.         throw ConnectionException::noActiveTransaction();  
  8.   
  9.     }  
  10.   
  11.     if ($this->_isRollbackOnly) {  
  12.   
  13.         throw ConnectionException::commitFailedRollbackOnly();  
  14.   
  15.     }  
  16.     if ($this->_transactionNestingLevel == 1) {  
  17.   
  18.         $this->_conn->commit();  
  19.   
  20.     } else if ($this->_nestTransactionsWithSavepoints) {  
  21.   
  22.         $this->releaseSavepoint($this->_getNestedTransactionSavePointName());  
  23.   
  24.     }  
  25.     --$this->_transactionNestingLevel;  
  26.   
  27. }  


算了,不費口舌解釋這段了吧 :)
三、laravel的解決方案

laravel的處理方式相對簡單粗暴一些,我們先來看下建立事務的操作:

[sql] view plaincopy 
  1. public function beginTransaction()  
  2.   
  3. {  
  4.   
  5.     ++$this->transactions;  
  6.     if ($this->transactions == 1)  
  7.   
  8.     {  
  9.   
  10.         $this->pdo->beginTransaction();  
  11.   
  12.     }  
  13.   
  14. }  


感覺如何?so easy吧?先判斷當前有幾個事務,如果是第一個,ok,事務開始,否則就啥都不做,那麼為啥是啥都不做呢?繼續往下看rollBack的操作:

[sql] view plaincopy 
  1. public function rollBack()  
  2.   
  3. {  
  4.   
  5.     if ($this->transactions == 1)  
  6.   
  7.     {  
  8.   
  9.         $this->transactions = 0;  
  10.         $this->pdo->rollBack();  
  11.   
  12.     }  
  13.   
  14.     else  
  15.   
  16.     {  
  17.   
  18.         --$this->transactions;  
  19.   
  20.     }  
  21.   
  22. }  


明白了吧?只有噹噹前事務只有一個的時候才會真正的rollback,否則只是將計數做減一操作。這也就是為啥剛才說laravel的處理比較簡單粗暴一些,在嵌套的內層裡面實際上是木有真正的事務的,只有最外層一個整體的事務,雖然簡單粗暴,但是也解決了在內層建立一個事務時會造成commit的問題。原理就是這個樣子了,為了保持完整起見,把commit的代碼也copy過來吧!

[sql] view plaincopy 
  1. public function commit()  
  2.   
  3. {  
  4.   
  5.     if ($this->transactions == 1) $this->pdo->commit();  
  6.     --$this->transactions;  
  7.   
  8. }  


參考來源: 
PHP中實現MySQL嵌套事務的兩種解決方案
http://www.lai18.com/content/345498.html

PHP中實現MySQL嵌套事務的兩種解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.