Transaction Design Pattern for MVC – Responsibility Location

mvcspringtransactionweb-applicationsweb-development

I'm designing a pretty standard Spring MVC application, and I'm trying to figure out where the responsibility for transaction management should lie.

I've been reading Java Transaction Design Strategies, and the one that I think best suits is the Server Delegate one, where the controller is what manages the transactions, and the services and DAOs are oblivious to the transactions.

Somehow, this doesn't feel right. My gut feeling is that the services are the ones that should control the transactions, but I have cases where a controller may call several services to perform an action.

How have you handled Transactions in MVC applications before, and where do you consider the best place for handling the transactions is?

Best Answer

I find it useful to implement a Service Facade that decouples the implementation of services from the service contract. The Controller from the presentation tier (your MVC stuff) should be able to make a single call to a Service Facade, which then orchestrates the calls to one or more "service" objects/methods, and returns whatever the service contract says it needs to return.

In this architecture, the Service Facade would also be the place to hook in your transaction management.