Home > Back-end >  Transactional anotation with expection
Transactional anotation with expection

Time:01-28

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.

CodePudding user response:

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.

  •  Tags:  
  • Related