Spring事务管理的三种方式

 
 

   一 、第一种:全注解声明式事务
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:p="http://www.springframework.org/schema/p"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xmlns:aop="http://www.springframework.org/schema/aop"  
  7.        xmlns:context="http://www.springframework.org/schema/context"  
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  10.        http://www.springframework.org/schema/tx  
  11.        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  12.        http://www.springframework.org/schema/aop  
  13.        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  14.        http://www.springframework.org/schema/context  
  15.        http://www.springframework.org/schema/context/spring-context-2.5.xsd"  
  16.        default-init-method="init">      
  17.        
  18.     <!-- 引入jdbc配置文件 -->  
  19.     <context:property-placeholder location="classpath:jdbc.properties" />  
  20.       
  21.     <!--创建jdbc数据源 -->  
  22.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  23.         <property name="driverClassName" value="${driver}" />  
  24.         <property name="url" value="${url}" />  
  25.         <property name="username" value="${username}" />  
  26.         <property name="password" value="${password}" />  
  27.     </bean>  
  28.    
  29.     <!-- 创建SqlSessionFactory,同时指定数据源 -->  
  30.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  31.         <property name="dataSource" ref="dataSource" />  
  32.         <!-- 配置sql映射文件所在位置  注意:默认与mapper类位置相同   -->  
  33.         <property name="mapperLocations" value="classpath:sqlmap/*.xml" />  
  34.     </bean>  
  35.    
  36.     <!-- 配置事务管理器:第一种 全注解声明式事务  -->  
  37.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  38.         <property name="dataSource" ref="dataSource" />  
  39.     </bean>  
  40.     <tx:annotation-driven transaction-manager="transactionManager" />  
  41.     <!-- 第一种 结束位置 -->  
  42.     <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->  
  43.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  44.         <property name="basePackage" value="com.xieke.test.mapper" />  
  45.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />    
  46.         <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->    
  47.     </bean>  
  48.       
  49. </beans>  

   使用时:在需要事务控制的类上加上@Transactional注解就可以了.

   二、第二种:使用tx标签配置的拦截器声明式事务

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.        xmlns:p="http://www.springframework.org/schema/p"    
  5.        xmlns:tx="http://www.springframework.org/schema/tx"    
  6.        xmlns:aop="http://www.springframework.org/schema/aop"    
  7.        xmlns:context="http://www.springframework.org/schema/context"    
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans    
  9.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  10.        http://www.springframework.org/schema/tx    
  11.        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
  12.        http://www.springframework.org/schema/aop    
  13.        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    
  14.        http://www.springframework.org/schema/context    
  15.        http://www.springframework.org/schema/context/spring-context-2.5.xsd"    
  16.        default-init-method="init">        
  17.          
  18.     <!-- 引入jdbc配置文件 -->    
  19.     <context:property-placeholder location="classpath:jdbc.properties" />    
  20.         
  21.     <!--创建jdbc数据源 -->    
  22.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">    
  23.         <property name="driverClassName" value="${driver}" />    
  24.         <property name="url" value="${url}" />    
  25.         <property name="username" value="${username}" />    
  26.         <property name="password" value="${password}" />    
  27.     </bean>    
  28.      
  29.     <!-- 创建SqlSessionFactory,同时指定数据源 -->    
  30.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    
  31.         <property name="dataSource" ref="dataSource" />    
  32.         <!-- 配置sql映射文件所在位置  注意:默认与mapper类位置相同   -->    
  33.         <property name="mapperLocations" value="classpath:sqlmap/*.xml" />    
  34.     </bean>    
  35.         
  36.     <!-- 配置事务管理器:第二种 使用tx标签配置的拦截器声明式事务  -->    
  37.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
  38.         <property name="dataSource" ref="dataSource" />    
  39.     </bean>    
  40.     <!-- 配置事务的传播特性  -->    
  41.     <tx:advice id="txAdvice" transaction-manager="transactionManager">    
  42.         <tx:attributes>    
  43.             <tx:method name="add*" propagation="NESTED" />    
  44.             <tx:method name="del*" propagation="NESTED" />    
  45.             <tx:method name="*" read-only="true" />    
  46.         </tx:attributes>    
  47.     </tx:advice>    
  48.     <!-- 配置事务的切入点  -->    
  49.     <aop:config>    
  50.         <aop:pointcut id="targetMethod" expression="execution(* com.xieke.test.service.impl.*.*(..))" />    
  51.         <aop:advisor advice-ref="txAdvice" pointcut-ref="targetMethod" />    
  52.     </aop:config>    
  53.     <!-- 第二种 结束结束位置 -->    
  54.      
  55.     <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->    
  56.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    
  57.         <property name="basePackage" value="com.xieke.test.mapper" />    
  58.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />      
  59.         <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->      
  60.     </bean>    
  61.         
  62. </beans>    

    三、 第三种:使用拦截器声明式事务 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:p="http://www.springframework.org/schema/p"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xmlns:aop="http://www.springframework.org/schema/aop"  
  7.        xmlns:context="http://www.springframework.org/schema/context"  
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  10.        http://www.springframework.org/schema/tx  
  11.        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  12.        http://www.springframework.org/schema/aop  
  13.        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  14.        http://www.springframework.org/schema/context  
  15.        http://www.springframework.org/schema/context/spring-context-2.5.xsd"  
  16.        default-init-method="init">      
  17.        
  18.     <!-- 引入jdbc配置文件 -->  
  19.     <context:property-placeholder location="classpath:jdbc.properties" />  
  20.       
  21.     <!--创建jdbc数据源 -->  
  22.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  23.         <property name="driverClassName" value="${driver}" />  
  24.         <property name="url" value="${url}" />  
  25.         <property name="username" value="${username}" />  
  26.         <property name="password" value="${password}" />  
  27.     </bean>  
  28.    
  29.     <!-- 创建SqlSessionFactory,同时指定数据源 -->  
  30.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  31.         <property name="dataSource" ref="dataSource" />  
  32.         <!-- 配置sql映射文件所在位置  注意:默认与mapper类位置相同   -->  
  33.         <property name="mapperLocations" value="classpath:sqlmap/*.xml" />  
  34.     </bean>  
  35.       
  36.     <!-- 配置事务管理器:第三种 使用拦截器声明式事务  -->      
  37.     <bean id="transactionManager"    
  38.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
  39.         <property name="dataSource" ref="dataSource" />   
  40.     </bean>     
  41.     <bean id="transactionInterceptor"      
  42.         class="org.springframework.transaction.interceptor.TransactionInterceptor">      
  43.         <property name="transactionManager" ref="transactionManager" />  
  44.         <!-- 配置事务属性   -->  
  45.         <property name="transactionAttributes">      
  46.             <props>  
  47.                 <!-- PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,则新建一个事务    -->  
  48.                 <prop key="add*">PROPAGATION_REQUIRED</prop>  
  49.                 <prop key="del*">PROPAGATION_REQUIRED</prop>      
  50.             </props>      
  51.         </property>      
  52.     </bean>    
  53.     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">      
  54.         <property name="beanNames">      
  55.             <list>      
  56.                 <value>*ServiceImpl</value>    
  57.             </list>      
  58.         </property>      
  59.         <property name="interceptorNames">      
  60.             <list>      
  61.                 <value>transactionInterceptor</value>      
  62.             </list>      
  63.         </property>      
  64.     </bean>      
  65.     <!-- 第三种 结束位置 -->  
  66.    
  67.     <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->  
  68.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  69.         <property name="basePackage" value="com.xieke.test.mapper" />  
  70.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />    
  71.         <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->    
  72.     </bean>  
  73.       
  74. </beans>  

 

Spring事务的传播属性:

PROPAGATION_REQUIRED -- 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 
PROPAGATION_SUPPORTS -- 支持当前事务,如果当前没有事务,就以非事务方式执行。 
PROPAGATION_MANDATORY -- 支持当前事务,如果当前没有事务,就抛出异常。 
PROPAGATION_REQUIRES_NEW -- 新建事务,如果当前存在事务,把当前事务挂起。 
PROPAGATION_NOT_SUPPORTED -- 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 
PROPAGATION_NEVER -- 以非事务方式执行,如果当前存在事务,则抛出异常。 
PROPAGATION_NESTED -- 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。 

 

转载请注明出处:http://xieke90.iteye.com/blog/2260045

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值