开发者

Transaction rollback in Spring MVC controller

开发者 https://www.devze.com 2023-03-26 15:25 出处:网络
I have a Spring MVC controller annotated with @Transaction, and under certain inputs I need to rollback the transaction.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号