Spring MVC 控制器中的事务回滚
我有一个用 @Transaction
注释的 Spring MVC 控制器,在某些输入下我需要回滚事务。
据我了解,触发回滚的正确方法是从控制器抛出异常。
但是,如果我从控制器抛出异常,我将没有机会从控制器返回 ModelAndView
对象。
如何在控制器中触发回滚,同时仍然提供要渲染的 ModelAndView
?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
例外是应遵循的适当途径。 Spring 可以 将异常映射到视图也是如此。这可能就是您想要做的。
Exceptions are the appropriate route to follow. Spring can map exceptions to views as well. This is probably what you want to do.
首先,我不认为在控制器方法上使用 @Transaction 是一件好事。最好将事务逻辑保留在“服务层”中并在那里使用
@Transaction
注释。但如果您仍然想在控制器上使用@Transaction,请查看org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
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 atorg.springframework.web.servlet.handler.SimpleMappingExceptionResolver