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

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

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

    歡迎光臨郝學(xué)武的blog。

    spring事務(wù)管理

    Posted on 2011-05-16 11:01 陜西BOY 閱讀(2051) 評論(3)  編輯  收藏



    spring
    事務(wù)管理

    zhuanzaizi:z_xiaofei168

          6.1spring的事務(wù)管理器

     

    Spring框架并沒有直接管理用戶的應(yīng)用系統(tǒng)中的事務(wù),它只是提供許多供用戶選擇的事務(wù)管理器,然后將事務(wù)管理的責(zé)任委托給與此事務(wù)管理器對應(yīng)的持久化技術(shù)的事務(wù)實(shí)現(xiàn)。 

     

    事務(wù)管理實(shí)現(xiàn)

    使用時(shí)機(jī)

    org.springframework.jdbc.datasource.

    DataSourceTransactionManager

    在單一的JDBC DataSource中管理事務(wù)

    org.springframework.orm.hibernate3.

    HibernateTransactionManager

    當(dāng)持久化機(jī)制是Hibernate時(shí),用它來管理職務(wù)

    org.springframework.orm.

    jpa.JpaTransactionManager

    當(dāng)JPA用作持久化時(shí),用它來管理職務(wù)

    org.springframework.transaction.

    jta.JtaTransactionManager

    使用一個(gè)JTA實(shí)現(xiàn)來管理事務(wù)。在一個(gè)事務(wù)跨越多個(gè)資源時(shí)必須使用

     

    配置文件中的配置如下:

     

    <bean id=”transactionManager” class=” org.springframework.jdbc.datasource. DataSourceTransactionManager”>

                       <property name=”dataSource” ref=” dataSource”/>

    </bean>

     

             6.2、事務(wù)屬性介紹

     

             1>.傳播行為

     

    傳播行為

    說明

    PROPAGATION_REQUIRED

    必須在一個(gè)事務(wù)中執(zhí)行。如果當(dāng)前有一個(gè)事務(wù)正在進(jìn)行,該方法將會(huì)在那個(gè)事務(wù)中執(zhí)行。否則要開始一個(gè)新事務(wù)。Spring事務(wù)傳播行為的默認(rèn)值。

    PROPAGATION_SUPPORTS

    支持現(xiàn)有的事務(wù)。如果當(dāng)前沒有事務(wù)在進(jìn)行,就以非事務(wù)的方式執(zhí)行

    PROPAGATION_MANDATORY

    方法必須在一個(gè)現(xiàn)有事務(wù)中進(jìn)行,否則會(huì)拋出異常。

    PROPAGATION_REQUIRES_NEW

    必須在它自己的新啟事務(wù)里進(jìn)行。如果現(xiàn)有事務(wù)在進(jìn)行就先暫停它

    PROPAGATION_NOT_SUPPORTED

    不應(yīng)在事務(wù)中進(jìn)行。如果現(xiàn)有事務(wù)在進(jìn)行就先暫停它

    PROPAGATION_NEVER

    不應(yīng)在事務(wù)中進(jìn)行。如果現(xiàn)有事務(wù)在進(jìn)行就拋出異常

    PROPAGATION_NESTED

    如果現(xiàn)有事務(wù)正在進(jìn)行,則該方法運(yùn)行在一個(gè)嵌套式事務(wù)中。否則PROPAGATION_REQUIRED執(zhí)行

     

             2>.隔離級別

     

     

    隔離級別

    說明

    ISOLATION_DEFAULT

    使用底層數(shù)據(jù)庫默認(rèn)的隔離級別spring事務(wù)隔離級別的默認(rèn)值

    ISOLATION_READ_UNCOMMITED

    充許另一個(gè)事務(wù)可以讀到這個(gè)事務(wù)未提交的數(shù)據(jù)可能導(dǎo)致臟讀、不可重復(fù)讀和幻讀。

    ISOLATION_READ_COMMITED

    保證一個(gè)事務(wù)修改的數(shù)據(jù)提交后才能被另一個(gè)事務(wù)讀取可能導(dǎo)致不可重復(fù)讀和幻讀。

    ISOLATION_REPEATABLE_READ

    要求對相同字段的多次讀取的結(jié)果必須相同,除非事務(wù)本身更新了數(shù)據(jù)可能導(dǎo)致幻讀。

    ISOLATION_SERIALIZABLE

    事務(wù)被處理為順序執(zhí)行可以防止臟讀、不可重復(fù)讀、幻讀。

     

             3>.只讀提示

     

             如果事務(wù)只對后端數(shù)據(jù)進(jìn)行讀操作,則后端數(shù)據(jù)庫可以采用一些優(yōu)化措施來提高執(zhí)行效率。但必須在事務(wù)中才有效。也就是說要搭配傳播行為PROPAGATION_REQUIREDPROPAGATION_REQUIRES_NEWPROPAGATION_NESTED 來設(shè)置。

     

             4>.事務(wù)超時(shí)間隔

     

             還可以設(shè)置事務(wù)的超時(shí)間隔,讓事務(wù)在特定秒數(shù)后自動(dòng)回滾,不必等它自己結(jié)束。由于計(jì)時(shí)是從事事務(wù)開始時(shí)算起的,所以它也得搭配傳播行為為 PROPAGATION_REQUIREDPROPAGATION_REQUIRES_NEWPROPAGATION_NESTED 來設(shè)置。

     

             5>.回滾規(guī)則

     

             當(dāng)事務(wù)運(yùn)行過程中拋出異常時(shí),事務(wù)可以被聲明為回滾或者不回滾。默認(rèn)情況下只在出現(xiàn)RuntimeExceptio才會(huì)回滾,而在出現(xiàn)受檢異常時(shí)不回滾。

             當(dāng)然,也可以改變這種回滾規(guī)則,可以聲明一個(gè)事務(wù)在出現(xiàn)特定的受檢異常時(shí)能回滾。也可以聲明一個(gè)事務(wù)在出現(xiàn)特定的非受檢異常時(shí)不回滾。

     

          6.3、聲明式事務(wù)管理

     

              1>.基于xml配置方式

     

                       1步:定義事務(wù)通知

     

     

          第2部:把事務(wù)通知綁定到切入點(diǎn)

     

     

     

    Xml代碼 復(fù)制代碼 收藏代碼
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2.   
    3. <beans xmlns="http://www.springframework.org/schema/beans"  
    4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5.         xmlns:aop="http://www.springframework.org/schema/aop"  
    6.         xmlns:tx="http://www.springframework.org/schema/tx"  
    7.         xsi:schemaLocation="   
    8.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
    9.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
    10.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
    11.        
    12.     <!-- 配置不帶連接池的數(shù)據(jù)源 -->  
    13.     <bean id="dataSource"    
    14.           class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    15.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
    16.         <property name="url" value="jdbc:mysql:///spring" />  
    17.         <property name="username" value="root" />  
    18.         <property name="password" value="123" />  
    19.     </bean>  
    20.        
    21.     <!-- JDBC事務(wù)管理器 -->  
    22.     <bean id="transactionManager"    
    23.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    24.         <!-- DataSource事務(wù)管理器需要數(shù)據(jù)源實(shí)例 -->  
    25.         <property name="dataSource" ref="dataSource"/>  
    26.     </bean>  
    27.        
    28.     <!-- 第1步:定義事務(wù)通知(主要是針對指定事務(wù)管理器對應(yīng)的事務(wù)實(shí)現(xiàn)配置事務(wù)參數(shù)) -->  
    29.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
    30.         <tx:attributes>  
    31.             <!-- 對選定的方法配置詳細(xì)的事務(wù)屬性 -->  
    32.             <tx:method name="find*" read-only="true" />  
    33.             <tx:method name="*"/>  
    34.         </tx:attributes>  
    35.     </tx:advice>  
    36.        
    37.     <!--  第2步:AOP配置 -->  
    38.     <aop:config>  
    39.         <!--  聲明事務(wù)切入點(diǎn)(配置哪些類的哪些方法參與事務(wù)) -->    
    40.         <aop:pointcut id="AllServiceMethod"    
    41.                 expression="execution(* com.zxf.service.*.*(..))" />    
    42.         <!-- 通知器(把事務(wù)通知綁定到切入點(diǎn)) -->  
    43.         <aop:advisor pointcut-ref="AllServiceMethod" advice-ref="txAdvice" />    
    44.     </aop:config>  
    45.        
    46.        
    47.     <!-- 以下是Spring容器管理的Bean -->  
    48.     <bean id="accountDao" class="com.zxf.dao.AccountDaoJDBCImpl">  
    49.         <property name="dataSource" ref="dataSource" />  
    50.     </bean>  
    51.     <bean id="accountService" class="com.zxf.service.AccountService">  
    52.         <property name="accountDao" ref="accountDao"/>  
    53.     </bean>  
    54.        
    55.     <!-- Hibernate事務(wù)管理器   
    56.     <bean id="txManager2"      
    57.          class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
    58.         <property name="sessionFactory" ref ="sessionFactory"/>  
    59.     </bean>  
    60.     -->    
    61.     <!-- JPA事務(wù)管理器   
    62.     <bean id="txManager3"      
    63.          class="org.springframework.orm.jpa.JpaTransactionManager">  
    64.         <property name="entityManagerFactory" ref ="entityManagerFactory"/>  
    65.     </bean>  
    66.     -->    
    67. </beans>  

     

     

       2>.基于注解方式

          1步:在spring配置文件中啟用對AspectJ注解的支持

     

     

    Xml代碼 復(fù)制代碼 收藏代碼
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2.   
    3. <beans xmlns="http://www.springframework.org/schema/beans"  
    4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5.         xmlns:aop="http://www.springframework.org/schema/aop"  
    6.         xmlns:tx="http://www.springframework.org/schema/tx"  
    7.         xsi:schemaLocation="   
    8.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
    9.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
    10.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
    11.        
    12.     <!-- 配置不帶連接池的數(shù)據(jù)源 -->  
    13.     <bean id="dataSource"    
    14.           class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    15.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
    16.         <property name="url" value="jdbc:mysql:///spring_04" />  
    17.         <property name="username" value="root" />  
    18.         <property name="password" value="root" />  
    19.     </bean>  
    20.        
    21.     <!-- JDBC事務(wù)管理器 -->  
    22.     <bean id="transactionManager"    
    23.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    24.         <!-- DataSource事務(wù)管理器需要數(shù)據(jù)源實(shí)例 -->  
    25.         <property name="dataSource" ref="dataSource"/>  
    26.     </bean>  
    27.     <!-- 啟用對事務(wù)注解的支持  -->  
    28.     <tx:annotation-driven transaction-manager="transactionManager"/>  
    29.        
    30.        
    31.     <!-- 以下是Spring容器管理的Bean -->  
    32.     <bean id="accountDao" class="com.zxf.dao.AccountDaoJDBCImpl">  
    33.         <property name="dataSource" ref="dataSource" />  
    34.     </bean>  
    35.     <bean id="accountServiceByTxAnnotation"    
    36.           class="com.zxf.service.AccountServiceByTxAnnotation">  
    37.         <property name="accountDao" ref="accountDao"/>  
    38.     </bean>  
    39. </beans>  

     

        2步:用@Transactional注解指定接口、類或方法的事務(wù)屬性

     

    Java代碼 復(fù)制代碼 收藏代碼
    1. package com.zxf.service;   
    2.   
    3. import java.util.List;   
    4. import org.springframework.transaction.annotation.Transactional;   
    5.   
    6. import com.zxf.dao.AccountDao;   
    7. import com.zxf.domain.Account;   
    8.   
    9. /** Account業(yè)務(wù)邏輯類--基于注解方式的聲明式事務(wù)管理配置 */  
    10. @Transactional //指定需要聲明式事務(wù),事務(wù)屬性使用默認(rèn)值   
    11. public class AccountServiceByTxAnnotation {   
    12.     private AccountDao accountDao;   
    13.     public void setAccountDao(AccountDao accountDao){   
    14.         this.accountDao = accountDao;   
    15.     }   
    16. }  

    Feedback

    # re: spring事務(wù)管理  回復(fù)  更多評論   

    2011-08-23 23:03 by GavinMiao
    路過,用的是spring2.5么?2.0不可以這樣配置吧?

    # re: spring事務(wù)管理  回復(fù)  更多評論   

    2011-08-23 23:03 by GavinMiao
    圖片顯示不出來....

    # re: spring事務(wù)管理  回復(fù)  更多評論   

    2011-08-24 11:45 by haoxuewu
    @GavinMiao
    是2.5的

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


    網(wǎng)站導(dǎo)航:
     

    posts - 17, comments - 65, trackbacks - 0, articles - 28

    Copyright © 陜西BOY

    主站蜘蛛池模板: 奇米影视亚洲春色| 久久99毛片免费观看不卡| 成人免费男女视频网站慢动作| 91精品国产亚洲爽啪在线观看| 中国性猛交xxxxx免费看| 亚洲男人的天堂www| 十八禁视频在线观看免费无码无遮挡骂过 | 亚洲免费观看视频| 国产在线精品免费aaa片| 亚洲AV无码乱码在线观看富二代 | 久久精品网站免费观看| 亚洲AV无码无限在线观看不卡| 免费看又爽又黄禁片视频1000| 人妻无码一区二区三区免费| 久久亚洲国产成人精品性色| 黄在线观看www免费看| 亚洲中文无码永久免| 免费看国产一级片| 成在线人免费无码高潮喷水| 亚洲人成网站在线播放影院在线 | 91丁香亚洲综合社区| 日本不卡在线观看免费v| 亚洲国产免费综合| 久久久久久久亚洲Av无码| 免费无码精品黄AV电影| 成年免费a级毛片| 亚洲a在线视频视频| 在线观看成人免费| 久久久久女教师免费一区| 亚洲精品在线免费观看视频| 日韩黄色免费观看| 热久久这里是精品6免费观看| 亚洲成人高清在线观看| 免费观看亚洲人成网站| 久久久久久成人毛片免费看| 亚洲色最新高清av网站| 亚洲人成影院在线无码按摩店| 97性无码区免费| yellow视频免费在线观看| 亚洲乱码一二三四区麻豆| 亚洲日韩在线中文字幕第一页|