spring 事务管理——回滚之service层(事务控制层)代码互调

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文件中进行配置,配置核心代码如下:

[html]  view plain  copy
  1. <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  2.         <property name="dataSource" ref="zigbeeDataSource" />  
  3.     </bean>  
  4.       
  5.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  6.         <tx:attributes>  
  7.             <tx:method name="get*" read-only="true"  />  
  8.             <tx:method name="query*" read-only="true"  />  
  9.             <tx:method name="find*" read-only="true" />  
  10.             <tx:method name="is*" read-only="true" />  
  11.             <tx:method name="*" rollback-for="Exception"/><!--默认回滚机制是RuntimeException-->  
  12.         </tx:attributes>  
  13.     </tx:advice>      
  14.      
  15.     <aop:config>  
  16.         <aop:pointcut id="service" expression="execution(* com.my.test..*Service*.*(..))" /><!--声明所有包含Service的类的所有方法使用事务-->  
  17.         <aop:advisor advice-ref="txAdvice" pointcut-ref="service" />  
  18.     </aop:config>  
2、测试类:接口,TestService,方法test() , test1();测试类 TestMain,main方法(此处只是简要写明测试核心代码,真正测试方法中需要初始化spring),代码如下:

[java]  view plain  copy
  1. public interface TestService {  
  2.         void test()throws Exception;  
  3.     void test1()throws Exception;  
  4. }  

[java]  view plain  copy
  1. //本文验证的代码,一下对两个方法的不同实现方式进行验证说明  
  2. @Service(value="testService")  
  3. public class TestServiceImpl  implements TestService {  
  4.     @Autowired  
  5.     TestDao testDao;  
  6.     @Override  
  7.     public void test() throws Exception {  
  8.         // TODO  
  9.     }  
  10.     @Override  
  11.     public void test1() throws Exception {  
  12.         // TODO  
  13.     }  
  14. }  

 
[java]  view plain  copy
  1. public class TestMain {  
  2.         public static void main(String[] args) {  
  3.         testService.test1();  
  4.     }  
  5. }  
 
3、测试方案: 

①根据配置文件txAdvice所示,test和test1都应有事务支持,当分别单独调用两个方法时,遇到异常抛出时,都应回滚。如TestMain中的main方法调用,在TestService中实现两个方法如下两种情况,事务都会回滚,PS:作者做过更多实验,将异常抛出的位置任意变换,都会进行回滚,数据库不会插入任何数据。

(结论:当两个都有事务回滚的方法之间进行相互调用,无论哪个方法中有异常,在任何位置进行异常抛出,两个方法执行的数据都会回滚)

[java]  view plain  copy
  1. @Service(value="testService")  
  2. public class TestServiceImpl implements TestService {  
  3.     @Autowired  
  4.     TestDao testDao;  
  5.     @Override  
  6.     public void test() throws Exception {  
  7.         TestEntity te = new TestEntity();  
  8.         te.setId(1);  
  9.         te.setName("111");  
  10.         testDao.insert(te);  
  11.           
  12.         throw new Exception(); // 此处抛出异常  
  13.     }  
  14.     @Override  
  15.     public void test1() throws Exception {  
  16.         TestEntity te = new TestEntity();  
  17.         te.setId(2);  
  18.         te.setName("222");  
  19.         testDao.insert(te);  
  20.           
  21.         this.test(); // 调用test()方法,在test方法中抛出异常  
  22.           
  23.     }  
  24. }  
[java]  view plain  copy
  1. @Service(value="testService")  
  2. public class TestServiceImpl implements TestService {  
  3.     @Autowired  
  4.     TestDao testDao;  
  5.     @Override  
  6.     public void test() throws Exception {  
  7.         TestEntity te = new TestEntity();  
  8.         te.setId(1);  
  9.         te.setName("111");  
  10.         testDao.insert(te);  
  11.     }  
  12.     @Override  
  13.     public void test1() throws Exception {  
  14.         TestEntity te = new TestEntity();  
  15.         te.setId(2);  
  16.         te.setName("222");  
  17.         testDao.insert(te);  
  18.           
  19.         this.test(); // 调用test()方法,test方法执行成功  
  20.           
  21.         throw new Exception(); // 此处抛出异常  
  22.     }  
  23. }  

②我们修改一些配置文件中的txAdvice, 把<tx:method name="*" rollback-for="Exception"/>改为默认值

[html]  view plain  copy
  1. <tx:method name="*"/>  
上述测试代码中的事务均不回滚,充分证明了 spring默认事务回滚机制为RuntimeException。

③我们继续修改配置文件中的txAdvice,继续使用<tx:method name="*" rollback-for="Exception"/>,另外在这一行上面增加一行

[html]  view plain  copy
  1. <tx:method name="test1*" no-rollback-for="Exception"/></span>  
我们让test1方法失去事务回滚的控制

继续测试,test方法虽然还在事务控制之中,但是test1没有事务回滚机制,在test方法中抛出异常并不会使事务回滚,数据库会插入两条数据。另外一种验证,test方法执行成功,test1方法抛出异常仍然不会事务回滚,数据库插入两条数据。

而如果我们将main方法中的调用,改为test()的时候,事务有效,会回滚,不插入数据。

[java]  view plain  copy
  1. @Service(value="testService")  
  2. public class TestServiceImpl  implements TestService {  
  3.     @Autowired  
  4.     TestDao testDao;  
  5.     @Override  
  6.     public void test() throws Exception {  
  7.         TestEntity te = new TestEntity();  
  8.         te.setId(1);  
  9.         te.setName("111");  
  10.         testDao.insert(te);  
  11.         throw new Exception(); // 此处抛出异常  
  12.     }  
  13.     @Override  
  14.     public void test1() throws Exception {  
  15.         TestEntity te = new TestEntity();  
  16.         te.setId(2);  
  17.         te.setName("222");  
  18.         testDao.insert(te);  
  19.           
  20.         this.test(); // 调用test()方法,test方法中抛出异常  
  21.     }  
  22. }  
[java]  view plain  copy
  1. @Service(value="testService")  
  2. public class TestServiceImpl  implements TestService {  
  3.     @Autowired  
  4.     TestDao testDao;  
  5.     @Override  
  6.     public void test() throws Exception {  
  7.         TestEntity te = new TestEntity();  
  8.         te.setId(1);  
  9.         te.setName("111");  
  10.         testDao.insert(te);  
  11.     }  
  12.     @Override  
  13.     public void test1() throws Exception {  
  14.         TestEntity te = new TestEntity();  
  15.         te.setId(2);  
  16.         te.setName("222");  
  17.         testDao.insert(te);  
  18.           
  19.         this.test(); // 调用test()方法,test方法执行成功  
  20.           
  21.         throw new Exception(); // 此处抛出异常  
  22.     }  
  23. }  

④延续③中的配置文件,我们再换一种测试,main方法中调用test,TestServiceImpl中,test调用test1, 因为test有事务回滚支持,因此无论在哪个方法中抛出异常,事务都会回滚。

[java]  view plain  copy
  1. public class TestMain {  
  2.         public static void main(String[] args) {  
  3.         testService.test();  
  4.     }  
  5. }  
[java]  view plain  copy
  1. @Service(value="testService")  
  2. public class TestServiceImpl  implements TestService {  
  3.     @Autowired  
  4.     TestDao testDao;  
  5.     @Override  
  6.     public void test() throws Exception {  
  7.         TestEntity te = new TestEntity();  
  8.         te.setId(1);  
  9.         te.setName("111");  
  10.         testDao.insert(te);  
  11.           
  12.         this.test1(); // 调用test1()方法,在test1方法中抛出异常  
  13.     }  
  14.     @Override  
  15.     public void test1() throws Exception {  
  16.         TestEntity te = new TestEntity();  
  17.         te.setId(2);  
  18.         te.setName("222");  
  19.         testDao.insert(te);  
  20.           
  21.         throw new Exception(); // 此处抛出异常  
  22.     }  
  23. }  
[java]  view plain  copy
  1. @Service(value="testService")  
  2. public class TestServiceImpl  implements TestService {  
  3.     @Autowired  
  4.     TestDao testDao;  
  5.     @Override  
  6.     public void test() throws Exception {  
  7.         TestEntity te = new TestEntity();  
  8.         te.setId(1);  
  9.         te.setName("111");  
  10.         testDao.insert(te);  
  11.           
  12.         this.test1(); // 调用test1()方法,test1方法执行成功  
  13.           
  14.         throw new Exception(); // 此处抛出异常  
  15.     }  
  16.     @Override  
  17.     public void test1() throws Exception {  
  18.         TestEntity te = new TestEntity();  
  19.         te.setId(2);  
  20.         te.setName("222");  
  21.         testDao.insert(te);  
  22.     }  
  23. }  

至此,本文的验证完毕,限于本人水平有限,文章难免有遗漏和不足支持,欢迎各位读者给予指导更正批评!

另外,对于文章中的代码,只是截取代码片段,如有错误请自行修改,并在回复中给予指出,切勿原文照搬测试。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值