Spring nested transaction rollback after handling exception
Asked Answered
P

1

8

I have a @Service class which has a @Transactional method that calls another @Transactional method on the another service. Something like that:

@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();
    }

}

I expect that A entity will be insert, but if any nested transaction throw exception insert will reject even this exception was handled at AService.methodOne().

I can annotate methodTwo() with @Transactional(propagation = Propagation.REQUIRES_NEW). But it will beat performance.

Pulverulent answered 26/1, 2018 at 13:22 Comment(5)
There is no nested transaction there is a single one. If you want separate transactions you ned to annotate the other one with REQUIRES_NEW. Yes that is a performance impact as it starts a new tx.Rawalpindi
Have this case another more elegant solution ?Pulverulent
No. You either commit everything or rollback everything... You cannot partially commit stuff that would beat the ACID properties of a transaction.Rawalpindi
If i move try-catch block into methodTwo() A entity insert successfully. But in my real methodOne i have a lot of method calls, and some of them is required for bussines logic.Pulverulent
did you find the solution? I'm also having the same problem.Lap
J
2

If you don't want to rollback your transaction from methodOne after some exception happens in the methodTwo, you can add annotate methodOne with @Transactional(noRollbackFor = {RuntimeException.class}). But please be aware that this is a bit of slippery slope and think twice if you really want to do it.

Jacquenette answered 26/1, 2018 at 13:44 Comment(1)
methodOne() has a lot of another service method calls, and this methods calls not only in AService. I want this behaviour only in AService.Pulverulent

© 2022 - 2024 — McMap. All rights reserved.