PHP中實現MySQL嵌套事務的兩種解決方案,mysql嵌套_PHP教程

來源:互聯網
上載者:User

PHP中實現MySQL嵌套事務的兩種解決方案,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中建立事務的代碼(幹掉了不相關的代碼):

[php] view plaincopy
  1. /**
  2. * author http://www.lai18.com
  3. * date 2015-04-19
  4. * version 1
  5. **/
  6. public function beginTransaction()
  7. {
  8. ++$this->_transactionNestingLevel;
  9. if ($this->_transactionNestingLevel == 1) {
  10. $this->_conn->beginTransaction();
  11. } else if ($this->_nestTransactionsWithSavepoints) {
  12. $this->createSavepoint($this->_getNestedTransactionSavePointName());
  13. }
  14. }

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

[php] view plaincopy
  1. 1. /**
  2. * author http://www.lai18.com
  3. * date 2015-04-19
  4. * version 1
  5. **/
  6. public function rollBack()
  7. {
  8. if ($this->_transactionNestingLevel == 0) {
  9. throw ConnectionException::noActiveTransaction();
  10. }
  11. if ($this->_transactionNestingLevel == 1) {
  12. $this->_transactionNestingLevel = 0;
  13. $this->_conn->rollback();
  14. $this->_isRollbackOnly = false;
  15. } else if ($this->_nestTransactionsWithSavepoints) {
  16. $this->rollbackSavepoint($this->_getNestedTransactionSavePointName());
  17. --$this->_transactionNestingLevel;
  18. } else {
  19. $this->_isRollbackOnly = true;
  20. --$this->_transactionNestingLevel;
  21. }
  22. }

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

[php] view plaincopy
  1. 1. /**
  2. * author http://www.lai18.com
  3. * date 2015-04-19
  4. * version 1
  5. **/
  6. public function commit()
  7. {
  8. if ($this->_transactionNestingLevel == 0) {
  9. throw ConnectionException::noActiveTransaction();
  10. }
  11. if ($this->_isRollbackOnly) {
  12. throw ConnectionException::commitFailedRollbackOnly();
  13. }
  14. if ($this->_transactionNestingLevel == 1) {
  15. $this->_conn->commit();
  16. } else if ($this->_nestTransactionsWithSavepoints) {
  17. $this->releaseSavepoint($this->_getNestedTransactionSavePointName());
  18. }
  19. --$this->_transactionNestingLevel;
  20. }

算了,不費口舌解釋這段了吧 :)

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

[php] view plaincopy
  1. 1.
  2. /**
  3. * author http://www.lai18.com
  4. * date 2015-04-19
  5. * version 1
  6. **/
  7. public function beginTransaction()
  8. {
  9. ++$this->transactions;
  10. if ($this->transactions == 1)
  11. {
  12. $this->pdo->beginTransaction();
  13. }
  14. }

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

[php] view plaincopy
  1. 1. /**
  2. * author http://www.lai18.com
  3. * date 2015-04-19
  4. * version 1
  5. **/
  6. public function rollBack()
  7. {
  8. if ($this->transactions == 1)
  9. {
  10. $this->transactions = 0;
  11. $this->pdo->rollBack();
  12. }
  13. else
  14. {
  15. --$this->transactions;
  16. }
  17. }

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

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

http://www.bkjia.com/PHPjc/991536.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/991536.htmlTechArticlePHP中實現MySQL嵌套事務的兩種解決方案,mysql嵌套 一、問題起源 在MySQL的官方文檔中有明確的說明不支援嵌套事務: 1. Transactions cannot be n...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.