monda
monda

Reputation: 3915

Make controller Transactional controller

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?

Upvotes: 4

Views: 4183

Answers (3)

th3morg
th3morg

Reputation: 4809

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"
 }

 ...

}

Upvotes: 10

rxn1d
rxn1d

Reputation: 1266

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

Upvotes: 7

Eylen
Eylen

Reputation: 2677

Check out services, they're transactional by default. Just do the updates there and if there's an error throw an exception and catch it in the controller

Upvotes: 6

Related Questions