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

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

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

    歡迎光臨郝學武的blog。

    spring事務管理

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



    spring
    事務管理

    zhuanzaizi:z_xiaofei168

          6.1、spring的事務管理器

     

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

     

    事務管理實現(xiàn)

    使用時機

    org.springframework.jdbc.datasource.

    DataSourceTransactionManager

    在單一的JDBC DataSource中管理事務

    org.springframework.orm.hibernate3.

    HibernateTransactionManager

    當持久化機制是Hibernate時,用它來管理職務

    org.springframework.orm.

    jpa.JpaTransactionManager

    JPA用作持久化時,用它來管理職務

    org.springframework.transaction.

    jta.JtaTransactionManager

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

     

    配置文件中的配置如下:

     

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

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

    </bean>

     

             6.2、事務屬性介紹

     

             1>.傳播行為

     

    傳播行為

    說明

    PROPAGATION_REQUIRED

    必須在一個事務中執(zhí)行。如果當前有一個事務正在進行,該方法將會在那個事務中執(zhí)行。否則要開始一個新事務。Spring事務傳播行為的默認值。

    PROPAGATION_SUPPORTS

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

    PROPAGATION_MANDATORY

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

    PROPAGATION_REQUIRES_NEW

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

    PROPAGATION_NOT_SUPPORTED

    不應在事務中進行。如果現(xiàn)有事務在進行就先暫停它

    PROPAGATION_NEVER

    不應在事務中進行。如果現(xiàn)有事務在進行就拋出異常

    PROPAGATION_NESTED

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

     

             2>.隔離級別

     

     

    隔離級別

    說明

    ISOLATION_DEFAULT

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

    ISOLATION_READ_UNCOMMITED

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

    ISOLATION_READ_COMMITED

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

    ISOLATION_REPEATABLE_READ

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

    ISOLATION_SERIALIZABLE

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

     

             3>.只讀提示

     

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

     

             4>.事務超時間隔

     

             還可以設置事務的超時間隔,讓事務在特定秒數(shù)后自動回滾,不必等它自己結束。由于計時是從事事務開始時算起的,所以它也得搭配傳播行為為 PROPAGATION_REQUIRED,PROPAGATION_REQUIRES_NEW,PROPAGATION_NESTED 來設置。

     

             5>.回滾規(guī)則

     

             當事務運行過程中拋出異常時,事務可以被聲明為回滾或者不回滾。默認情況下只在出現(xiàn)RuntimeExceptio才會回滾,而在出現(xiàn)受檢異常時不回滾。

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

     

          6.3、聲明式事務管理

     

              1>.基于xml配置方式

     

                       1步:定義事務通知

     

     

          第2部:把事務通知綁定到切入點

     

     

     

    Xml代碼 復制代碼 收藏代碼
    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事務管理器 -->  
    22.     <bean id="transactionManager"    
    23.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    24.         <!-- DataSource事務管理器需要數(shù)據(jù)源實例 -->  
    25.         <property name="dataSource" ref="dataSource"/>  
    26.     </bean>  
    27.        
    28.     <!-- 第1步:定義事務通知(主要是針對指定事務管理器對應的事務實現(xiàn)配置事務參數(shù)) -->  
    29.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
    30.         <tx:attributes>  
    31.             <!-- 對選定的方法配置詳細的事務屬性 -->  
    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.         <!--  聲明事務切入點(配置哪些類的哪些方法參與事務) -->    
    40.         <aop:pointcut id="AllServiceMethod"    
    41.                 expression="execution(* com.zxf.service.*.*(..))" />    
    42.         <!-- 通知器(把事務通知綁定到切入點) -->  
    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事務管理器   
    56.     <bean id="txManager2"      
    57.          class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
    58.         <property name="sessionFactory" ref ="sessionFactory"/>  
    59.     </bean>  
    60.     -->    
    61.     <!-- JPA事務管理器   
    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代碼 復制代碼 收藏代碼
    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事務管理器 -->  
    22.     <bean id="transactionManager"    
    23.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    24.         <!-- DataSource事務管理器需要數(shù)據(jù)源實例 -->  
    25.         <property name="dataSource" ref="dataSource"/>  
    26.     </bean>  
    27.     <!-- 啟用對事務注解的支持  -->  
    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注解指定接口、類或方法的事務屬性

     

    Java代碼 復制代碼 收藏代碼
    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è)務邏輯類--基于注解方式的聲明式事務管理配置 */  
    10. @Transactional //指定需要聲明式事務,事務屬性使用默認值   
    11. public class AccountServiceByTxAnnotation {   
    12.     private AccountDao accountDao;   
    13.     public void setAccountDao(AccountDao accountDao){   
    14.         this.accountDao = accountDao;   
    15.     }   
    16. }  

    Feedback

    # re: spring事務管理  回復  更多評論   

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

    # re: spring事務管理  回復  更多評論   

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

    # re: spring事務管理  回復  更多評論   

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

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


    網(wǎng)站導航:
     

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

    Copyright © 陜西BOY

    主站蜘蛛池模板: 亚洲天堂男人影院| 9420免费高清在线视频| 鲁大师在线影院免费观看| 国产精品成人无码免费| 久久精品国产亚洲AV无码偷窥| 特级毛片aaaa免费观看| 免费看a级黄色片| 亚洲kkk4444在线观看| 青青青国产在线观看免费| 久久亚洲国产精品| 日本不卡免费新一区二区三区| 国产精品亚洲综合专区片高清久久久| 亚洲成人一级电影| 国产精品久久久久久久久免费| 亚洲黄色免费网站| 在线观看日本免费a∨视频| 亚洲免费在线视频播放| 中文字幕影片免费在线观看 | 精品久久久久久亚洲| 亚洲一卡二卡三卡| 成人无遮挡裸免费视频在线观看| 亚洲国产精品日韩在线| 亚洲精品国产精品乱码不卞| 成人国产网站v片免费观看| 亚洲毛片一级带毛片基地| 噼里啪啦电影在线观看免费高清| 久久精品免费大片国产大片| 亚洲AV中文无码乱人伦下载 | 成年黄网站色大免费全看| 黄页网址大全免费观看12网站| 亚洲天堂免费在线视频| 国产麻豆成人传媒免费观看| 亚洲欧洲日韩国产综合在线二区| 免费观看无遮挡www的小视频| 亚洲欧美日韩综合久久久| 蜜桃精品免费久久久久影院| 3344免费播放观看视频| 亚洲AV无码专区亚洲AV桃| 精品亚洲成α人无码成α在线观看| 成年18网站免费视频网站| 国产无遮挡色视频免费观看性色|