Spring2.0框架的事务处理有两大类:
1 编码式事务 , 这个不说.
2 声明式事务 , 就说这个.
声明式事务又有三种实现方法:
1(第一种)最早的方法,用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>
3(第三种)这个方法方便,使用注解来实现声明式事务, 下面详细说说这个方法:
第一步:引入<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 的所有可选属性如下:
| 属性 | 类型 | 默认值 | 说明 |
| 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上
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
- <!--配置SessionFactory-->
- <beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <propertyname="configLocation"value="classpath:hibernate.cfg.xml"/>
- </bean>
- <!--配置事务管理器-->
- <beanid="transactionMgr"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <propertyname="sessionFactory">
- <refbean="sessionFactory"/>
- </property>
- </bean>
- <!--配置事务传播特性-->
- <tx:adviceid="txAdvice"transaction-manager="transactionMgr">
- <tx:attributes>
- <tx:methodname="add*"propagation="REQUIRED"/>
- <tx:methodname="del*"propagation="REQUIRED"/>
- <tx:methodname="update*"propagation="REQUIRED"/>
- <tx:methodname="*"read-only="true"/>
- </tx:attributes>
- </tx:advice>
- <!--那些类使用事务-->
- <aop:config>
- <aop:pointcutid="point-cut"expression="execution(*com.wlh.spring.manager.*.*(..))"/>
- <aop:advisoradvice-ref="txAdvice"pointcut-ref="point-cut"/>
- </aop:config>
- </beans>
本文介绍Spring2.0框架中的声明式事务处理方法,包括使用TransactionProxyFactoryBean、tx命名空间配置及注解方式实现事务管理。重点讲解了注解@Transactional的应用,包括其属性配置及注意事项。
339

被折叠的 条评论
为什么被折叠?



