I have the following method within my controller:
@Transactional
def update(Filter filterInstance) {
bindData(filterInstance, params, [include: ['name, 'code', 'value']])
filterService.update(filterInstance)
mappingService.update(filterInstance)
respond filterInstance
}
I need to use @Transactional here because I call multiple services. How to handle case when transaction fails for some reason? Should I use try catch or may be there is some other way?
Thanks!
If you have the code inside a controller action, you have to keep an eye on 2 things:
It's complicated enough, so you should put the service calls into another service method, where you have to deal with the transactions only.
A controller action is a place, where you may fire really simple 1-step transactions. If you need to implement a more complex TX-logic, do it on the service layer
see ref-doc on how to control multi-step transactions.
you can make your update()
methods return false or throw an exception, and if such thing occurs, call status.setRollbackOnly()
. Don't forget also to mark the update()
methods with @Transactional(propagation = Propagation.SUPPORTS)