首先,准备service接口,两个

  1. public interface AccountService {
  2.  
  3. public void createAccount(Account account, int throwExpFlag) throws Exception;
  4.  
  5. public void createAccountShell(Account account, int i) throws Exception;
  6.  
  7. }
  1. public interface RoleService {
  2.  
  3. public void createAccountShell(Account account, int i) throws Exception;
  4.  
  5. }

相关impl

  1. @Service
  2. public class AccountServiceImpl implements AccountService {
  3.  
  4. @Resource
  5. private AccountDAO accountDAO;
  6.  
  7. @Override
  8. @Transactional
  9. public void createAccount(Account account, int throwExpFlag) throws Exception {
  10. accountDAO.save(account);
  11. RoleServiceImpl.throwExp(throwExpFlag);
  12. }
  13.  
  14. @Override
  15. public void createAccountShell(Account account, int i) throws Exception {
  16. this.createAccount(account, i);
  17. }
  18.  
  19. }
  1. @Service
  2. public class RoleServiceImpl implements RoleService {
  3.  
  4. @Autowired
  5. AccountService accountService;
  6.  
  7. public static void throwExp(int throwExpFlag) throws Exception {
  8. if (throwExpFlag == 0) {
  9. throw new RuntimeException("<< rollback transaction >>");
  10. } else if (throwExpFlag != 1) {
  11. throw new Exception("<< do not rollback transaction >>");
  12. }
  13. }
  14.  
  15. @Override
  16. public void createAccountShell(Account account, int i) throws Exception {
  17. accountService.createAccount(account, i);
  18. }
  19.  
  20. }

测试类

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration({"classpath:spring/spring-dao.xml", "classpath:spring/spring-service.xml"})
  3. public class ServiceTransactionTest extends TestCase {
  4.  
  5. public static Account account;
  6.  
  7. static {
  8. account = new Account();
  9. account.setId(779);
  10. account.setUsername("779 USER");
  11. account.setPassword("0123456");
  12. }
  13.  
  14. @Autowired
  15. AccountService accountService;
  16.  
  17. @Autowired
  18. RoleService roleService;
  19.  
  20. @Test
  21. public void test_1() throws Exception {
  22. this.accountService.createAccount(account, 0);
  23. }
  24.  
  25. @Test
  26. public void test_2() throws Exception {
  27. this.accountService.createAccountShell(account, 0);
  28. }
  29.  
  30. @Test
  31. public void test_3() throws Exception {
  32. roleService.createAccountShell(account, 0);
  33. }
  34.  
  35. }

(一)对测试类的test_1方法进行单元测试时,由于AccountServiceImpl.createAccount方法显示配置了事务(@Transactional),所以spring正常接管事务。

(二)对测试类的test_2方法进行单元测试时,AccountServiceImpl.createAccountShell方法并没有显示配置事务,但其却调用了AccountServiceImpl.createAccount方法(已配事务)。然并卵,当抛出RuntimeException时,没有rollback,说明spring没有接管事务。(猜测原因:AccountServiceImpl.createAccountShell 被显示调用时,spring是知道的,但由于AccountServiceImpl.createAccountShell没有显示配置事务,spring并没有对此进行事务的管理,在AccountServiceImpl.createAccountShell内部虽然调用了配置了事务的createAccount方法,但spring并不知道或无法确定事务上下文,所以结果是并没有因为抛出的运行时异常而进行rollback)。

(三)测试test_3,虽然RoleServiceImpl.createAccountShell同样没有配置事务,但抛出RuntimeException时,spring接管了事务并rollback。(猜测原因:虽然RoleServiceImpl.createAccountShell没有配置事务,但其内部调用另一个service实例的方法,即AccountService.createAccount时,spring对此是获知的,又因为AccountServiceImpl.createAccount显示配置了事务,所以spring接管了事务)。

(四)如果在AccountServiceImpl.createAccountShell配置了事务,那么在执行test_2时,spring是可以接管事务的。

[spring transaction],service实现类中非事务方法直接调用自身事务方法导致事务无效的原因的更多相关文章

  1. SNF快速开发平台MVC-EasyUI3.9之-WebApi和MVC-controller层接收的json字符串的取值方法和调用后台服务方法

    最近项目组很多人问我,从前台页面传到后台controller控制层或者WebApi 时如何取值和运算操作. 今天就都大家一个在框架内一个取值技巧 前台JS调用代码: 1.下面是选中一行数据后右键点击时 ...

  2. QT源码解析(七)Qt创建窗体的过程,作者“ tingsking18 ”(真正的创建QPushButton是在show()方法中,show()方法又调用了setVisible方法)

    前言:分析Qt的代码也有一段时间了,以前在进行QT源码解析的时候总是使用ue,一个函数名在QTDIR/src目录下反复的查找,然后分析函数之间的调用关系,效率实在是太低了,最近总结出一个更简便的方法, ...

  3. 是否可以从一个static方法内部调用非static方法?

    不可以.静态成员不能调用非静态成员. 非static方法属于对象,必须创建一个对象后,才可以在通过该对象来调用static方法.而static方法调用时不需要创建对象,通过类就可以调用该方法.也就是说 ...

  4. Spring中 PROPAGATION_REQUIRED 解释 事物是在一个方法里调用其他的方法,一起成功或者一起失败,是方法之间的关系,而不是某一个方法内部的问题。而且要以抛异常的方式来表明方法的失败,以此来导致事物起作用,大家全失败。

    事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为, 它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播: 事务传播行为类型 事务传播 ...

  5. 关于srping的AOP事务管理问题,自定义切面是否导致事务控制失效

    applicationContext.xml: <!-- 方法调用时间记录 --> <bean id="methodExecuteTime" class=&quo ...

  6. JNI-Thread中start方法的调用与run方法的回调分析

    前言 在java编程中,线程Thread是我们经常使用的类.那么创建一个Thread的本质究竟是什么,本文就此问题作一个探索. 内容主要分为以下几个部分 1.JNI机制的使用 2.Thread创建线程 ...

  7. python__基础 : 多继承中方法的调用顺序 __mro__方法

    在多继承中,如果一个子类继承了两个平级的父类,而这两个父类有两个相同名字的方法,那么一般先继承谁,调用方法就调用先继承的那个父类的方法.如: class A: def test(self): prin ...

  8. Java-main方法中调用非static方法

    java的calss中,在public static void main(String[] args) { }方法中调用非static的方法:在main方法中创建该calss的对象,用对象调用非sta ...

  9. 10、一个action中处理多个方法的调用第二种方法method的方式

    在实际的项目中,经常采用现在的第二种方式在struct.xml中采用清单文件的方式 我们首先来看action package com.bjpowernode.struts2; import com.o ...

  10. 10、一个action中处理多个方法的调用第一种方法动态调用

    我们新建一个用户的action package com.weiyuan.test; import com.opensymphony.xwork2.ActionSupport; /** * * 这里不用 ...

随机推荐

  1. PHP常用设计模式讲解

    开发中适当的使用设计模式,可以让项目有更易扩展,易维护.低耦合,代码简洁等 单例模式 <?php /** * 单例模式:使类在全局范围内只允许创建一个对象,常用于数据库连接等 */ class ...

  2. python高级-装饰器(19)

    一.什么是闭包 先看一个例子: #定义一个函数 def test(number): #在函数内部在定义一个函数,并且这个函数用到外围函数的变量 #那么将这个函数及用到的一些变量称之为闭包 def te ...

  3. 7.Ajax

    优先级 如果发送的是[普通数据] jQuery XMLHttpRequest iframe 如果发送的是[文件] iframe jQuery(FormData) XMLHttpRequest(Form ...

  4. JS对json操作的扩展

    一.JSON对象 JSON是 JavaScript 的原生对象,用来处理 JSON 格式数据.它有两个静态方法:JSON.stringify()和JSON.parse(). JSON.stringif ...

  5. RabbitMQ学习笔记(四) Routing

    新的场景 在我们学习了RabbitMQ的发布与订阅之后,我们很容易就可以完成一个简单的消息群发器. 使用这个消息群发器,所有的消费者程序实例都会接收到相同的消息信息,从而实现广播的效果. 但是这种广播 ...

  6. Android Studio 获取数字签名

    下面介绍下调试版本和发布版本,获取数字签名的方法,通过以下方法可以获取到SHA1和MD5 1.调试版本 在调试模式下,Android studio会默认生成一个debug.keystore签名文件,因 ...

  7. Chapter 5 Blood Type——15

    I hesitated, torn, but then the first bell sent me hurrying out the door — with a last glance confir ...

  8. iOS逆向开发(8):微信自动添加好友

    这一次,小程演示怎么让一个APP自动地运行,从而代替手工的操作.同样以"微信"以例,实现在一个微信群里面,对所有的成员,自动地一个一个地发出添加好友的请求. 知识点还是之前介绍的东 ...

  9. 补习系列(2)-springboot mime类型处理

    目标 了解http常见的mime类型定义: 如何使用springboot 处理json请求及响应: 如何使用springboot 处理 xml请求及响应: http参数的获取及文件上传下载: 如何获得 ...

  10. springboot情操陶冶-jmx解析

    承接前文springboot情操陶冶-@Configuration注解解析,近期笔者接触的项目中有使用到了jmx的协议框架,遂在前文的基础上讲解下springboot中是如何整合jmx的 知识储备 J ...