Nomad
Nomad

Reputation: 781

How to implement Transactional for multiple updates in single method Springboot JPA

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.

Upvotes: 0

Views: 1973

Answers (1)

tpnoon
tpnoon

Reputation: 35

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

Upvotes: 1

Related Questions