Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make controller Transactional controller

Tags:

grails

In my controller I have a action which updates 2 domain class.

I want it to make in such a way that if the second updates fails first update should roll back, basically if there is an error all the previous actions should roll back.

What's the basic idea here?

like image 900
monda Avatar asked Nov 29 '22 15:11

monda


2 Answers

If you don't want to move your logic to a Service (possibly because you may be calling multiple services to execute both of those updates), annotate your controller action with @Transactional.

public MyController {
 @Transactional
 def save(){
   myService.save(params)
   myOtherService.save(params)
   render "success"
 }

 ...

}
like image 168
th3morg Avatar answered Dec 05 '22 15:12

th3morg


Just move all your business logic to services that already are transactional. Use one service for first update action and second for another action.

like image 21
rxn1d Avatar answered Dec 05 '22 15:12

rxn1d