Transaction rollback in Spring MVC controller
I have a Spring MVC controller annotated with @Transaction
, and under certain inputs I need to rollback the transaction.
As I understand, the proper way to trigger a rollback is to throw an exception from the controller.
But if I throw an exception from the controller, I won't get a chance to return a ModelAndView
object from the controller.
How can I trigger a rollback in the controller, while still 开发者_运维技巧providing a ModelAndView
to be rendered?
Exceptions are the appropriate route to follow. Spring can map exceptions to views as well. This is probably what you want to do.
First, I don't think that using @Transaction
on controller methods is a good thing. It's better to keep transactional logic in a 'service tier' and use the @Transaction
annotation there.
But if you still want to use @Transaction
on the controller, take a look at org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
精彩评论