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

Spring2.0框架的事务处理有两大类:

1 编码式事务 , 这个不说.

2 声明式事务 , 就说这个.

 

声明式事务又有三种实现方法:

(第一种) 最早的方法,用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> 
(第二种) 使用<tx:>来实现声明式事务 ,也要在spring的配置文件XML中配置,比较麻烦,不详细说.

Xml代码

    <tx:advice id="">  
    .....  
    </tx:advice>  
      
    <aop:config>  
    .....  
    </aop:config>  

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

 

第一步:引入<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上

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: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>  


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring框架提供了两种事务管理方式:程式事务管理和声明式事务管理。 1. 程式事务管理: 程式事务管理是通过写代码来管理事务的提交和回滚。在这种方式下,开发人员需要手动事务的开始、提交和回滚的代码。Spring提供了`TransactionTemplate`和`TransactionDefinition`等类来简化程式事务管理的操作。通过使用`TransactionTemplate`,可以在需要进行事务处理的代码块中对事务进行管理。 2. 声明式事务管理: 声明式事务管理是通过配置的方式来管理事务的提交和回滚,而不需要手动事务管理的代码。在这种方式下,开发人员只需要在需要进行事务处理的方法上使用注解或者XML配置文件来声明事务的属性,Spring框架就会根据配置自动实现事务管理。常见的注解方式是使用`@Transactional`注解。 两种事务管理方式各有优劣,程式事务管理灵活性较高,适用于复杂的事务场景,但需要开发人员手动写大量的事务管理代码;声明式事务管理简化了开发工作,通过配置即可实现事务管理,但对于一些复杂的业务场景可能不够灵活。 总的来说,对于大部分应用场景而言,推荐使用声明式事务管理,可以减少重复代码的写,提高开发效率。而在一些特殊的业务场景下,如需要动态控制事务的提交和回滚,或者需要手动处理一些特殊情况,可以考虑使用程式事务管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值