Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 处理异常后的Spring嵌套事务回滚_Java_Spring_Transactions_Nested Transactions - Fatal编程技术网

Java 处理异常后的Spring嵌套事务回滚

Java 处理异常后的Spring嵌套事务回滚,java,spring,transactions,nested-transactions,Java,Spring,Transactions,Nested Transactions,我有一个@Service类,它有一个@Transactional方法,在另一个服务上调用另一个@Transactional方法。诸如此类: @Service public class AService { @Autowired BService b; @Autowired ARepository aRepo; @Transactional public void methodOne(){ try{ b.methodTwo(); }catch(

我有一个
@Service
类,它有一个
@Transactional
方法,在另一个服务上调用另一个
@Transactional
方法。诸如此类:

@Service
public class AService {
  @Autowired
  BService b;
  @Autowired
  ARepository aRepo;

  @Transactional
  public void methodOne(){
    try{
      b.methodTwo();
    }catch(RuntimeException e){}
    aRepo.save(new A());
  }

} 

@Service
public class BService{

    @Transactional
    public void methodTwo(){
      if(true)
        throw new RuntimeException();
    }

}
我希望一个实体将被插入,但如果任何嵌套事务抛出异常,则即使在
AService.methodOne()处理了此异常,插入也将拒绝


我可以用
@Transactional(propagation=propagation.REQUIRES_NEW)注释
methodTwo()
。但是它会影响性能。

如果在
methodTwo
中发生异常后不想从
methodOne
回滚事务,可以使用
@Transactional(norollboor={RuntimeException.class})添加注释
methodOne
。但请注意,这有点滑倒,如果您真的想这样做,请三思。

没有嵌套事务,只有一个事务。如果需要单独的事务,则需要使用
REQUIRES\u NEW
注释另一个事务。是的,这是一个性能影响,因为它启动了一个新的tx。这种情况下有另一个更优雅的解决方案吗?没有。您要么提交所有内容,要么回滚所有内容。。。您不能部分提交将超过事务的ACID属性的内容。如果我将try catch block移到methodTwo()中,请成功插入实体。但是在我真正的methodOne中,我有很多方法调用,其中一些是业务逻辑所必需的。methodOne()有很多其他服务方法调用,并且这些方法不仅在服务中调用。我只想在服务中使用这种行为。