Spring MVC控制器事务性 [英] Spring MVC Controller Transactional
问题描述
我正在尝试获取一个用@Transactional注释的Spring MVC Controller方法,以在该方法返回之前如果拉回客户端的网络电缆将其回滚,我似乎无法使其正常运行,在这里是我要实现的目标的一个例子.
I'm trying to get a Spring MVC Controller method which has been annotated with @Transactional to rollback if the network cable in pulled on the client before the method returns, I just can't seem to get it to work, here is an example of what I'm trying to achieve.
@Transactional(rollbackFor = IOException.class)
@RequestMapping(value = "/test")
public @ResponseBody
Integer testMethod(HttpServletResponse response) throws Exception {
LOG.debug("Put breakpoint here, and pull network cable on client...");
//IMHO this should throw an IOException, but it isn't doing?
response.getOutputStream();
return 10;
}
因此,如果我在日志记录语句上放置一个断点,然后拔下客户端上的网络电缆然后播放,则可能会从response.getOutputStream()获得IOException,但事实并非如此,任何帮助都会非常感谢?
So if I put a breakpoint on the logging statement, then unplug the network cable on the client then play, I would of expected to get an IOException from response.getOutputStream(), but this is not the case, any help will be much appreciated?
推荐答案
不要使控制器具有事务性.事务用于服务层.常见的做法是拥有一个基本控制器,其他控制器进行扩展并包含错误处理-在这种情况下,可以返回不同的异常消息.
Don't make the controller transactional. Transactions are for the service layer. A common practice is to have a base controller, that other controllers extend and contains error handling - in which case different exception messages can be returned.
您要拔出客户端上的网络电缆,并期待服务器上出现异常吗?这没有道理.
You are unplugging a network cable on the client, and expecting an exception on the server ? This doesn't make sense.
这篇关于Spring MVC控制器事务性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!