编程式事务、XML配置事务、注解实现事务

Spring2.0框架的事务处理有两大类:
1 编码式事务 , 这个不说.
2 声明式事务 , 就说这个.

声明式事务又有三种实现方法:
1 (第一种) 最早的方法,用TransactionProxyFactoryBean,他是一个有AOP代理功能的FactoryBean.他返回的对象有事务.
还要在spring的配置文件XML中配置,比较麻烦,不详细说.
Xml代码  收藏代码
<!-- 事务测试DAO --> 
<bean id="go_TestPOAO" class="pic.dao.transaction_test.TestPOAOImpl" parent="go_POAOBase"></bean>    
     
<!-- 事务测试DAO 声明式事务管理 --> 
<bean id="go_TestPOAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">   
        <property name="proxyInterfaces">   
            <list>    
                <value>pic.dao.transaction_test.TestPOAO</value>   
            </list>   
        </property>   
        <property name="target" ref="go_TestPOAO"/>    
        <property name="transactionManager" ref="transactionManager"/>   
        <property name="transactionAttributes">   
            <props>   
                <prop key="insert*">PROPAGATION_REQUIRED</prop>   
            </props>   
        </property>   
</bean>  
2 (第二种) 使用<tx:>来实现声明式事务 ,也要在spring的配置文件XML中配置,比较麻烦,不详细说.
Xml代码  收藏代码
<tx:advice id=""> 
..... 
</tx:advice> 
 
<aop:config> 
..... 
</aop:config> 


3 (第三种) 这个方法方便,使用注解来实现声明式事务, 下面详细说说这个方法:

第一步:引入<tx:>命名空间 ,在spring的配置文件中修改, beans根元素里多了三行,如下
Xml代码  收藏代码
<?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:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 

第二步:在spring的配置文件中修改,将所有具有@Transactional 注解的bean自动配置为声明式事务支持
Java代码  收藏代码
<!--JDBC事务管理器,根据你的情况使用不同的事务管理器,如果工程中有Hibernate,就用Hibernate的事务管理器 -->   
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
        <property name="dataSource"> 
            <ref local="dataSource"/> 
        </property> 
</bean>    
         
<!-- 用注解来实现事务管理 --> 
<tx:annotation-driven transaction-manager="transactionManager"/> 

第三步: 在接口或类的声明处 ,写一个@Transactional. 要是只的接口上写, 接口的实现类就会继承下来.
接口的实现类的具体方法,还可以覆盖类声明处的设置.
Java代码  收藏代码
@Transactional 
public class TestPOAOImpl extends POAOBase implements TestPOAO 
{    
    @Transactional(isolation = Isolation.READ_COMMITTED) 
    public void test1() 
    { 
        String sql = "INSERT INTO sy_test (NAME,AGE) VALUES('注解赵云',30)"; 
        execute(sql); 
 
        sql = "INSERT INTO sy_test (NAME,AGE) VALUES('注解张飞',26)"; 
        execute(sql); 
 
        int a = 9 / 0; //异常 
 
        sql = "INSERT INTO sy_test (NAME,AGE) VALUES('注解关羽',33)"; 
        execute(sql); 
        System.out.println("走完了"); 
    } 
//execute() 方法略... 


注意的几点:
1  @Transactional 只能被应用到public方法上, 对于其它非public的方法,如果标记了@Transactional也不会报错,但方法没有事务功能.

2 默认情况下,一个有事务方法, 遇到RuntiomeException 时会回滚 .  遇到 受检查的异常 是不会回滚 的. 要想所有异常都回滚,要加上 @Transactional( rollbackFor={Exception.class,其它异常}) .



@Transactional  的所有可选属性如下:

属性 类型 默认值 说明
propagation Propagation枚举 REQUIRED 事务传播属性 (下有说明)
isolation isolation枚举 DEFAULT 事务隔离级别 (另有说明)
readOnly boolean false 是否只读
timeout int -1 超时(秒)
rollbackFor Class[] {} 需要回滚的异常类
rollbackForClassName String[] {} 需要回滚的异常类名
noRollbackFor Class[] {} 不需要回滚的异常类
noRollbackForClassName String[] {} 不需要回滚的异常类名


=================================================================

XML配置 声明式事务 --  tx命名空间

采用声明式事务
1、声明式事务配置
* 配置SessionFactory
* 配置事务管理器
* 事务的传播特性
* 那些类那些方法使用事务
2、编写业务逻辑方法
* 继承HibernateDaoSupport类,使用HibernateTemplate来持久化,HibernateTemplate是Hibernate session的封装     * 默认的回滚是RuntimeException(包括继承RuntimeException的子类),普通异常不回滚  
 
* 在编写业务逻辑方法时,最好将异常一直往上抛出,在呈现层处理(struts)    

* spring的事务需要设置到业务方法上(事务边界定义到Facade类上),不要添加到Dao上
Java代码 
<?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: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.0.xsd  
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
     
    <!-- 配置SessionFactory --> 
    <bean id="sessionFactory"   class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" > 
      <property  name="configLocation"  value= "classpath:hibernate.cfg.xml" /> 
    </bean> 
    <!-- 配置事务管理器 --> 
    <bean id="transactionMgr"   class = "org.springframework.orm.hibernate3.HibernateTransactionManager" > 
     <property name="sessionFactory" > 
        <ref bean="sessionFactory" />            
     </property> 
    </bean> 
    <!-- 配置事务传播特性 --> 
    <tx:advice id="txAdvice"  transaction-manager= "transactionMgr" > 
         <tx:attributes> 
             <tx:method name="add*"  propagation= "REQUIRED" />  
              <tx:method name="del*"  propagation= "REQUIRED" /> 
              <tx:method name="update*"  propagation= "REQUIRED" /> 
              <tx:method name="*"  read-only= "true" /> 
         </tx:attributes> 
    </tx:advice> 
    <!-- 那些类使用事务 --> 
    <aop:config> 
      <aop:pointcut id="point-cut"  expression= "execution(* com.wlh.spring.manager.*.*(..))" /> 
      <aop:advisor advice-ref="txAdvice"  pointcut-ref= "point-cut" /> 
    </aop:config> 
 
     
</beans>

猜你喜欢

转载自cdzm5211314.iteye.com/blog/2254285