spring事物配置使用方法

本文转自:http://www.blogjava.net/robbie/archive/2009/04/05/264003.html  、

 http://developer.51cto.com/art/200906/129854.htm 、 http://www.cnblogs.com/rushoooooo/archive/2011/08/28/2155960.html

Spring 的事务管理方式从大的方向上来讲,分为 编程式事务和声明式事务,编程式事务就是实现PlatformTransactionManager 接口 和 TransactionTemplate 两种方式。

1. TransactionTempale采用和其他Spring模板,如JdbcTempalte和HibernateTemplate一样的方法。它使用回调方法,把应用程序从处理取得和释放资源中解脱出来。如同其他模板,TransactionTemplate是线程安全的。代码片段:

 

 

[java]  view plain  copy
 
  1. Object result = tt.execute(new TransactionCallback()...{    
  2. public Object doTransaction(TransactionStatus status)...{    
  3. updateOperation();    
  4. return resultOfUpdateOperation();    
  5. }    
  6. });  

 

使用TransactionCallback()可以返回一个值。如果使用TransactionCallbackWithoutResult则没有返回值。

2. 也可以使用PlatformTransactionManager直接管理事务。简单地通过一个bean引用给你的bean传递一个你使用的 PlatformTransaction对象。然后,使用TransactionDefinition和TransactionStatus对象就可以发起、回滚、提交事务。如下片段:

 

[java]  view plain  copy
 
  1.  //new 一个事务   
  2. DefaultTransactionDefinition def= new DefaultTransactionDefinition();   
  3.   def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);  
  4.   //初始化事务,参数定义事务的传播类型;   
  5.   TransactionStatus status = transactionManager.getTransaction(def);   
  6. //获得事务状态   
  7. try...{   
  8.    ……………..                                   
  9.   //提交事务;  
  10.   transactionManager.commit(status);    
  11. }catch(…..)...{                    
  12.  //回滚事务;                                  
  13.  transactionManager.rollback(status);    
  14. }   

 

Spring也提供声明式事务管理。这是通过AOP实现的。大多数Spring用户选择声明式事务管理,这是最少影响应用代码的选择,因而这是和非侵入性的轻量级容器的观念是一致的。

1)通常通过TransactionProxyFactoryBean设置Spring事务代理。需要一个目标对象包装在事务代理中。这个目标对象一般是一个普通Javabean。当我们定义TransactionProxyFactoryBean时,必须提供一个相关的 PlatformTransactionManager的引用和事务属性。事务属性含有事务定义。

 

 Spring配置文件 applicationContext.xml 中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。

    DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为 HibernateTransactionManager。

  具体如下图:

Spring事务配置 (2)

 

 根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:

    第一种方式:每个Bean都有一个代理

<? xml version="1.0" encoding="UTF-8" ?>
< beans  xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context
="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xsi:schemaLocation ="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
>

    
< bean  id ="sessionFactory"   
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >   
        
< property  name ="configLocation"  value ="classpath:hibernate.cfg.xml"   />   
        
< property  name ="configurationClass"  value ="org.hibernate.cfg.AnnotationConfiguration"   />
    
</ bean >   

    
<!--  定义事务管理器(声明式的事务)  -->   
    
< bean  id ="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        
< property  name ="sessionFactory"  ref ="sessionFactory"   />
    
</ bean >
    
    
<!--  配置DAO  -->
    
< bean  id ="userDaoTarget"  class ="com.bluesky.spring.dao.UserDaoImpl" >
        
< property  name ="sessionFactory"  ref ="sessionFactory"   />
    
</ bean >
    
    
< bean  id ="userDao"   
        class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >   
           
<!--  配置事务管理器  -->   
           
< property  name ="transactionManager"  ref ="transactionManager"   />      
        
< property  name ="target"  ref ="userDaoTarget"   />   
         
< property  name ="proxyInterfaces"  value ="com.bluesky.spring.dao.GeneratorDao"   />
        
<!--  配置事务属性  -->   
        
< property  name ="transactionAttributes" >   
            
< props >   
                
< prop  key ="*" > PROPAGATION_REQUIRED </ prop >
            
</ props >   
        
</ property >   
    
</ bean >   
</ beans >

    第二种方式:所有Bean共享一个代理基类

<? xml version="1.0" encoding="UTF-8" ?>
< beans  xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context
="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
>

    
< bean  id ="sessionFactory"   
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >   
        
< property  name ="configLocation"  value ="classpath:hibernate.cfg.xml"   />   
        
< property  name ="configurationClass"  value ="org.hibernate.cfg.AnnotationConfiguration"   />
    
</ bean >   

    
<!--  定义事务管理器(声明式的事务)  -->   
    
< bean  id ="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        
< property  name ="sessionFactory"  ref ="sessionFactory"   />
    
</ bean >
    
    
< bean  id ="transactionBase"   
            class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"   
            lazy-init
="true"  abstract ="true" >   
        
<!--  配置事务管理器  -->   
        
< property  name ="transactionManager"  ref ="transactionManager"   />   
        
<!--  配置事务属性  -->   
        
< property  name ="transactionAttributes" >   
            
< props >   
                
< prop  key ="*" > PROPAGATION_REQUIRED </ prop >   
            
</ props >   
        
</ property >   
    
</ bean >     
   
    
<!--  配置DAO  -->
    
< bean  id ="userDaoTarget"  class ="com.bluesky.spring.dao.UserDaoImpl" >
        
< property  name ="sessionFactory"  ref ="sessionFactory"   />
    
</ bean >
    
    
< bean  id ="userDao"  parent ="transactionBase"   >   
        
< property  name ="target"  ref ="userDaoTarget"   />    
    
</ bean >
</ beans >

第三种方式:使用拦截器

<? xml version="1.0" encoding="UTF-8" ?>
< beans  xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context
="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xsi:schemaLocation ="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
>

    
< bean  id ="sessionFactory"   
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >   
        
< property  name ="configLocation"  value ="classpath:hibernate.cfg.xml"   />   
        
< property  name ="configurationClass"  value ="org.hibernate.cfg.AnnotationConfiguration"   />
    
</ bean >   

    
<!--  定义事务管理器(声明式的事务)  -->   
    
< bean  id ="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        
< property  name ="sessionFactory"  ref ="sessionFactory"   />
    
</ bean >  
   
    
< bean  id ="transactionInterceptor"   
        class
="org.springframework.transaction.interceptor.TransactionInterceptor" >   
        
< property  name ="transactionManager"  ref ="transactionManager"   />   
        
<!--  配置事务属性  -->   
        
< property  name ="transactionAttributes" >   
            
< props >   
                
< prop  key ="*" > PROPAGATION_REQUIRED </ prop >   
            
</ props >   
        
</ property >   
    
</ bean >
      
    
< bean  class ="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >   
        
< property  name ="beanNames" >   
            
< list >   
                
< value > *Dao </ value >
             </ list >   
        
</ property >   
        
< property  name ="interceptorNames" >   
            
< list >   
                
< value > transactionInterceptor </ value >   
            
</ list >   
        
</ property >   
    
</ bean >   
  
    
<!--  配置DAO  -->
    
< bean  id ="userDao"  class ="com.bluesky.spring.dao.UserDaoImpl" >
        
< property  name ="sessionFactory"  ref ="sessionFactory"   />
    
</ bean >
</ beans >

第四种方式:使用tx标签配置的拦截器

<? xml version="1.0" encoding="UTF-8" ?>
< beans  xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context
="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xmlns:tx
="http://www.springframework.org/schema/tx"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>

    
< context:annotation-config  />
    
< context:component-scan  base-package ="com.bluesky"   />

    
< bean  id ="sessionFactory"   
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >   
        
< property  name ="configLocation"  value ="classpath:hibernate.cfg.xml"   />   
        
< property  name ="configurationClass"  value ="org.hibernate.cfg.AnnotationConfiguration"   />
    
</ bean >   

    
<!--  定义事务管理器(声明式的事务)  -->   
    
< bean  id ="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        
< property  name ="sessionFactory"  ref ="sessionFactory"   />
    
</ bean >

    
< tx:advice  id ="txAdvice"  transaction-manager ="transactionManager" >
        
< tx:attributes >
            
< tx:method  name ="*"  propagation ="REQUIRED"   />
        
</ tx:attributes >
    
</ tx:advice >
    
    
< aop:config >
        
< aop:pointcut  id ="interceptorPointCuts"
            expression
="execution(* com.bluesky.spring.dao.*.*(..))"   />
        
< aop:advisor  advice-ref ="txAdvice"
            pointcut-ref
="interceptorPointCuts"   />         
    
</ aop:config >       
</ beans >

第五种方式:全注解

<? xml version="1.0" encoding="UTF-8" ?>
< beans  xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context
="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xmlns:tx
="http://www.springframework.org/schema/tx"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>

    
< context:annotation-config  />
    
< context:component-scan  base-package ="com.bluesky"   />

    
< tx:annotation-driven  transaction-manager ="transactionManager" />

    
< bean  id ="sessionFactory"   
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >   
        
< property  name ="configLocation"  value ="classpath:hibernate.cfg.xml"   />   
        
< property  name ="configurationClass"  value ="org.hibernate.cfg.AnnotationConfiguration"   />
    
</ bean >   

    
<!--  定义事务管理器(声明式的事务)  -->   
    
< bean  id ="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        
< property  name ="sessionFactory"  ref ="sessionFactory"   />
    
</ bean >
    
</ beans >

此时在DAO上需加上@Transactional注解,如下:

package  com.bluesky.spring.dao;

import  java.util.List;

import  org.hibernate.SessionFactory;
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import  org.springframework.stereotype.Component;

import  com.bluesky.spring.domain.User;

@Transactional
@Component(
" userDao " )
public   class  UserDaoImpl  extends  HibernateDaoSupport  implements  UserDao {

    
public  List < User >  listUsers() {
        
return   this .getSession().createQuery( " from User " ).list();
    }
    
    
}

 

<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop><prop key="store*">PROPAGATION_REQUIRED</prop>

估计有好多朋友还没有弄清楚里面的值的意思,仔细看完下面应该知道自己什么情况下面应该使用什么样的声明。^_^

Spring中常用事务类型:

PROPAGATION_REQUIRED (propagation_required)--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

PROPAGATION_SUPPORTS(propagation_supports)--支持当前事务,如果当前没有事务,就以非事务方式执行。

PROPAGATION_MANDATORY(propagation_mandatory)--支持当前事务,如果当前没有事务,就抛出异常。

PROPAGATION_REQUIRES_NEW(propagation_requires_new)--新建事务,如果当前存在事务,把当前事务挂起。

PROPAGATION_NOT_SUPPORTED(propagation_not_support)--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

PROPAGATION_NEVER(propagation_never)--以非事务方式执行,如果当前存在事务,则抛出异常。

PROPAGATION_NESTED(propagation_nested)--如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。

Transaction后缀给声明式事务管理带来的好处

 良好的面向对象的程序,一般都使用接口和实现分离的模式。我在《事务管理最佳实践全面解析》一文中提出,用*Transaction和*Dao后缀这样的形式,区分方法的不同用途。

这样,可以提醒接口的实现者和方法的使用者注意到它们对于数据库连接和事务的依赖。

实际上,使用*Transaction后缀这样的命名方式,对于声明式事务管理也是很有用处的。如,Spring的事务管理中,我们一般使用方法名的匹配来应用声明式事务。

一、请看下面的Spring配置:

 

[java]  view plain  copy
 
  1. <bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">   
  2. <property name="transactionManager" ref="transactionManager"/>   
  3. <property name="transactionAttributes">   
  4. <props>   
  5.     <prop key="*">readOnly</prop>   
  6.     <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>   
  7.     <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>   
  8.     <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>   
  9.     <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>   
  10.     <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>   
  11.     <prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>   
  12.     <prop key="query*">PROPAGATION_REQUIRED, readOnly,-Exception</prop>   
  13.     <prop key="load*">PROPAGATION_REQUIRED, -Exception</prop>   
  14. </props>   
  15. </property>   
  16. </bean>   

 

这是来自于真实项目中的Spring声明式事务配置。我们对每一个业务层的实现类都应用了这样的事务配置。

我们对所有业务服务Service方法使用了只读事务。对以add,save,modify,update,delete,remove,load开头的方法都使用了事务。

但是,实际上,虽然我们开发的软件一个“信息管理系统”,是围绕数据库开发的。但是,在Service层,我们还是有很多不操作数据库的方法。

如,单纯根据业务逻辑进行计算的,适用缓存进行计算的,执行email发送,文件上传等等任务的方法,在这种配置下都不分青红皂白的应用了事务。

SpringAOP生成的代理对象代理了我们的服务实现类,所有的方法执行前后都被拦截,用来得到和关闭数据库连接,设置、提交和回滚事务。而不管这个方法是否用到了这个数据库。

如果遵照我提出的这个方法,使用*Transaction后缀来标识需要处理事务的方法,那么我们使用Spring声明式事务时,就可以非常精确、有效的应用事务

猜你喜欢

转载自ganxueyun.iteye.com/blog/2413182