<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Spring事務的傳播行為和隔離級別

    事務的傳播行為和隔離級別[transaction behavior and isolated level]

    一、Spring中事務的定義:
    key屬性確定代理應該給哪個方法增加事務行為。這樣的屬性最重要的部份是傳播行為。有以下選項可供使用:
    PROPAGATION_REQUIRED--支持當前事務,如果當前沒有事務,就新建一個事務。這是最常見的選擇。
    PROPAGATION_SUPPORTS--支持當前事務,如果當前沒有事務,就以非事務方式執行。
    PROPAGATION_MANDATORY--支持當前事務,如果當前沒有事務,就拋出異常。
    PROPAGATION_REQUIRES_NEW--新建事務,如果當前存在事務,把當前事務掛起。
    PROPAGATION_NOT_SUPPORTED--以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。
    PROPAGATION_NEVER--以非事務方式執行,如果當前存在事務,則拋出異常。

    重點分析PROPAGATION_REQUIRED 和 PROPAGATION_REQUIRED_NESTED的
    解惑 spring 嵌套事務
    ********TransactionDefinition 接口定義*******************
    /**  
          * Support a current transaction, create a new one if none exists.  
          * Analogous to EJB transaction attribute of the same name.  
          *

    This is typically the default setting of a transaction definition.  
          
    */  
         
    int PROPAGATION_REQUIRED = 0;  
     
         
    /**  
          * Support a current transaction, execute non-transactionally if none exists.  
          * Analogous to EJB transaction attribute of the same name.  
          *

    Note: For transaction managers with transaction synchronization,  
          * PROPAGATION_SUPPORTS is slightly different from no transaction at all,  
          * as it defines a transaction scopp that synchronization will apply for.  
          * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc)  
          * will be shared for the entire specified scope. Note that this depends on  
          * the actual synchronization configuration of the transaction manager.  
          * 
    @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization  
          
    */  
         
    int PROPAGATION_SUPPORTS = 1;  
     
         
    /**  
          * Support a current transaction, throw an exception if none exists.  
          * Analogous to EJB transaction attribute of the same name.  
          
    */  
         
    int PROPAGATION_MANDATORY = 2;  
     
         
    /**  
          * Create a new transaction, suspend the current transaction if one exists.  
          * Analogous to EJB transaction attribute of the same name.  
          *

    Note: Actual transaction suspension will not work on out-of-the-box  
          * on all transaction managers. This in particular applies to JtaTransactionManager,  
          * which requires the javax.transaction.TransactionManager to be  
          * made available it to it (which is server-specific in standard J2EE).  
          * 
    @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager  
          
    */  
         
    int PROPAGATION_REQUIRES_NEW = 3;  
     
         
    /**  
          * Execute non-transactionally, suspend the current transaction if one exists.  
          * Analogous to EJB transaction attribute of the same name.  
          *

    Note: Actual transaction suspension will not work on out-of-the-box  
          * on all transaction managers. This in particular applies to JtaTransactionManager,  
          * which requires the javax.transaction.TransactionManager to be  
          * made available it to it (which is server-specific in standard J2EE).  
          * 
    @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager  
          
    */  
         
    int PROPAGATION_NOT_SUPPORTED = 4;  
     
         
    /**  
          * Execute non-transactionally, throw an exception if a transaction exists.  
          * Analogous to EJB transaction attribute of the same name.  
          
    */  
         
    int PROPAGATION_NEVER = 5;  
     
         
    /**  
          * Execute within a nested transaction if a current transaction exists,  
          * behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB.  
          *

    Note: Actual creation of a nested transaction will only work on specific  
          * transaction managers. Out of the box, this only applies to the JDBC  
          * DataSourceTransactionManager when working on a JDBC 3.0 driver.  
          * Some JTA providers might support nested transactions as well.  
          * 
    @see org.springframework.jdbc.datasource.DataSourceTransactionManager  
          
    */  
         
    int PROPAGATION_NESTED = 6;  

    ********************sample***********************
    ServiceA {  
          
         /** 
          * 事務屬性配置為 PROPAGATION_REQUIRED 
          */ 
         void methodA() {  
             ServiceB.methodB();  
         }  
     
    }  
     
    ServiceB {  
          
         /** 
          * 事務屬性配置為 PROPAGATION_REQUIRED 
          */   
         void methodB() {  
         }  
          
    }     
    *************************************************
    1: 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也要回滾

    2:   PROPAGATION_SUPPORTS

    如果當前在事務中,即以事務的形式運行,如果當前不再一個事務中,那么就以非事務的形式運行
    這就跟平常用的普通非事務的代碼只有一點點區別了。不理這個,因為我也沒有覺得有什么區別

    3:   PROPAGATION_MANDATORY
    必須在一個事務中運行。也就是說,他只能被一個父事務調用。否則,他就要拋出異常。

    4:   PROPAGATION_REQUIRES_NEW

    這個就比較繞口了。 比如我們設計ServiceA.methodA的事務級別為PROPAGATION_REQUIRED,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事務仍然可能提交。

    5:   PROPAGATION_NOT_SUPPORTED
    當前不支持事務。比如ServiceA.methodA的事務級別是PROPAGATION_REQUIRED ,而ServiceB.methodB的事務級別是PROPAGATION_NOT_SUPPORTED ,
    那么當執行到ServiceB.methodB時,ServiceA.methodA的事務掛起,而他以非事務的狀態運行完,再繼續ServiceA.methodA的事務。

    6:   PROPAGATION_NEVER
    不能在事務中運行。假設ServiceA.methodA的事務級別是PROPAGATION_REQUIRED, 而ServiceB.methodB的事務級別是PROPAGATION_NEVER ,
    那么ServiceB.methodB就要拋出異常了。

    7:   PROPAGATION_NESTED
    理解Nested的關鍵是savepoint。他與PROPAGATION_REQUIRES_NEW的區別是,PROPAGATION_REQUIRES_NEW另起一個事務,將會與他的父事務相互獨立,
    而Nested的事務和他的父事務是相依的,他的提交是要等和他的父事務一塊提交的。也就是說,如果父事務最后回滾,他也要回滾的。
    而Nested事務的好處是他有一個savepoint。
    *****************************************
    ServiceA {  
          
         /** 
          * 事務屬性配置為 PROPAGATION_REQUIRED 
          */ 
         void methodA() {  
             try {
          //savepoint  
                 ServiceB.methodB();    //PROPAGATION_NESTED 級別
             } catch (SomeException) {  
                 // 執行其他業務, 如 ServiceC.methodC();  
             }  
         }  
     
    }  
    ********************************************
    也就是說ServiceB.methodB失敗回滾,那么ServiceA.methodA也會回滾到savepoint點上,ServiceA.methodA可以選擇另外一個分支,比如
    ServiceC.methodC,繼續執行,來嘗試完成自己的事務。
    但是這個事務并沒有在EJB標準中定義。

    二、Isolation Level(事務隔離等級):
    1、Serializable:最嚴格的級別,事務串行執行,資源消耗最大;

    2、REPEATABLE READ:保證了一個事務不會修改已經由另一個事務讀取但未提交(回滾)的數據。避免了“臟讀取”和“不可重復讀取”的情況,但是帶來了更多的性能損失。

    3、READ COMMITTED:大多數主流數據庫的默認事務等級,保證了一個事務不會讀到另一個并行事務已修改但未提交的數據,避免了“臟讀取”。該級別適用于大多數系統。

    4、Read Uncommitted:保證了讀取過程中不會讀取到非法數據。
    隔離級別在于處理多事務的并發問題。
    我們知道并行可以提高數據庫的吞吐量和效率,但是并不是所有的并發事務都可以并發運行,這需要查看數據庫教材的可串行化條件判斷了。
    這里就不闡述。

    我們首先說并發中可能發生的3中不討人喜歡的事情
    1:   Dirty reads--讀臟數據。也就是說,比如事務A的未提交(還依然緩存)的數據被事務B讀走,如果事務A失敗回滾,會導致事務B所讀取的的數據是錯誤的。

    2:   non-repeatable reads--數據不可重復讀。比如事務A中兩處讀取數據-total-的值。在第一讀的時候,total是100,然后事務B就把total的數據改成 200,事務A再讀一次,結果就發現,total竟然就變成200了,造成事務A數據混亂。

    3:   phantom reads--幻象讀數據,這個和non-repeatable reads相似,也是同一個事務中多次讀不一致的問題。但是non-repeatable reads的不一致是因為他所要取的數據集被改變了(比如total的數據),但是phantom reads所要讀的數據的不一致卻不是他所要讀的數據集改變,而是他的條件數據集改變。比如Select account.id where account.name="ppgogo*",第一次讀去了6個符合條件的id,第二次讀取的時候,由于事務b把一個帳號的名字由"dd"改成"ppgogo1",結果取出來了7個數據。
                              Dirty reads          non-repeatable reads            phantom reads
    Serializable                     不會                   不會                           不會
    REPEATABLE READ           不會                   不會                            會
    READ COMMITTED            不會                    會                             會
    Read Uncommitted            會                     會                             會

    三、readOnly

    事務屬性中的readOnly標志表示對應的事務應該被最優化為只讀事務。這是一個最優化提示。在一些情況下,一些事務策略能夠起到顯著的最優化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)時避免dirty checking(試圖“刷新”)。

    四、Timeout
           在事務屬性中還有定義“timeout”值的選項,指定事務超時為幾秒。在JTA中,這將被簡單地傳遞到J2EE服務器的事務協調程序,并據此得到相應的解釋。


    posted on 2009-11-24 12:13 MichaelLee 閱讀(245) 評論(0)  編輯  收藏 所屬分類: Spring

    <2009年11月>
    25262728293031
    1234567
    891011121314
    15161718192021
    22232425262728
    293012345

    導航

    統計

    公告

    ====Michael Lee====
    =Java Sofware Engineer=
    =Work @ Beijing=
    ---再煩,也別忘微笑;再急,也要注意語氣;再苦,也別忘堅持;再累,也要愛自己!---
    ---低調做人,你會一次比一次穩健;高調做事,你會一次比一次優秀---
    ---成功的時候不要忘記過去;失敗的時候不要忘記還有未來---

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 1000部拍拍拍18勿入免费视频软件 | 久久狠狠躁免费观看2020| 成人亚洲综合天堂| 噜噜综合亚洲AV中文无码| 成熟女人特级毛片www免费| 亚洲三级在线免费观看| 国内精品乱码卡1卡2卡3免费 | 亚洲AV无码乱码在线观看代蜜桃| 日本xxxx色视频在线观看免费| 久久精品国产亚洲av麻| 91成人免费观看| 亚洲午夜久久久精品电影院| 免费精品国偷自产在线在线| 国产精品亚洲四区在线观看| 免费高清av一区二区三区| 婷婷亚洲综合一区二区| 波多野结衣视频在线免费观看| 深夜免费在线视频| 亚洲桃色AV无码| 在线a免费观看最新网站| 亚洲精品二三区伊人久久| 日韩成人免费aa在线看| 一级毛片免费在线| 亚洲a在线视频视频| 免费毛片a在线观看67194| 亚洲国产无线乱码在线观看 | 亚洲三级电影网址| 免费可以看黄的视频s色| 黄床大片30分钟免费看| 亚洲人成人无码网www电影首页 | 国产精品区免费视频| 亚洲人成激情在线播放| 亚洲成AⅤ人影院在线观看 | 久草视频在线免费| 亚洲av纯肉无码精品动漫| 亚洲精品无码成人片在线观看 | 国产精品免费久久久久电影网| 久久久久久亚洲精品| 成人性生免费视频| 美女巨胸喷奶水视频www免费| 亚洲高清无在码在线无弹窗|