Will
Will

Reputation: 5537

Transaction rollback in Spring MVC controller

I have a Spring MVC controller annotated with @Transaction, and under certain inputs I need to rollback the transaction.

As I understand, the proper way to trigger a rollback is to throw an exception from the controller.

But if I throw an exception from the controller, I won't get a chance to return a ModelAndView object from the controller.

How can I trigger a rollback in the controller, while still providing a ModelAndView to be rendered?

Upvotes: 3

Views: 4336

Answers (2)

robi
robi

Reputation: 167

First, I don't think that using @Transaction on controller methods is a good thing. It's better to keep transactional logic in a 'service tier' and use the @Transaction annotation there.

But if you still want to use @Transaction on the controller, take a look at org.springframework.web.servlet.handler.SimpleMappingExceptionResolver

Upvotes: 2

Ryan Stewart
Ryan Stewart

Reputation: 128769

Exceptions are the appropriate route to follow. Spring can map exceptions to views as well. This is probably what you want to do.

Upvotes: 2

Related Questions