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

Transaction propagation in spring @required

@GetMapping("trans")
    @Transactional()
    public String primaryTrans() {
        User u1 = new User(0,"test","test@email.com");
        us.save(u1);
        User u2 = new User(0,"test1","test1@email.com");
        us.save(u2);
        secondaryTrans();
        return "index";
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    private void secondaryTrans() {
        // TODO Auto-generated method stub
        User u2 = new User(0,"test2","test3@email.com".repeat(300));
        us.save(u2);
    }

Here i am manually raising DATA TOO LONG exception from secondary transaction, But it causes primary transaction also rolled back. How can we make sure that primary transaction to be committed irrespective of secondary transaction

>Solution :

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

In this case, since the second method is called from the same class, the second transaction is most likely not created. Springs transactional support uses AOP proxies to create transactions. The docs contain a description on why this will not work.

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