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

 

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

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

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

 

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

(第一种) 最早的方法,用TransactionProxyFactoryBean,他是一个有AOP代理功能的FactoryBean.他返回的对象有事务.

还要在spring的配置文件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中配置,比较麻烦,不详细说.

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

<aop:config>
.....
</aop:config>

 

 

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

 

第一步:引入<tx:>命名空间 ,在spring的配置文件中修改, 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: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自动配置为声明式事务支持

<!--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. 要是只的接口上写, 接口的实现类就会继承下来.

接口的实现类的具体方法,还可以覆盖类声明处的设置.

@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  的所有可选属性如下:

 

属性类型默认值说明
propagationPropagation枚举REQUIRED 事务传播属性 (下有说明)
isolationisolation枚举DEFAULT 事务隔离级别 (另有说明)
readOnlybooleanfalse是否只读
timeoutint-1超时(秒)
rollbackForClass[]{}需要回滚的异常类
rollbackForClassNameString[]{}需要回滚的异常类名
noRollbackForClass[]{}不需要回滚的异常类
noRollbackForClassNameString[]{}不需要回滚的异常类名

 

 

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

 

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

 

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

* spring的事务需要设置到业务方法上(事务边界定义到Facade类上),不要添加到Dao上

Java代码  
  1. <?xml version= "1.0"  encoding= "UTF-8" ?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"   
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.          xmlns:aop="http://www.springframework.org/schema/aop"   
  6.          xmlns:tx="http://www.springframework.org/schema/tx"   
  7.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
  9.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">   
  10.       
  11.     <!-- 配置SessionFactory -->  
  12.     <bean id="sessionFactory"   class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >  
  13.       <property  name="configLocation"  value= "classpath:hibernate.cfg.xml" />  
  14.     </bean>  
  15.     <!-- 配置事务管理器 -->  
  16.     <bean id="transactionMgr"   class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >  
  17.      <property name="sessionFactory" >  
  18.         <ref bean="sessionFactory" />             
  19.      </property>  
  20.     </bean>  
  21.     <!-- 配置事务传播特性 -->  
  22.     <tx:advice id="txAdvice"  transaction-manager= "transactionMgr" >  
  23.          <tx:attributes>  
  24.              <tx:method name="add*"  propagation= "REQUIRED" />   
  25.               <tx:method name="del*"  propagation= "REQUIRED" />  
  26.               <tx:method name="update*"  propagation= "REQUIRED" />  
  27.               <tx:method name="*"  read-only= "true" />  
  28.          </tx:attributes>  
  29.     </tx:advice>  
  30.     <!-- 那些类使用事务 -->  
  31.     <aop:config>  
  32.       <aop:pointcut id="point-cut"  expression= "execution(* com.wlh.spring.manager.*.*(..))" />  
  33.       <aop:advisor advice-ref="txAdvice"  pointcut-ref= "point-cut" />  
  34.     </aop:config>  
  35.   
  36.       
  37. </beans>  

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值