1

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?

Jack
  • 417
  • 2
  • 8
  • 18
  • may be by writing throws Exception you'll autocast all exceptions to Exception class try to change to throws IOException. and actualy does the exception happends? – Ruslan F. Sep 05 '13 at 10:13
  • Even when I inspect the line response.getOutputStream() I can view the object so I know there is no exception being thrown, really weird. – Jack Sep 05 '13 at 10:17
  • It is not happend because you should unplug your cabel right in the moment of reciving data form servlet and in that way exception will be on a servlet level wich is not accessable from controller – Ruslan F. Sep 05 '13 at 10:20
  • just for testing you could in some condition manualy throw IOException – Ruslan F. Sep 05 '13 at 10:21
  • I'll try and test by throwing an IOException, but I'm out of ideas, might try writing out to the outputstream and see what happens. – Jack Sep 05 '13 at 10:25
  • Rollback works when manually throwing an IOException – Jack Sep 05 '13 at 10:52

1 Answers1

4

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.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • 1
    I already have exception handling using @ExceptionHandler annotated methods, I also understand that really you should only have the service layer transacted. However if the client disconnects before the controller method returns, then I would want any transaction to rollback. – Jack Sep 05 '13 at 13:10
  • you won;t get a file io excetion if you pull out cable on the client, you'll get an unable to connect error on yr client. Look at the source of getOutputstream() to see when the error wwoul dbe thrown. – NimChimpsky Sep 05 '13 at 13:12
  • So what would be the best to handle the scenario I have said? – Jack Sep 05 '13 at 13:14
  • On the server you won't, as I said it knows nothing of the client. – NimChimpsky Sep 05 '13 at 13:14
  • In other words there is no way to rollback in that scenario? – Jack Sep 05 '13 at 13:16