0

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!

1 Answer 1

2

If you have the code inside a controller action, you have to keep an eye on 2 things:

  • transactionality (with try/catch/finally etc)
  • what should the action return/render in case of TX-success/-failure.

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.