Spring事務REQUIRED與REQUIRES_NEW區別

原文鏈接:https://blog.csdn.net/C707631864/article/details/75051869

PROPAGATION_REQUIRED 
加入當前正要執行的事務不在另外一個事務裏,那麼就起一個新的事務 
比如說,ServiceB.methodB的事務級別定義爲PROPAGATION_REQUIRED, 那麼由於執行ServiceA.methodA的時候,ServiceA.methodA已經起了事務,這時調用ServiceB.methodB,ServiceB.methodB看到自己已經運行在ServiceA.methodA 
的事務內部,就不再起新的事務。而假如ServiceA.methodA運行的時候發現自己沒有在事務中,他就會爲自己分配一個事務。這樣,在ServiceA.methodA或者在ServiceB.methodB內的任何地方出現異常,事務都會被回滾。即使ServiceB.methodB的事務已經被提交,但是ServiceA.methodA在接下來fail要回滾,ServiceB.methodB也要回滾 


PROPAGATION_REQUIRES_NEW 
那麼當執行到ServiceB.methodB的時候,ServiceA.methodA所在的事務就會掛起,ServiceB.methodB會起一個新的事務,等待ServiceB.methodB的事務完成以後, 
他才繼續執行。他與PROPAGATION_REQUIRED 的事務區別在於事務的回滾程度了。因爲ServiceB.methodB是新起一個事務,那麼就是存在 
兩個不同的事務。如果ServiceB.methodB已經提交,那麼ServiceA.methodA失敗回滾,ServiceB.methodB是不會回滾的。如果ServiceB.methodB失敗回滾, 
如果他拋出的異常被ServiceA.methodA捕獲,ServiceA.methodA事務仍然可能提交。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章