spring 事务管理——回滚之service层(事务控制层)代码互调
2016-04-24 13:17
555 查看
spring事务管理相关的文章已经有很多了,本人写此文章主要为自己的实验做一个记录,年纪大了,记性不好
首先先贴几个地址,有兴趣研读的同学可以参考一下:
初级使用:
http://blog.csdn.net/xugangjava/article/details/6770799
初级容易犯的错:事务中catch异常
http://blog.csdn.net/yipanbo/article/details/46048413
官方介绍:
http://docs.spring.io/spring/docs/2.0.x/reference/transaction.html
默认回滚配置实验:
http://blog.csdn.net/lovejavaydj/article/details/7635848
以上几个地址是从不同的角度来讲spring的事务处理的,本文并非重复的去做以上文中已做过的实验,本文的实验对象是两个事务方法之间的调用,检验是否产生回滚。
实验准备:
1、采用spring声明式事务,实现方式,spring的xml文件中进行配置,配置核心代码如下:
3、测试方案:
①根据配置文件txAdvice所示,test和test1都应有事务支持,当分别单独调用两个方法时,遇到异常抛出时,都应回滚。如TestMain中的main方法调用,在TestService中实现两个方法如下两种情况,事务都会回滚,PS:作者做过更多实验,将异常抛出的位置任意变换,都会进行回滚,数据库不会插入任何数据。
(结论:当两个都有事务回滚的方法之间进行相互调用,无论哪个方法中有异常,在任何位置进行异常抛出,两个方法执行的数据都会回滚)
②我们修改一些配置文件中的txAdvice,把<tx:method name="*" rollback-for="Exception"/>改为默认值
③我们继续修改配置文件中的txAdvice,继续使用<tx:method name="*" rollback-for="Exception"/>,另外在这一行上面增加一行
继续测试,test方法虽然还在事务控制之中,但是test1没有事务回滚机制,在test方法中抛出异常并不会使事务回滚,数据库会插入两条数据。另外一种验证,test方法执行成功,test1方法抛出异常仍然不会事务回滚,数据库插入两条数据。
而如果我们将main方法中的调用,改为test()的时候,事务有效,会回滚,不插入数据。
④延续③中的配置文件,我们再换一种测试,main方法中调用test,TestServiceImpl中,test调用test1,因为test有事务回滚支持,因此无论在哪个方法中抛出异常,事务都会回滚。
至此,本文的验证完毕,限于本人水平有限,文章难免有遗漏和不足支持,欢迎各位读者给予指导更正批评!
另外,对于文章中的代码,只是截取代码片段,如有错误请自行修改,并在回复中给予指出,切勿原文照搬测试。
首先先贴几个地址,有兴趣研读的同学可以参考一下:
初级使用:
http://blog.csdn.net/xugangjava/article/details/6770799
初级容易犯的错:事务中catch异常
http://blog.csdn.net/yipanbo/article/details/46048413
官方介绍:
http://docs.spring.io/spring/docs/2.0.x/reference/transaction.html
默认回滚配置实验:
http://blog.csdn.net/lovejavaydj/article/details/7635848
以上几个地址是从不同的角度来讲spring的事务处理的,本文并非重复的去做以上文中已做过的实验,本文的实验对象是两个事务方法之间的调用,检验是否产生回滚。
实验准备:
1、采用spring声明式事务,实现方式,spring的xml文件中进行配置,配置核心代码如下:
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="zigbeeDataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="query*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="is*" read-only="true" /> <tx:method name="*" rollback-for="Exception"/><!--默认回滚机制是RuntimeException--> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="service" expression="execution(* com.my.test..*Service*.*(..))" /><!--声明所有包含Service的类的所有方法使用事务--> <aop:advisor advice-ref="txAdvice" pointcut-ref="service" /> </aop:config>2、测试类:接口,TestService,方法test() , test1();测试类 TestMain,main方法(此处只是简要写明测试核心代码,真正测试方法中需要初始化spring),代码如下:
public interface TestService { void test()throws Exception; void test1()throws Exception; }
//本文验证的代码,一下对两个方法的不同实现方式进行验证说明 @Service(value="testService") public class TestServiceImpl implements TestService { @Autowired TestDao testDao; @Override public void test() throws Exception { // TODO } @Override public void test1() throws Exception { // TODO } }
public class TestMain { public static void main(String[] args) { testService.test1(); } }
3、测试方案:
①根据配置文件txAdvice所示,test和test1都应有事务支持,当分别单独调用两个方法时,遇到异常抛出时,都应回滚。如TestMain中的main方法调用,在TestService中实现两个方法如下两种情况,事务都会回滚,PS:作者做过更多实验,将异常抛出的位置任意变换,都会进行回滚,数据库不会插入任何数据。
(结论:当两个都有事务回滚的方法之间进行相互调用,无论哪个方法中有异常,在任何位置进行异常抛出,两个方法执行的数据都会回滚)
@Service(value="testService") public class TestServiceImpl implements TestService { @Autowired TestDao testDao; @Override public void test() throws Exception { TestEntity te = new TestEntity(); te.setId(1); te.setName("111"); testDao.insert(te); throw new Exception(); // 此处抛出异常 } @Override public void test1() throws Exception { TestEntity te = new TestEntity(); te.setId(2); te.setName("222"); testDao.insert(te); this.test(); // 调用test()方法,在test方法中抛出异常 } }
@Service(value="testService") public class TestServiceImpl implements TestService { @Autowired TestDao testDao; @Override public void test() throws Exception { TestEntity te = new TestEntity(); te.setId(1); te.setName("111"); testDao.insert(te); } @Override public void test1() throws Exception { TestEntity te = new TestEntity(); te.setId(2); te.setName("222"); testDao.insert(te); this.test(); // 调用test()方法,test方法执行成功 throw new Exception(); // 此处抛出异常 } }
②我们修改一些配置文件中的txAdvice,把<tx:method name="*" rollback-for="Exception"/>改为默认值
<tx:method name="*"/>上述测试代码中的事务均不回滚,充分证明了spring默认事务回滚机制为RuntimeException。
③我们继续修改配置文件中的txAdvice,继续使用<tx:method name="*" rollback-for="Exception"/>,另外在这一行上面增加一行
<tx:method name="test1*" no-rollback-for="Exception"/></span>我们让test1方法失去事务回滚的控制
继续测试,test方法虽然还在事务控制之中,但是test1没有事务回滚机制,在test方法中抛出异常并不会使事务回滚,数据库会插入两条数据。另外一种验证,test方法执行成功,test1方法抛出异常仍然不会事务回滚,数据库插入两条数据。
而如果我们将main方法中的调用,改为test()的时候,事务有效,会回滚,不插入数据。
@Service(value="testService") public class TestServiceImpl implements TestService { @Autowired TestDao testDao; @Override public void test() throws Exception { TestEntity te = new TestEntity(); te.setId(1); te.setName("111"); testDao.insert(te); throw new Exception(); // 此处抛出异常 } @Override public void test1() throws Exception { TestEntity te = new TestEntity(); te.setId(2); te.setName("222"); testDao.insert(te); this.test(); // 调用test()方法,test方法中抛出异常 } }
@Service(value="testService") public class TestServiceImpl implements TestService { @Autowired TestDao testDao; @Override public void test() throws Exception { TestEntity te = new TestEntity(); te.setId(1); te.setName("111"); testDao.insert(te); } @Override public void test1() throws Exception { TestEntity te = new TestEntity(); te.setId(2); te.setName("222"); testDao.insert(te); this.test(); // 调用test()方法,test方法执行成功 throw new Exception(); // 此处抛出异常 } }
④延续③中的配置文件,我们再换一种测试,main方法中调用test,TestServiceImpl中,test调用test1,因为test有事务回滚支持,因此无论在哪个方法中抛出异常,事务都会回滚。
public class TestMain { public static void main(String[] args) { testService.test(); } }
@Service(value="testService") public class TestServiceImpl implements TestService { @Autowired TestDao testDao; @Override public void test() throws Exception { TestEntity te = new TestEntity(); te.setId(1); te.setName("111"); testDao.insert(te); this.test1(); // 调用test1()方法,在test1方法中抛出异常 } @Override public void test1() throws Exception { TestEntity te = new TestEntity(); te.setId(2); te.setName("222"); testDao.insert(te); throw new Exception(); // 此处抛出异常 } }
@Service(value="testService") public class TestServiceImpl implements TestService { @Autowired TestDao testDao; @Override public void test() throws Exception { TestEntity te = new TestEntity(); te.setId(1); te.setName("111"); testDao.insert(te); this.test1(); // 调用test1()方法,test1方法执行成功 throw new Exception(); // 此处抛出异常 } @Override public void test1() throws Exception { TestEntity te = new TestEntity(); te.setId(2); te.setName("222"); testDao.insert(te); } }
至此,本文的验证完毕,限于本人水平有限,文章难免有遗漏和不足支持,欢迎各位读者给予指导更正批评!
另外,对于文章中的代码,只是截取代码片段,如有错误请自行修改,并在回复中给予指出,切勿原文照搬测试。
相关文章推荐
- java IO总结(3)
- Java IO流总结(2)
- 20145201《Java程序设计》第8周学习总结
- Springmvc jar包介绍
- Java IO流总结(1)
- 【结构型模式】组合模式(Composite)之23种java设计模式
- Java 常忽略知识点总结
- JAVA基础之五:流程控制
- JDK1.7新特性
- Maven+Springmvc+mybatis+Dubbo+Zookeeper整合分布式架构
- spring测试junit事务管理及spring面向接口注入和实现类单独注入(无实现接口),实现类实现接口而实现类单独注入否则会报错。
- java基础学习总结一(java语言发展历史、jdk的下载安装以及配置环境变量)
- spring中bean配置和bean注入
- eclipse中Bulid Path的作用及用法
- JAVA 32位随机ID
- javaBean为什么要实现Serializable接口?
- Java中的包与访问权限的控制
- [NIO日记](1)Java NIO概述
- 【Leetcode】:94. Binary Tree Inorder Traversal 问题 in JAVA
- spring数据连接池配置参数