Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Transactional anotation with expection

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading