xhchc

          危波帆墻,笑談只在桃花上;與誰共尚,風吹萬里浪; 相依相偎,不做黃泉想;莫惆悵,碧波潮生,一蕭自狂放……

           

          Spring聲明式事務管理源碼解讀之事務提交(轉)

          簡介:上次說到spring聲明式事務管理的事務開始部分,按流程來講,下面應該提交事務了, spring的聲明式事務管理其實是比較復雜的,事實上這種復雜性正是由于事務本身的復雜性導致的,如果能用兩三句話就把這部分內容說清楚是不現實的,也是不成熟的,而我對這部分的理解也可能是不全面的,還是那句話,希望大家和我一起把本貼的質量提交起來。
          在下面的文章中,我講會多次提到第一篇文章,第一篇文章的地址是:http://www.javaeye.com/topic/87426
          如果要理解事務提交的話,理解事務開始是一個前提條件,所以請先看第一篇文章,再來看這篇
          如果你仔細看下去,我想肯定是有很多收獲,因為我們確實能從spring的代碼和思想中學到很多東西。

          正文:

          其實俺的感覺就是事務提交要比事務開始復雜,看事務是否提交我們還是要回到TransactionInterceptor類的invoke方法
          Java代碼 復制代碼
          1. public Object invoke(MethodInvocation invocation) throws Throwable {   
          2.         // Work out the target class: may be <code>null</code>.   
          3.         // The TransactionAttributeSource should be passed the target class   
          4.         // as well as the method, which may be from an interface   
          5.         Class targetClass = (invocation.getThis() != null) ? invocation.getThis().getClass() : null;   
          6.            
          7.         // Create transaction if necessary.   
          8.         TransactionInfo txInfo = createTransactionIfNecessary(invocation.getMethod(), targetClass);   
          9.   
          10.         Object retVal = null;   
          11.         try {   
          12.             // This is an around advice.   
          13.             // Invoke the next interceptor in the chain.   
          14.             // This will normally result in a target object being invoked.   
          15.             retVal = invocation.proceed();   
          16.         }   
          17.         catch (Throwable ex) {   
          18.             // target invocation exception   
          19.             doCloseTransactionAfterThrowing(txInfo, ex);   
          20.             throw ex;   
          21.         }   
          22.         finally {   
          23.             doFinally(txInfo);//業務方法出棧后必須先執行的一個方法   
          24.         }   
          25.         doCommitTransactionAfterReturning(txInfo);   
          26.         return retVal;   
          27.     }  

          其中的doFinally(txInfo)那一行很重要,也就是說不管如何,這個doFinally方法都是要被調用的,為什么它這么重要呢,舉個例子:
          我們還是以propregation_required來舉例子吧,假設情況是這樣的,AService中有一個方法調用了BService中的,這兩個方法都處在事務體之中,他們的傳播途徑都是required。那么調用開始了,AService的方法首先入方法棧,并創建了TransactionInfo的實例,接著BService的方法入棧,又創建了一個TransactionInfo的實例,而重點要說明的是TransactionInfo是一個自身關聯的內部類,第二個方法入棧時,會給新創建的TransactionInfo的實例設置一個屬性,就是TransactionInfo對象中的private TransactionInfo oldTransactionInfo;屬性,這個屬性表明BService方法的創建的TransactionInfo對象是有一個old的transactionInfo對象的,這個oldTransactionInfo對象就是AService方法入棧時創建的TransactionInfo對象,我們還記得在createTransactionIfNecessary方法里有這樣一個方法吧:
          Java代碼 復制代碼
          1. protected TransactionInfo createTransactionIfNecessary(Method method, Class targetClass) {   
          2.                 // We always bind the TransactionInfo to the thread, even if we didn't create   
          3.         // a new transaction here. This guarantees that the TransactionInfo stack   
          4.         // will be managed correctly even if no transaction was created by this aspect.   
          5.         txInfo.bindToThread();   
          6.         return txInfo;   
          7.     }   
          8.   
          9. 就是這個bindToThread()方法在作怪:   
          10. private void bindToThread() {   
          11.             // Expose current TransactionStatus, preserving any existing transactionStatus for   
          12.             // restoration after this transaction is complete.   
          13.             oldTransactionInfo = (TransactionInfo) currentTransactionInfo.get();   
          14.             currentTransactionInfo.set(this);   
          15.         }  

          如果當前線程中已經有了一個TransactionInfo,則拿出來放到新建的transactionInfo對象的oldTransactionInfo屬性中,然后再把新建的TransactionInfo設置到當前線程中。

          這里有一個概念要搞清楚,就是TransactionInfo對象并不是表明事務狀態的對象,表明事務狀態的對象是TransactionStatus對象,這個對象同樣是TransactionInfo的一個屬性(這一點,我在前面一篇文章中并沒有講清楚)。

          接下來BService中的那個方法返回,那么該它退棧了,它退棧后要做的就是doFinally方法,即把它的oldTransactionInfo設置到當前線程中(這個TransactionInfo對象顯然就是AService方法入棧時創建的,怎么現在又要設置到線程中去呢,原因就是BService的方法出棧時并不提交事務,因為BService的傳播途徑是required,所以要把棧頂的方法所創建transactioninfo給設置到當前線程中),即調用AService的方法時所創建的TransactionInfo對象。那么在AServie的方法出棧時同樣會設置TransactionInfo對象的oldTransactionInfo到當前線程,這時候顯然oldTransactionInfo是空的,但AService中的方法會提交事務,所以它的oldTransactionInfo也應該是空了。

          在這個小插曲之后,么接下來就應該是到提交事務了,之前在AService的方法出棧時,我們拿到了它入棧時創建的TransactionInfo對象,這個對象中包含了AService的方法事務狀態。即TransactionStatus對象,很顯然,太顯然了,事務提交中的任何屬性都和事務開始時的創建的對象息息相關,這個TransactionStatus對象哪里來的,我們再回頭看看createTransactionIfNessary方法吧:
          Java代碼 復制代碼
          1. protected TransactionInfo createTransactionIfNecessary(Method method, Class targetClass) {   
          2.             txInfo.newTransactionStatus(this.transactionManager.getTransaction(txAttr));   
          3.         }  

          再看看transactionManager.getTransaction(txAttr)方法吧:
          Java代碼 復制代碼
          1. public final TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {   
          2.            
          3.         else if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED ||   
          4.                 definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW ||   
          5.             definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {   
          6.             if (debugEnabled) {   
          7.                 logger.debug("Creating new transaction with name [" + definition.getName() + "]");   
          8.             }   
          9.             doBegin(transaction, definition);   
          10.             boolean newSynchronization = (this.transactionSynchronization != SYNCHRONIZATION_NEVER);   
          11.             return newTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, null);//注意這里的返回值,返回的就是一個TransactionStatus對象,這個對象表明了一個事務的狀態,比如說是否是一個新的事務,事務是否已經結束,等等,這個對象是非常重要的,在事務提交的時候還是會用到它的。        }   
          12.             }   
          13.     }  

          還有一點需要說明的是,AService的方法在執行之前創建的transactionstatus確實是通過這個方法創建的,但是,BService的方法在執行之前創建transactionstatus的方法就與這個不一樣了,下面會有詳解。

          回顧了事務開始時所調用的方法之后,是不是覺得現在對spring如何處理事務越來越清晰了呢。由于這么幾個方法的調用,每個方法入棧之前它的事務狀態就已經被設置好了。這個事務狀態就是為了在方法出棧時被調用而準備的。

          讓我們再次回到BService中的方法出棧的那個時間段,看看spring都做了些什么,我們知道,后入棧的肯定是先出棧,BService中的方法后入棧,拿它肯定要先出棧了,它出棧的時候是要判斷是否要提交事務,釋放資源的,讓我們來看看TransactionInterceptor的invoke的最后那個方法doCommitTransactionAfterReturning:

          Java代碼 復制代碼
          1. protected void doCommitTransactionAfterReturning(TransactionInfo txInfo) {   
          2.         if (txInfo != null && txInfo.hasTransaction()) {   
          3.             if (logger.isDebugEnabled()) {   
          4.                 logger.debug("Invoking commit for transaction on " + txInfo.joinpointIdentification());   
          5.             }   
          6.             this.transactionManager.commit(txInfo.getTransactionStatus());   
          7. //瞧:提交事務時用到了表明事務狀態的那個TransactionStatus對象了。   
          8.         }   
          9.     }  

          看這個方法的名字就知道spring是要在業務方法出棧時提交事務,貌似很簡單,但是事實是這樣的嗎? 我們接著往下看。
          Java代碼 復制代碼
          1. public final void commit(TransactionStatus status) throws TransactionException {   
          2.         DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status;   
          3.   
          4.         if (defStatus.isCompleted()) {   
          5.             throw new IllegalTransactionStateException(   
          6.                     "Transaction is already completed - do not call commit or rollback more than once per transaction");   
          7.         }   
          8.         if (defStatus.isLocalRollbackOnly()) {   
          9.             if (defStatus.isDebug()) {   
          10.                 logger.debug("Transactional code has requested rollback");   
          11.             }   
          12.             processRollback(defStatus);   
          13.             return;   
          14.         }   
          15.         if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) {   
          16.             if (defStatus.isDebug()) {   
          17.                 logger.debug("Global transaction is marked as rollback-only but transactional code requested commit");   
          18.             }   
          19.             processRollback(defStatus);   
          20.             throw new UnexpectedRollbackException(   
          21.                     "Transaction has been rolled back because it has been marked as rollback-only");   
          22.         }   
          23.   
          24.         processCommit(defStatus);   
          25.     }  

          上面這段代碼就是transactionmanager中的commit,但是看上去,它又把自己的職責分配給別人了,從代碼里我們看到,如果事務已經結束了就拋異常,如果事務是rollbackonly的,那么就rollback吧,但是按照正常流程,我們還是想來看一下,事務的提交,就是processCommit(status)這個方法吧。
          Java代碼 復制代碼
          1. private void processCommit(DefaultTransactionStatus status) throws TransactionException {   
          2.         try {   
          3.             boolean beforeCompletionInvoked = false;   
          4.             try {   
          5.                 triggerBeforeCommit(status);   
          6.                 triggerBeforeCompletion(status);   
          7.                 beforeCompletionInvoked = true;   
          8.                 if (status.hasSavepoint()) {   
          9.                     if (status.isDebug()) {   
          10.                         logger.debug("Releasing transaction savepoint");   
          11.                     }   
          12.                     status.releaseHeldSavepoint();   
          13.                 }   
          14.                 else if (status.isNewTransaction()) {//這個判斷非常重要,下面會詳細講解這個判斷的作用   
          15.                     if (status.isDebug()) {   
          16.                         logger.debug("Initiating transaction commit");   
          17.                     }   
          18.                     boolean globalRollbackOnly = status.isGlobalRollbackOnly();   
          19.                     doCommit(status);   
          20.                     // Throw UnexpectedRollbackException if we have a global rollback-only   
          21.                     // marker but still didn't get a corresponding exception from commit.   
          22.                     `````````````````````   
          23.     }  

          我們注意到,在判斷一個事務是否是新事務之前還有一個status.hasSavepoint()的判斷,我認為這個判斷事實上就是嵌套事務的判斷,即判斷這個事務是否是嵌套事務,如果不是嵌套事務,則再判斷它是否是一個新事務,下面這段話就非常重要了,BService的中的方法是先出棧的,也就是說在調用BService之前的創建的那個事務狀態對象在這里要先被判斷,但是由于在調用BService的方法之前已經創建了一個Transaction和Session(假設我們使用的是hibernate3),這時候在創建第二個TransactionInfo(再強調一下吧,TransactionInfo并不是Transaction,Transaction是真正的事務對象,TransactionInfo只不過是一個輔助類而已,用來記錄一系列狀態的輔助類)的TransactionStatus的時候就會進入下面這個方法(當然在這之前會判斷一下當前線程中是否已經有了一個SessionHolder對象,不清楚SessionHolder作用的同學情況第一篇文章),這個方法其實應該放到第一篇文章中講的,但是想到如果不講事務提交就講這個方法好像沒有這么貼切,廢話少說,我們來看一下吧:
          Java代碼 復制代碼
          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, nullfalse, 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, falsefalse, 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.   
          60.         // Assumably PROPAGATION_SUPPORTS.   
          61.         if (debugEnabled) {   
          62.             logger.debug("Participating in existing transaction");   
          63.         }   
          64.         boolean newSynchronization = (this.transactionSynchronization != SYNCHRONIZATION_NEVER);   
          65.         return newTransactionStatus(definition, transaction, false, newSynchronization, debugEnabled, null);   
          66.     }  

          我們看到這個方法其實很明了,就是什么樣的傳播途徑就創建什么樣的transactionstatus,這個方法是在事務開始時被調用的,拿到我們之前舉的例子中來看下,我們就恍然大悟了,原來,如果之前已經創建過事務,那個這個新建的transactionstauts就不應該是屬于一個newTransaction了,所以第3個參數就是false了。

          也就是說,在BService的方法出棧要要執行processcommit,但是由于BService的那個TransactionStatus不是一個newTransaction,所以它根本不會觸發這個動作:
          Java代碼 復制代碼
          1. else if (status.isNewTransaction()) {//這個判斷非常重要,下面會詳細講解這個判斷的作用   
          2.                     if (status.isDebug()) {   
          3.                         logger.debug("Initiating transaction commit");   
          4.                     }   
          5. boolean globalRollbackOnly = status.isGlobalRollbackOnly();   
          6.                     doCommit(status);   
          7. }  

          也就是說在BService的方法出棧后,事務是不會提交的。這完全符合propragation_required的模型。
          而在AService的方法出棧后,AService的方法所對應的那個TransactionStatus對象的newTransaction屬性是為true的,即它會觸發上面這段代碼,進行真正的事務提交。讓我們回想一下AService方法入棧之前創建TransactionStatus對象的情形吧:
          newTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, null);看到第3個參數為true沒有。

          那么事務該提交了吧,事務的提交我想使用過hibernate的人都知道怎么提交了:
          txObject.getSessionHolder().getTransaction().commit();
          從當前線程中拿到SessionHolder,再拿到開始事務的那個Transaction對象,然后再commit事務。在沒有用spring之前,我們經常這么做。呵呵。

          好吧,我已經說到了spring聲明式事務管理的70%到80%的內容了,這70%到80%的內容看上去還是非常容易理解的,如果把這兩篇文章認真看過,我相信會有所收獲的,剩下的內容需要靠大家自己去挖掘了,因為另剩下的內容可是需要花費很多時間的,因為牽扯的東西實在是太多了,呵呵。最后祝大家閱讀愉快,因為我的文筆實在是讓大家的眼睛受罪了。

          posted on 2008-04-30 11:48 chu 閱讀(1402) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          我的鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 渭源县| 康定县| 宁晋县| 通许县| 蚌埠市| 陈巴尔虎旗| 泰安市| 阳山县| 买车| 双鸭山市| 罗城| 文化| 清徐县| 河津市| 扎兰屯市| 隆化县| 博客| 车险| 沈阳市| 滨海县| 新田县| 韩城市| 高州市| 沅江市| 沛县| 任丘市| 红河县| 监利县| 方山县| 滨州市| 九寨沟县| 包头市| 定远县| 百色市| 宝坻区| 平乡县| 二连浩特市| 乐陵市| 保康县| 南汇区| 深州市|