1

I want my transaction to be canceled following a personalized exception

@Transactional
public class MyClass{

    public void step1() throws Exception {
        throw new java.lang.Exception();
    }
    public void step2() throws Exception {
        throw new java.lang.Exception();
    }


}

But nothing happens when my exceptions occur.

Vishrant
  • 15,456
  • 11
  • 71
  • 120
tinit
  • 33
  • 5
  • not sure if I followed, but are you looking for `rollbackFor` https://stackoverflow.com/q/21188239/2704032? – Vishrant Jan 27 '22 at 23:25

1 Answers1

1

This is normal, by default spring canceled only when an unchecked exception is thrown.

you have to add at the top of your class @Transactional ( rollbackFor = Exception . class ).

Like that


@Transactional ( rollbackFor =  Exception.class )

public class MyClass{

    public void step1() throws Exception {
        throw new java.lang.Exception();
    }
    public void step2() throws Exception {
        throw new java.lang.Exception();
    }


}

Now like that your exception can cancel your transaction.