隨筆 - 100  文章 - 50  trackbacks - 0
          <2014年12月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          我收藏的一些文章!

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          框架采用的是spring管理聲明式事務(wù),這幾天業(yè)務(wù)開發(fā)時(shí)遇到了點(diǎn)麻煩,記錄下備忘。

           

           場(chǎng)景:在Service類中使用子事務(wù)(saveponit)時(shí),當(dāng)子事務(wù)拋出異常后,此異常處理掉不繼續(xù)往外拋,spring在提交主事務(wù)時(shí)會(huì)拋出

          org.springframework.transaction.UnexpectedRollbackException: Transaction has been rolled back because it has been marked as rollback

           

           方法調(diào)用結(jié)構(gòu):

                假若有A、B、C三個(gè)Service類,其實(shí)例對(duì)象分別為a、b、c,類分別定義如下:

           

                      A {

                              方法() {                   //propagation="REQUIRED"

            try{

           

                                          b.方法();

             } catch (Exception e) {

                     }

                              }      

             }

           

                      B{

                               savePoint方法() {  //propagation="NESTED"

             c.方法(); //如果這里邊的操作全是普通類(不是Service類)操作,不會(huì)有問(wèn)題。

                               }

                       }

           

           

                      C{

                                方法() {                 //propagation="REQUIRED"

            throw new Exception("出錯(cuò)");

               }

           

                        }

           

           

             通過(guò)調(diào)試spring源碼 

                     ......

                     Getting transaction for [A.方法] .....

                     ......

             Creating nested transaction with name [B.savePoint方法]........

             ......

                     Participating in existing transaction

                     Getting transaction for [C.方法]

                     ........

             Participating transaction failed - marking existing transaction as rollback-only

                     //此時(shí),已把主事務(wù)標(biāo)記成了rollback-only

           

                     所以,當(dāng)在a.方法完成時(shí)提交事務(wù)時(shí)會(huì)報(bào)Transaction has been rolled back because it has been marked as rollback錯(cuò)誤。

           

                     認(rèn)真的您可能會(huì)發(fā)現(xiàn),在 org.springframework.transaction.support.AbstractPlatformTransactionManager 中有個(gè)叫

          isGlobalRollbackOnParticipationFailure的參數(shù),默認(rèn)是true.

           

                    源碼中說(shuō)明:

           

                  Switch this to "false" to let the transaction originator make the rollback decision. If a participating transaction fails with an exception, the caller can still decide to continue with a different path within the transaction. However, note that this will only work as long as all participating resources are capable of continuing towards a transaction commit even after a data access failure: This is generally not the case for a Hibernate Session, for example; neither is it for a sequence of JDBC insert/update/delete operations.

           

                    大意是:如果isGlobalRollbackOnParticipationFailure為false,則會(huì)讓主事務(wù)決定回滾,如果當(dāng)遇到exception加入事務(wù)失敗時(shí),調(diào)用者能繼續(xù)在事務(wù)內(nèi)決定是回滾還是繼續(xù)。然而,要注意是那樣做僅僅適用于在數(shù)據(jù)訪問(wèn)失敗的情況下且只要所有操作事務(wù)能提交。

           

                  

              初步解決方案:

          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="globalRollbackOnParticipationFailure" value="false" /> <!--指定此參數(shù)為false-->

          </bean>

           

           

              經(jīng)測(cè)試,此問(wèn)題暫時(shí)得到解決,不知道會(huì)不會(huì)引起其它問(wèn)題,至少目前還沒有發(fā)現(xiàn)其它異常。您若通過(guò)此方案解決之后出現(xiàn)了新的問(wèn)題請(qǐng)留信回復(fù),我們一起交流,非常感謝!

           

          posted on 2014-12-20 14:23 fly 閱讀(601) 評(píng)論(0)  編輯  收藏 所屬分類: J2EE
          主站蜘蛛池模板: 平阳县| 瑞昌市| 花莲县| 韩城市| 来宾市| 叶城县| 新昌县| 阿拉善左旗| 会泽县| 辰溪县| 青岛市| 长治市| 白水县| 建阳市| 彭阳县| 宝兴县| 祁门县| 同心县| 勃利县| 高雄市| 濮阳市| 阿克| 青田县| 富顺县| 德格县| 达尔| 平泉县| 桐城市| 沭阳县| 来凤县| 双峰县| 万州区| 渝北区| 上蔡县| 平阳县| 缙云县| 泽普县| 团风县| 莱西市| 肃北| 兖州市|