Where to put @Transactional? In interface specification or implementation?

JavaSpringInterfaceTransactions

Java Problem Overview


What is considered the best practice in placing the @Transactional annotation? Should I annotate the interface method or the implementation?

Java Solutions


Solution 1 - Java

It really all depends on your application architecture, in my opinion. It depends on how you are proxying your classes. If you have your app set to proxy-target-class='true' (in your application context, then your @Transactional information wont be picked up if you annotate the Interface.

Check out The Spring Docs -- "Tips" for more information.

> Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the @Transactional annotation, as opposed to annotating interfaces. You certainly can place the @Transactional annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies. The fact that Java annotations are not inherited from interfaces means that if you are using class-based proxies (proxy-target-class="true") or the weaving-based aspect (mode="aspectj"), then the transaction settings are not recognized by the proxying and weaving infrastructure, and the object will not be wrapped in a transactional proxy, which would be decidedly bad.

Solution 2 - Java

Good question. I've always put it in the implementation. Perhaps because it is an implementation detail, rather than an abstraction.

You may want different implementations to have different transactional behaviours.

El Guapo noted that, in addition to that, there are more issues that can arise from putting on on the interface, related to the proxying strategy.

Solution 3 - Java

While transactions management is implementation detail in many cases quite often it's an interface detail as well. For example, when defining interface of services of your application you might consider putting @Transactional into interface definition to specifically clarify what propagation strategy you're using.

Solution 4 - Java

I don't use interfaces on my system because so far I don't really see if it will be possible to implement anything over it. So I put annotations on the implementation and I believe Spring would make everything correct to me.

I don't think that all classes must have interfaces. I see around lots of architectures with lots of patterns and they all love interfaces. But a question: if you put the Spring annotation into the interface and you, for some reason, you want another approach to the transaction of an implementation class done over this interface, you couldn't do that. Or am I wrong?

Cheers.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn ManakView Question on Stackoverflow
Solution 1 - JavaEl GuapoView Answer on Stackoverflow
Solution 2 - JavaBozhoView Answer on Stackoverflow
Solution 3 - JavaoiavorskyiView Answer on Stackoverflow
Solution 4 - JavaLuiz Feijão VeronesiView Answer on Stackoverflow