开发者

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 th开发者_开发知识库rough the Facade to ensure transactions).
  • or else?

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


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.


The only criteria for placing transaction boundaries should be business logic criteria not technical issues. If services methods should be atomic operations then manage transactions there. If your facade can combine 2 or more service calls and they together should be "execute all or nothing", this means the transaction should be put in facade. As axtavt mentioned, cglib proxies can be used to proxy classes with no interfaces.

I also recommend this series by Mark Richards: http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=transaction+strategies:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜