歡迎光臨郝學武的blog。

          spring事務管理

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



          spring
          事務管理

          zhuanzaizi:z_xiaofei168

                6.1spring的事務管理器

           

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

           

          事務管理實現

          使用時機

          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實現來管理事務。在一個事務跨越多個資源時必須使用

           

          配置文件中的配置如下:

           

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

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

          </bean>

           

                   6.2、事務屬性介紹

           

                   1>.傳播行為

           

          傳播行為

          說明

          PROPAGATION_REQUIRED

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

          PROPAGATION_SUPPORTS

          支持現有的事務。如果當前沒有事務在進行,就以非事務的方式執行

          PROPAGATION_MANDATORY

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

          PROPAGATION_REQUIRES_NEW

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

          PROPAGATION_NOT_SUPPORTED

          不應在事務中進行。如果現有事務在進行就先暫停它

          PROPAGATION_NEVER

          不應在事務中進行。如果現有事務在進行就拋出異常

          PROPAGATION_NESTED

          如果現有事務正在進行,則該方法運行在一個嵌套式事務中。否則PROPAGATION_REQUIRED執行

           

                   2>.隔離級別

           

           

          隔離級別

          說明

          ISOLATION_DEFAULT

          使用底層數據庫默認的隔離級別spring事務隔離級別的默認值

          ISOLATION_READ_UNCOMMITED

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

          ISOLATION_READ_COMMITED

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

          ISOLATION_REPEATABLE_READ

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

          ISOLATION_SERIALIZABLE

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

           

                   3>.只讀提示

           

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

           

                   4>.事務超時間隔

           

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

           

                   5>.回滾規則

           

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

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

           

                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.     <!-- 配置不帶連接池的數據源 -->  
          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事務管理器需要數據源實例 -->  
          25.         <property name="dataSource" ref="dataSource"/>  
          26.     </bean>  
          27.        
          28.     <!-- 第1步:定義事務通知(主要是針對指定事務管理器對應的事務實現配置事務參數) -->  
          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.     <!-- 配置不帶連接池的數據源 -->  
          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事務管理器需要數據源實例 -->  
          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業務邏輯類--基于注解方式的聲明式事務管理配置 */  
          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的

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


          網站導航:
           

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

          Copyright © 陜西BOY

          主站蜘蛛池模板: 酉阳| 老河口市| 万载县| 准格尔旗| 邵阳市| 太原市| 肇东市| 咸丰县| 北流市| 禹州市| 广平县| 太原市| 鹤山市| 湖北省| 南陵县| 永清县| 汨罗市| 沧州市| 武山县| 平乐县| 鄂托克旗| 嵩明县| 佛坪县| 鄂托克前旗| 贵定县| 彰化市| 历史| 闻喜县| 阿拉善右旗| 广丰县| 嘉祥县| 崇州市| 宁南县| 广安市| 皋兰县| 谷城县| 钟山县| 武义县| 维西| 祁连县| 慈利县|