Search code examples
springinterfacetransactionsproxyfacade

Facade, service interfaces and transaction proxies with Spring


Situation

There is a high chance that users will not interact directly with the various Service objects (POJOs or Session Beans), since using the Facade pattern the different single services are collected into one bunch.

The @Transactional annotation is applied on the level of methods of single services, as opposed to on the methods of the Facade.

This meets a practical problem - if the services have no interface, Spring can't use nice transaction proxies for them, leading to various complications.

Question

What is a desired practice?

  • Creating single service interfaces for the sake of nice proxies,
  • or moving the @Transactional annotations to the Facade methods (whereby internally using the services also have to flow through the Facade to ensure transactions).
  • or else?

What is your field experience? I'm also open for considerations from a wider perspective.


Solution

  • If service has no interfaces, Spring still can apply transactional aspect to it by use of CGLIB proxies, in most cases it works fine. Real problems arise when service has some interfaces, but transactional method is not a part of these interfaces.

    However, applying @Transactional to Facade methods can be a cleaner solution, since Facade interface defines clear transaction boundaries. If you worry about using services wihtout Facade in this case, you can apply @Transactional to services as well as to Facade methods. In these case transactions created when you call Facade methods are propagated to the service methods.