夢之天堂

          我學故我知,我思故我在;java你我,happy你我——sylilzy

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            3 Posts :: 8 Stories :: 2 Comments :: 0 Trackbacks

          公告

          正如王詠剛在《凌波微步》中所說的:我整天生活在控制電腦(編程)和被電腦控制(debug)的辯證關系里。我想,作為程序員,大家都是一樣。不同的是,我希望能從這種辯證關系中尋找樂趣。 幾經努力,我的Blog正式開通了,希望能帶給我更多的樂趣:o)
          ---sylilzy(施祖陽) msn:sylilzy@163.com

          >>> 歡迎留言 <<<

          常用鏈接

          留言簿(1)

          隨筆檔案(3)

          文章分類(7)

          文章檔案(8)

          相冊

          最新隨筆

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          spring的事務處理詳解:調用一個方法前的事務處理過程(源代碼分析)
          ?
          sylilzy@163.com
          版權所有,轉載請注明出處
          ?
          實際上,在spring的事務中,只要該類被設置為了事務代理:
          ?
          攔截器都會創建一個TransactionInfo 對象:
          ?
          TransactionInfo txInfo = new TransactionInfo(txAttr, method);
          ?
          ?
          而且如果 只要被調用的方法設置了事務屬性(txAttr),不管是什么屬性都會調用:
          ?
          txInfo.newTransactionStatus(this.transactionManager.getTransaction(txAttr));
          ?
          根據該方法的事務屬性(definition )的不同,this.transactionManager.getTransaction(txAttr)的返回值會有所不同(代碼見AbstractPlatformTransactionManager),具體為以下幾種情況:
          1.當前沒有事務時(即以下代碼中的((HibernateTransactionObject) transaction).hasTransaction()返回false),會返回以下幾種:
          ?
          ?1 // ?Check?definition?settings?for?new?transaction.
          ?2 ?? if ?(definition.getTimeout()? < ?TransactionDefinition.TIMEOUT_DEFAULT)? {
          ?3 ??? throw ? new ?InvalidTimeoutException( " Invalid?transaction?timeout " ,?definition.getTimeout());
          ?4 ??}

          ?5
          ?6 ?? // ?No?existing?transaction?found?->?check?propagation?behavior?to?find?out?how?to?behave.
          ?7 ?? if ?(definition.getPropagationBehavior()? == ?TransactionDefinition.PROPAGATION_MANDATORY)? {
          ?8 ??? throw ? new ?IllegalTransactionStateException(
          ?9 ????? " Transaction?propagation?'mandatory'?but?no?existing?transaction?found " );
          10 ??}

          11 ?? else ? if ?(definition.getPropagationBehavior()? == ?TransactionDefinition.PROPAGATION_REQUIRED? ||
          12 ????definition.getPropagationBehavior()? == ?TransactionDefinition.PROPAGATION_REQUIRES_NEW? ||
          13 ??????definition.getPropagationBehavior()? == ?TransactionDefinition.PROPAGATION_NESTED)? {
          14 ??? if ?(debugEnabled)? {
          15 ????logger.debug( " Creating?new?transaction?with?name?[ " ? + ?definition.getName()? + ? " ] " );
          16 ???}

          17 ???doBegin(transaction,?definition);
          18 ??? boolean ?newSynchronization? = ?( this .transactionSynchronization? != ?SYNCHRONIZATION_NEVER);
          19 ??? return ?newTransactionStatus(definition,?transaction,? true ,?newSynchronization,?debugEnabled,? null );
          20 ??}

          21 ?? else ? {
          22 ??? // ?Create?"empty"?transaction:?no?actual?transaction,?but?potentially?synchronization.
          23 ??? boolean ?newSynchronization? = ?( this .transactionSynchronization? == ?SYNCHRONIZATION_ALWAYS);
          24 ??? return ?newTransactionStatus(definition,? null ,? false ,?newSynchronization,?debugEnabled,? null );
          25 ??}

          26
          2.當前有事務時
          ?1 private ?TransactionStatus?handleExistingTransaction(
          ?2 ???TransactionDefinition?definition,?Object?transaction,? boolean ?debugEnabled)
          ?3 ??? throws ?TransactionException? {
          ?4
          ?5 ?? if ?(definition.getPropagationBehavior()? == ?TransactionDefinition.PROPAGATION_NEVER)? {
          ?6 ??? throw ? new ?IllegalTransactionStateException(
          ?7 ????? " Transaction?propagation?'never'?but?existing?transaction?found " );
          ?8 ??}

          ?9
          10 ?? if ?(definition.getPropagationBehavior()? == ?TransactionDefinition.PROPAGATION_NOT_SUPPORTED)? {
          11 ??? if ?(debugEnabled)? {
          12 ????logger.debug( " Suspending?current?transaction " );
          13 ???}

          14 ???Object?suspendedResources? = ?suspend(transaction);
          15 ??? boolean ?newSynchronization? = ?( this .transactionSynchronization? == ?SYNCHRONIZATION_ALWAYS);
          16 ??? return ?newTransactionStatus(
          17 ?????definition,? null ,? false ,?newSynchronization,?debugEnabled,?suspendedResources);
          18 ??}

          19
          20 ?? if ?(definition.getPropagationBehavior()? == ?TransactionDefinition.PROPAGATION_REQUIRES_NEW)? {
          21 ??? if ?(debugEnabled)? {
          22 ????logger.debug( " Suspending?current?transaction,?creating?new?transaction?with?name?[ " ? +
          23 ??????definition.getName()? + ? " ] " );
          24 ???}

          25 ???Object?suspendedResources? = ?suspend(transaction);
          26 ???doBegin(transaction,?definition);
          27 ??? boolean ?newSynchronization? = ?( this .transactionSynchronization? != ?SYNCHRONIZATION_NEVER);
          28 ??? return ?newTransactionStatus(
          29 ?????definition,?transaction,? true ,?newSynchronization,?debugEnabled,?suspendedResources);
          30 ??}

          31
          32 ?? if ?(definition.getPropagationBehavior()? == ?TransactionDefinition.PROPAGATION_NESTED)? {
          33 ??? if ?( ! isNestedTransactionAllowed())? {
          34 ???? throw ? new ?NestedTransactionNotSupportedException(
          35 ?????? " Transaction?manager?does?not?allow?nested?transactions?by?default?-? " ? +
          36 ?????? " specify?'nestedTransactionAllowed'?property?with?value?'true' " );
          37 ???}

          38 ??? if ?(debugEnabled)? {
          39 ????logger.debug( " Creating?nested?transaction?with?name?[ " ? + ?definition.getName()? + ? " ] " );
          40 ???}

          41 ??? if ?(useSavepointForNestedTransaction())? {
          42 ???? // ?Create?savepoint?within?existing?Spring-managed?transaction,
          43 ???? // ?through?the?SavepointManager?API?implemented?by?TransactionStatus.
          44 ???? // ?Usually?uses?JDBC?3.0?savepoints.?Never?activates?Spring?synchronization.
          45 ????DefaultTransactionStatus?status? =
          46 ??????newTransactionStatus(definition,?transaction,? false ,? false ,?debugEnabled,? null );
          47 ????status.createAndHoldSavepoint();
          48 ???? return ?status;
          49 ???}

          50 ??? else ? {
          51 ???? // ?Nested?transaction?through?nested?begin?and?commit/rollback?calls.
          52 ???? // ?Usually?only?for?JTA:?Spring?synchronization?might?get?activated?here
          53 ???? // ?in?case?of?a?pre-existing?JTA?transaction.
          54 ????doBegin(transaction,?definition);
          55 ???? boolean ?newSynchronization? = ?( this .transactionSynchronization? != ?SYNCHRONIZATION_NEVER);
          56 ???? return ?newTransactionStatus(definition,?transaction,? true ,?newSynchronization,?debugEnabled,? null );
          57 ???}

          58 ??}

          59
          最后,txInfo被綁定到當前線程上作為當前事務:
          ?
          txInfo.bindToThread()
          ?
          然后,調用實際的目標類的方法并捕捉異常:
          ?
          try ? {
          ???
          // ?This?is?an?around?advice.
          ???
          // ?Invoke?the?next?interceptor?in?the?chain.
          ???
          // ?This?will?normally?result?in?a?target?object?being?invoked.
          ???retVal? = ?invocation.proceed();
          ??}

          ??
          catch ?(Throwable?ex)? {
          ???
          // ?target?invocation?exception
          ???doCloseTransactionAfterThrowing(txInfo,?ex);
          ???
          throw ?ex;
          ??}

          ??
          finally ? {
          ???doFinally(txInfo);
          ??}

          ??doCommitTransactionAfterReturning(txInfo);
          ??
          return ?retVal;
          ?}

          另外一點,TransactionInfo的newTransactionStatus調用時如果參數的不是null,TransactionInfo.hasTransaction()方法返回true;
          ?
          重要提示:
          在spring中創建的事務代理類并是目標類的超類,只是一個實現這目標類接口的類,該類會調用目標類的方法,所在如果一個目標類中的方法調用自身的另一個事務方法,另一個方法只是作為普通方法來調用,并不會加入事務機制

          參考資料:
          1.Spring Reference Manual:http://static.springframework.org/spring/docs/1.2.x/reference/index.html
          2.Spring API doc:http://static.springframework.org/spring/docs/1.2.x/api/index.html
          3.Spring 的源代碼

          作者簡介:
          ????施祖陽,網名sylilzy,1979年生。
          ????2002年起從事軟件開發工作,主要研究為JAVA、Linux及相關技術。
          ????你可通過 sylilzy@163.com 與作者聯系。
          ?
          ?
          ?
          posted on 2006-06-14 18:34 sylilzy 閱讀(1095) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 合水县| 漠河县| 云霄县| 台北县| 萨嘎县| 化隆| 兴宁市| 镇巴县| 扎赉特旗| 大同市| 汤阴县| 孝昌县| 卓尼县| 巍山| 张家口市| 麻阳| 吉木乃县| 酉阳| 荔浦县| 全椒县| 连山| 蓬溪县| 竹溪县| 和林格尔县| 长丰县| 富裕县| 渭南市| 溧阳市| 富宁县| 晋江市| 河池市| 海伦市| 隆安县| 大英县| 邯郸市| 乐清市| 建德市| 泰州市| 阿城市| 沾化县| 许昌县|