Alex
Alex

Reputation: 670

grails: maintain transactional context from Service to Controller

Have a transaction in a grails Service class on which a rollback flag is set:

TransactionAspectSupport
          .currentTransactionInfo()
               .transactionStatus
                    .setRollbackOnly()

what happens is that when we return to the Controller an exception:

org.springframework.transaction.UnexpectedRollbackException

which we have to catch in the Controller (but not in any of the Service classes). This code is being refactored from the previous solution where all the logic happened direct in the Controller. Any advise on what happens that trips this exception to be thrown when the method returns, given that:

static transactional = true

has been set on all the classes. Guessing theres some subtle Controller verses Service magic happening - does anyone know about this? For now just catching the exception as a workaround, but this loses the TransactionStatus object that otherwise would have been returned.

Any thoughts much appreciated

Upvotes: 2

Views: 1643

Answers (1)

Oleksandr
Oleksandr

Reputation: 3801

Transaction management in Grails is pretty ugly (for me). So i'm proffering Spring declarative transactions: Chapter 9. Transaction management They works perfectly in grails services.

Returning back to setRollbackOnly(). This method is not simple... While you have set RollBack=true in your inner transaction you have triggered to rollback your outer transaction to sou you are getting exception.

I've similar problem some time ago - here is useful info to find best solution suiting for you:

Upvotes: 3

Related Questions