夢之天堂

          我學(xué)故我知,我思故我在;java你我,happy你我——sylilzy

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            3 Posts :: 8 Stories :: 2 Comments :: 0 Trackbacks
          spring的事務(wù)處理詳解:調(diào)用一個(gè)方法前的事務(wù)處理過程(源代碼分析)
          ?
          sylilzy@163.com
          版權(quán)所有,轉(zhuǎn)載請注明出處
          ?
          實(shí)際上,在spring的事務(wù)中,只要該類被設(shè)置為了事務(wù)代理:
          ?
          攔截器都會(huì)創(chuàng)建一個(gè)TransactionInfo 對象:
          ?
          TransactionInfo txInfo = new TransactionInfo(txAttr, method);
          ?
          ?
          而且如果 只要被調(diào)用的方法設(shè)置了事務(wù)屬性(txAttr),不管是什么屬性都會(huì)調(diào)用:
          ?
          txInfo.newTransactionStatus(this.transactionManager.getTransaction(txAttr));
          ?
          根據(jù)該方法的事務(wù)屬性(definition )的不同,this.transactionManager.getTransaction(txAttr)的返回值會(huì)有所不同(代碼見AbstractPlatformTransactionManager),具體為以下幾種情況:
          1.當(dāng)前沒有事務(wù)時(shí)(即以下代碼中的((HibernateTransactionObject) transaction).hasTransaction()返回false),會(huì)返回以下幾種:
          ?
          ?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.當(dāng)前有事務(wù)時(shí)
          ?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被綁定到當(dāng)前線程上作為當(dāng)前事務(wù):
          ?
          txInfo.bindToThread()
          ?
          然后,調(diào)用實(shí)際的目標(biāo)類的方法并捕捉異常:
          ?
          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;
          ?}

          另外一點(diǎn),TransactionInfo的newTransactionStatus調(diào)用時(shí)如果參數(shù)的不是null,TransactionInfo.hasTransaction()方法返回true;
          ?
          重要提示:
          在spring中創(chuàng)建的事務(wù)代理類并是目標(biāo)類的超類,只是一個(gè)實(shí)現(xiàn)這目標(biāo)類接口的類,該類會(huì)調(diào)用目標(biāo)類的方法,所在如果一個(gè)目標(biāo)類中的方法調(diào)用自身的另一個(gè)事務(wù)方法,另一個(gè)方法只是作為普通方法來調(diào)用,并不會(huì)加入事務(wù)機(jī)制

          參考資料:
          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 的源代碼

          作者簡介:
          ????施祖陽,網(wǎng)名sylilzy,1979年生。
          ????2002年起從事軟件開發(fā)工作,主要研究為JAVA、Linux及相關(guān)技術(shù)。
          ????你可通過 sylilzy@163.com 與作者聯(lián)系。
          ?
          ?
          ?
          posted on 2006-06-14 18:34 sylilzy 閱讀(1092) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 裕民县| 鹤峰县| 资溪县| 平果县| 西贡区| 邯郸县| 肥城市| 无极县| 彩票| 宜章县| 东海县| 永靖县| 长葛市| 崇信县| 云梦县| 武山县| 襄樊市| 晋宁县| 金华市| 石狮市| 甘谷县| 迁西县| 溧阳市| 苏尼特左旗| 满城县| 且末县| 页游| 通渭县| 武安市| 青铜峡市| 苗栗县| 安乡县| 嘉祥县| 梅河口市| 邓州市| 五台县| 宜州市| 和硕县| 衡阳县| 陕西省| 罗山县|