0

I have a method with multiple update, delete and save method calls to DB using SPringboot JPA. each of those update, delete and save methods are decorated with @Transactional. How to implement Transactinal on my userdefined_method()? so that all in case of any failure at subsequent following method would revert back previous method changes.

private void userdefined_method(){
    // part1 few lines of business logic code 
    orderService.deleteById(123);  // these methods are decorated with @Transactional
    //part2 few lines of business logic code
    orderservice.save(order);   // this method is decorated with @Transactional
    //part 3 few more lines of business logic

}

In this example deleteById has been called and Entry get deleted from table, and assume there is an error in part2, or error in save(order) method, should roll back previous delete statement.

Nomad
  • 751
  • 4
  • 13
  • 34
  • Maybe read https://www.baeldung.com/spring-transactional-propagation-isolation – Scary Wombat Sep 30 '21 at 01:33
  • 1
    I looked at that article before writing this question, I did not get the answer to how can i achieve the Transactional feature applied to custom method – Nomad Sep 30 '21 at 22:50

1 Answers1

1

You can put @Transactional on the method and change private to public

@Transactional
public void userdefined_method(){
    // part1 few lines of business logic code 
    orderService.deleteById(123);  // these methods are decorated with @Transactional
    //part2 few lines of business logic code
    orderservice.save(order);   // this method is decorated with @Transactional
    //part 3 few more lines of business logic

}

For the explanation about @Transactional, you can have a look at this link

tpnoon
  • 35
  • 3
  • but this class is not extending CrudRepository, to use @Transactional. and I tried with Transactional to try out to see if that works, but failed to rollback deletedById when there was an issue in Part2 business logic – Nomad Sep 30 '21 at 22:47