开源框架spring学习之道:事务管理的深刻理解

来源:互联网 发布:来源百度地图大数据 编辑:程序博客网 时间:2024/05/16 07:45
Java代码  收藏代码
  1. spring事务管理  
  2.   
  3. author:z_xiaofei168  
  4.   
  5.       6.1、spring的事务管理器  
  6.    
  7. Spring框架并没有直接管理用户的应用系统中的事务,它只是提供许多供用户选择的事务管理器,然后将事务管理的责任委托给与此事务管理器对应的持久化技术的事务实现。   
  8.    
  9. 事务管理实现  
  10. 使用时机  
  11. org.springframework.jdbc.datasource.  
  12. DataSourceTransactionManager  
  13. 在单一的JDBC DataSource中管理事务  
  14. org.springframework.orm.hibernate3.  
  15. HibernateTransactionManager  
  16. 当持久化机制是Hibernate时,用它来管理职务  
  17. org.springframework.orm.  
  18. jpa.JpaTransactionManager  
  19. 当JPA用作持久化时,用它来管理职务  
  20. org.springframework.transaction.  
  21. jta.JtaTransactionManager  
  22. 使用一个JTA实现来管理事务。在一个事务跨越多个资源时必须使用  
  23.    
  24. 配置文件中的配置如下:  
  25.    
  26. <bean id=”transactionManager” class=” org.springframework.jdbc.datasource. DataSourceTransactionManager”>  
  27.                    <property name=”dataSource” ref=” dataSource”/>  
  28. </bean>  
  29.    
  30.          6.2、事务属性介绍  
  31.    
  32.          1>.传播行为  
  33.    
  34. 传播行为  
  35. 说明  
  36. PROPAGATION_REQUIRED  
  37. 必须在一个事务中执行。如果当前有一个事务正在进行,该方法将会在那个事务中执行。否则要开始一个新事务。Spring事务传播行为的默认值。  
  38. PROPAGATION_SUPPORTS  
  39. 支持现有的事务。如果当前没有事务在进行,就以非事务的方式执行  
  40. PROPAGATION_MANDATORY  
  41. 方法必须在一个现有事务中进行,否则会抛出异常。  
  42. PROPAGATION_REQUIRES_NEW  
  43. 必须在它自己的新启事务里进行。如果现有事务在进行就先暂停它  
  44. PROPAGATION_NOT_SUPPORTED  
  45. 不应在事务中进行。如果现有事务在进行就先暂停它  
  46. PROPAGATION_NEVER  
  47. 不应在事务中进行。如果现有事务在进行就抛出异常  
  48. PROPAGATION_NESTED  
  49. 如果现有事务正在进行,则该方法运行在一个嵌套式事务中。否则PROPAGATION_REQUIRED执行  
  50.    
  51.          2>.隔离级别  
  52.    
  53.    
  54. 隔离级别  
  55. 说明  
  56. ISOLATION_DEFAULT  
  57. 使用底层数据库默认的隔离级别spring事务隔离级别的默认值  
  58. ISOLATION_READ_UNCOMMITED  
  59. 充许另一个事务可以读到这个事务未提交的数据可能导致脏读、不可重复读和幻读。  
  60. ISOLATION_READ_COMMITED  
  61. 保证一个事务修改的数据提交后才能被另一个事务读取可能导致不可重复读和幻读。  
  62. ISOLATION_REPEATABLE_READ  
  63. 要求对相同字段的多次读取的结果必须相同,除非事务本身更新了数据可能导致幻读。  
  64. ISOLATION_SERIALIZABLE  
  65. 事务被处理为顺序执行可以防止脏读、不可重复读、幻读。  
  66.    
  67.          3>.只读提示  
  68.    
  69.          如果事务只对后端数据进行读操作,则后端数据库可以采用一些优化措施来提高执行效率。但必须在事务中才有效。也就是说要搭配传播行为PROPAGATION_REQUIRED,PROPAGATION_REQUIRES_NEW,PROPAGATION_NESTED 来设置。  
  70.    
  71.          4>.事务超时间隔  
  72.    
  73.          还可以设置事务的超时间隔,让事务在特定秒数后自动回滚,不必等它自己结束。由于计时是从事事务开始时算起的,所以它也得搭配传播行为为 PROPAGATION_REQUIRED,PROPAGATION_REQUIRES_NEW,PROPAGATION_NESTED 来设置。  
  74.    
  75.          5>.回滚规则  
  76.    
  77.          当事务运行过程中抛出异常时,事务可以被声明为回滚或者不回滚。默认情况下只在出现RuntimeExceptio才会回滚,而在出现受检异常时不回滚。  
  78.          当然,也可以改变这种回滚规则,可以声明一个事务在出现特定的受检异常时能回滚。也可以声明一个事务在出现特定的非受检异常时不回滚。  
  79.    
  80.       6.3、声明式事务管理  
  81.    
  82.           1>.基于xml配置方式  
  83.    
  84.                    第1步:定义事务通知  
  85.     
  86.    
  87.       第2部:把事务通知绑定到切入点  
  88.    
  89. Xml代码    
  90. <?xml version="1.0" encoding="UTF-8"?>    
  91.     
  92. <beans xmlns="http://www.springframework.org/schema/beans"    
  93.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  94.         xmlns:aop="http://www.springframework.org/schema/aop"    
  95.         xmlns:tx="http://www.springframework.org/schema/tx"    
  96.         xsi:schemaLocation="    
  97.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
  98.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd    
  99.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">    
  100.         
  101.     <!-- 配置不带连接池的数据源 -->    
  102.     <bean id="dataSource"     
  103.           class="org.springframework.jdbc.datasource.DriverManagerDataSource">    
  104.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />    
  105.         <property name="url" value="jdbc:mysql:///spring" />    
  106.         <property name="username" value="root" />    
  107.         <property name="password" value="123" />    
  108.     </bean>    
  109.         
  110.     <!-- JDBC事务管理器 -->    
  111.     <bean id="transactionManager"     
  112.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
  113.         <!-- DataSource事务管理器需要数据源实例 -->    
  114.         <property name="dataSource" ref="dataSource"/>    
  115.     </bean>    
  116.         
  117.     <!-- 第1步:定义事务通知(主要是针对指定事务管理器对应的事务实现配置事务参数) -->    
  118.     <tx:advice id="txAdvice" transaction-manager="transactionManager">    
  119.         <tx:attributes>    
  120.             <!-- 对选定的方法配置详细的事务属性 -->    
  121.             <tx:method name="find*" read-only="true" />    
  122.             <tx:method name="*"/>    
  123.         </tx:attributes>    
  124.     </tx:advice>    
  125.         
  126.     <!--  第2步:AOP配置 -->    
  127.     <aop:config>    
  128.         <!--  声明事务切入点(配置哪些类的哪些方法参与事务) -->     
  129.         <aop:pointcut id="AllServiceMethod"     
  130.                 expression="execution(* com.zxf.service.*.*(..))" />     
  131.         <!-- 通知器(把事务通知绑定到切入点) -->    
  132.         <aop:advisor pointcut-ref="AllServiceMethod" advice-ref="txAdvice" />     
  133.     </aop:config>    
  134.         
  135.         
  136.     <!-- 以下是Spring容器管理的Bean -->    
  137.     <bean id="accountDao" class="com.zxf.dao.AccountDaoJDBCImpl">    
  138.         <property name="dataSource" ref="dataSource" />    
  139.     </bean>    
  140.     <bean id="accountService" class="com.zxf.service.AccountService">    
  141.         <property name="accountDao" ref="accountDao"/>    
  142.     </bean>    
  143.         
  144.     <!-- Hibernate事务管理器    
  145.     <bean id="txManager2"       
  146.          class="org.springframework.orm.hibernate3.HibernateTransactionManager">    
  147.         <property name="sessionFactory" ref ="sessionFactory"/>    
  148.     </bean>    
  149.     -->     
  150.     <!-- JPA事务管理器    
  151.     <bean id="txManager3"       
  152.          class="org.springframework.orm.jpa.JpaTransactionManager">    
  153.         <property name="entityManagerFactory" ref ="entityManagerFactory"/>    
  154.     </bean>    
  155.     -->     
  156. </beans>    
  157.    
  158.    
  159.    2>.基于注解方式  
  160.       第1步:在spring配置文件中启用对AspectJ注解的支持  
  161.    
  162.    
  163. Xml代码    
  164. <?xml version="1.0" encoding="UTF-8"?>    
  165.     
  166. <beans xmlns="http://www.springframework.org/schema/beans"    
  167.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  168.         xmlns:aop="http://www.springframework.org/schema/aop"    
  169.         xmlns:tx="http://www.springframework.org/schema/tx"    
  170.         xsi:schemaLocation="    
  171.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
  172.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd    
  173.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">    
  174.         
  175.     <!-- 配置不带连接池的数据源 -->    
  176.     <bean id="dataSource"     
  177.           class="org.springframework.jdbc.datasource.DriverManagerDataSource">    
  178.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />    
  179.         <property name="url" value="jdbc:mysql:///spring_04" />    
  180.         <property name="username" value="root" />    
  181.         <property name="password" value="root" />    
  182.     </bean>    
  183.         
  184.     <!-- JDBC事务管理器 -->    
  185.     <bean id="transactionManager"     
  186.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
  187.         <!-- DataSource事务管理器需要数据源实例 -->    
  188.         <property name="dataSource" ref="dataSource"/>    
  189.     </bean>    
  190.     <!-- 启用对事务注解的支持  -->    
  191.     <tx:annotation-driven transaction-manager="transactionManager"/>    
  192.         
  193.         
  194.     <!-- 以下是Spring容器管理的Bean -->    
  195.     <bean id="accountDao" class="com.zxf.dao.AccountDaoJDBCImpl">    
  196.         <property name="dataSource" ref="dataSource" />    
  197.     </bean>    
  198.     <bean id="accountServiceByTxAnnotation"     
  199.           class="com.zxf.service.AccountServiceByTxAnnotation">    
  200.         <property name="accountDao" ref="accountDao"/>    
  201.     </bean>    
  202. </beans>    
  203.    
  204.     第2步:用@Transactional注解指定接口、类或方法的事务属性  
  205.    
  206. Java代码    
  207. package com.zxf.service;    
  208.     
  209. import java.util.List;    
  210. import org.springframework.transaction.annotation.Transactional;    
  211.     
  212. import com.zxf.dao.AccountDao;    
  213. import com.zxf.domain.Account;    
  214.     
  215. /** Account业务逻辑类--基于注解方式的声明式事务管理配置 */    
  216. @Transactional //指定需要声明式事务,事务属性使用默认值    
  217. public class AccountServiceByTxAnnotation {    
  218.     private AccountDao accountDao;    
  219.     public void setAccountDao(AccountDao accountDao){    
  220.         this.accountDao = accountDao;    
  221.     }    
  222. }    
原创粉丝点击