Using Grails Transaction when saving two different domain object in one shot -


i have domain classes need updated @ same time, want use transaction in order allow changes both or neither. example :

i have 2 different domain classes (user, , follow)

user currentuser =.. user targetuser = .. follow followuser = ..  targetuser.follower = targetuser.follower + 1 currentuser.follow = currentuser.follow + 1 targetuser.save(flush:true) currentuser.save(flush:true) followuser.save(flush:true)  

i want of happen or if 1 fails none of happen , gets rolled back. how can in grails ? saw domainobject.withtransaction, have 2 different domain, should nested ?

the proper solution move transactional code service. documentation outlines how create , use services controllers. that's proper solution.

however, that's not way. have seen there ability run code within transaction scope using withtransaction. example (directly documentation):

account.withtransaction { status ->     def source = account.get(params.from)     def dest = account.get(params.to)      int amount = params.amount.tointeger()     if (source.active) {         source.balance -= amount          if (dest.active) {             dest.amount += amount         }         else {             status.setrollbackonly()         }     } } 

the code within withtransaction closure can span number of domain classes. can mix , match see fit.

again, stress. the proper way use services.


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -