Jack
Jack

Reputation: 427

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.

@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?

Upvotes: 1

Views: 4868

Answers (1)

NimChimpsky
NimChimpsky

Reputation: 47290

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.

Upvotes: 4

Related Questions