nKognito
nKognito

Reputation: 6363

@Transactional continues at exception handling

I am facing some strange issue..

Let's say:

@RequestMapping(value = "/do", method = RequestMethod.POST)
public void do() {
   try {
      Float x = 200;
      Float y = 100;

      TestObject x = new TestObject(x, y);
      x.setX(50);

      if (x.getX() > x.getY())
         testService.save(x);
      else
         throw new Exception("ERROR");
   } 
   catch (Exception e) {
      errorsService.save(e);
   }
}

Both save methods are marked as @Transactional.

Problem: As soon as code throws an exception - errorsService.save(e); makes a commit - add 50 as a x value of TestObject. I tried to declare @Transactional(rollbackFor = Exception.class) - no success. Always commit, without rollback!

In all other methods there is no any issue with transactions - everything works perfect!

Any suggestion? Thank you in advance!

Upvotes: 0

Views: 468

Answers (1)

coyote
coyote

Reputation: 167

For a transaction to roll back, the Exception needs to be thrown from inside the @Transactional method, which doesn't happen in your case.

Upvotes: 2

Related Questions