简体   繁体   English

Spring MVC Controller Transactional

[英]Spring MVC Controller Transactional

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. 我试图获得一个Spring @ MVC Controller方法,该方法已经注释了@Transactional如果在方法返回之前在客户端上拉网络电缆,我只是无法让它工作,这里是一个例子我正在努力实现的目标。

@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;
}

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? 因此,如果我在日志记录语句中放置断点,然后拔掉客户端上的网络电缆然后播放,我希望从response.getOutputStream()获得IOException,但事实并非如此,任何帮助将不胜感激?

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. 这没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM