Spring MVC Controller Transactional

浪尽此生 提交于 2019-12-19 11:28:43

问题


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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/18633514/spring-mvc-controller-transactional

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!