Spring使用tx标签配置拦截器动态为指定方法添加事务

来源:互联网 发布:量子网络代替互联网 编辑:程序博客网 时间:2024/06/05 01:08

1、事务传播特性

1REQUIRED

默认的,加入当前正要执行的事务不在另外一个事务里,那么就起一个新的事务

比如说,ServiceB.methodB的事务级别定义为PROPAGATION_REQUIRED,那么由于执行ServiceA.methodA的时候ServiceA.methodA已经起了事务,这时调用ServiceB.methodB,ServiceB.methodB看到自己已经运行在ServiceA.methodA的事务内部,就不再起新的事务。而假ServiceA.methodA运行的时候发现自己没有在事务中,他就会为自己分配一个事务。

这样,在ServiceA.methodA或者在ServiceB.methodB内的任何地方出现异常,事务都会被回滚。即使ServiceB.methodB的事务已经被提交,但是ServiceA.methodA在接下来fail要回滚,ServiceB.methodB也要回滚。

 

2SUPPORTS

如果当前在事务中,即以事务的形式运行,如果当前不再一个事务中,那么就以非事务的形式运行。

 

3MANDATORY

必须在一个事务中运行,如果没有就抛出异常。也就是说,他只能被一个父事务调用。否则,他就要抛出异常。

 

4REQUIRES_NEW

这个就比较绕口了。比如我们设计ServiceA.methodA的事务级别为PROPAGATION_REQUIRED,ServiceB.methodB的事务级别为PROPAGATION_REQUIRES_NEW,那么当执行到ServiceB.methodB的时候,ServiceA.methodA所在的事务就会挂起,ServiceB.methodB会起一个新的事务,等待ServiceB.methodB的事务完成以后,他才继续执行。他与PROPAGATION_REQUIRED 的事务区别在于事务的回滚程度了。因为ServiceB.methodB是新起一个事务,那么就是存在两个不同的事务。如果ServiceB.methodB已经提交,那么ServiceA.methodA失败回滚,ServiceB.methodB是不会回滚的。如果ServiceB.methodB失败回滚,如果他抛出的异常被ServiceA.methodA捕获,ServiceA.methodA事务仍然可能提交。

 

5NOT_SUPPORTED

当前不支持事务以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。比如ServiceA.methodA的事务级别是PROPAGATION_REQUIRED,而ServiceB.methodB的事务级别是PROPAGATION_NOT_SUPPORTED,那么当执行到ServiceB.methodB时,ServiceA.methodA的事务挂起,而他以非事务的状态运行完,再继续ServiceA.methodA的事务。

 

6NEVER

不能在事务中运行如果当前存在事务,则抛出异常。假设ServiceA.methodA的事务级别是PROPAGATION_REQUIRED,而ServiceB.methodB的事务级别是PROPAGATION_NEVER ,那么ServiceA.methodA中调用ServiceB.methodB就要抛出异常了。

 

7NESTED

理解Nested的关键是savepoint。他与PROPAGATION_REQUIRES_NEW的区别是,PROPAGATION_REQUIRES_NEW另起一个事务,将会与他的父事务相互独立,而Nested的事务和他的父事务是相依的,他的提交是要等和他的父事务一块提交的。也就是说,如果父事务最后回滚,他也要回滚的。

而Nested事务的好处是他有一个savepoint。也就是说ServiceB.methodB失败回滚,那么ServiceA.methodA也会回滚到savepoint点上,ServiceA.methodA可以选择另外一个分支,比如ServiceC.methodC,继续执行,来尝试完成自己的事务。 但是这个事务并没有在EJB标准中定义。


2、代码配置

<!-- 事务配置 -->    <bean id="transactionManager"       class="org.springframework.jdbc.datasource.DataSourceTransactionManager">       <property name="dataSource" ref="dataSource"></property>    </bean>    <tx:advice id="txAdvice" transaction-manager="transactionManager">       <tx:attributes>         <!-- 创建数据的方法名 -->                <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>                <tx:method name="new*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>                <tx:method name="set*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>                <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>                <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>                <tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>                <!-- 更新数据的方法名 -->                <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>                <tx:method name="write*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>                <tx:method name="exec*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>                <tx:method name="backed*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>                <!-- 删除数据的方法名 -->                <tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>                <!-- 检索数据的方法名 -->                <tx:method name="get*" propagation="SUPPORTS" read-only="true" />                <tx:method name="obtain*" propagation="SUPPORTS" read-only="true" />                               <!-- 指定当前方法以非事务方式执行操作,如果当前存在事务,就把当前事务挂起,等我以非事务的状态运行完,再继续原来的事务。 -->                <tx:method name="*" propagation="NOT_SUPPORTED"/>       </tx:attributes>    </tx:advice>

3、回滚方法

在方法中有多种方法进行回滚,常用如下:
1)throw new RuntimeException()
2)TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
3)等等


原创粉丝点击