views:

80

answers:

4

The question from the title in code:

@Transactional (readonly = true)
public interface FooService {
   void doSmth ();
}


public class FooServiceImpl implements FooService {
   ...
}

vs

public interface FooService {
   void doSmth ();
}

@Transactional (readonly = true)
public class FooServiceImpl implements FooService {
   ...
}
A: 

Putting it on the interface is fine as long all foreseeable implementers of your IFC care about TX data (transactions aren't problems that just databases deal with). If the method doesn't care about TX (but you need to put it there for Hibernate or whatever), put it on the impl.

Also, it might be a bit better to place @Transactional on the methods in the interface:

public interface FooService {
    @Transactional(readOnly = true)
    void doSmth();
}
engfer
+3  A: 

You can put them on the interface but be warn that transactions may not end up happening in some cases. See the second tip in Secion 10.5.6 of the Spring docs:

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.

I would recommend putting them on the implementation for this reason.

Also, to me, transactions seem like an implementation detail so they should be in the implementation class. Imagine having wrapper implementations for logging or test implementations (mocks) that don't need to be transactional.

AngerClown
+5  A: 

From http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html

The Spring team's recommendation is that you only annotate 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 will only work as you would expect it to if you are using interface-based proxies. The fact that annotations are not inherited means that if you are using class-based proxies then the transaction settings will not be recognised by the class-based proxying infrastructure and the object will not be wrapped in a transactional proxy (which would be decidedly bad). So please do take the Spring team's advice and only annotate concrete classes (and the methods of concrete classes) with the @Transactional annotation.

Note: Since this mechanism is based on proxies, only 'external' method calls coming in through the proxy will be intercepted. This means that 'self-invocation', i.e. a method within the target object calling some other method of the target object, won't lead to an actual transaction at runtime even if the invoked method is marked with @Transactional!

(Emphasis added to the first sentence, other emphasis from the original.)

Romain Hippeau
Big +1 BTW, I took the liberty of making the quote a quote, so people aren't confused, and added back the italics and such from the original.
T.J. Crowder
@T.J. Crowder - Amazing how other answers change. thx, NP
Romain Hippeau
@Romain Hippeau: I have read this statement before in Spring docu, but still I cannot understand why _transaction settings will not be recognised by the class-based proxying infrastructure_? I personally think, this is just Spring implementation limitation, not the problem of underlying proxy infrastructure.
dma_k
@Romain Hippeau: Looking at Spring sources one can find out that `JdkDynamicAopProxy` goes through all bean advisors on each method invocation (see also `DefaultAdvisorChainFactory#getInterceptorsAndDynamicInterceptionAdvice()`), which in case of declarative transaction setup, includes `BeanFactoryTransactionAttributeSourceAdvisor`. In its turn `TransactionAttributeSourcePointcut#matches()` is invoked, which should collect the transaction-relative information. It is passed the target class and it can always traverse all interfaces this class implements. Now: why this cannot not work reliably?
dma_k
+3  A: 

Spring's recommendation is that you annotate the concrete implementations instead of an interface. It's not incorrect to use the annotation on an interface, it's just possible to misuse that feature and inadvertently bypass your @Transaction declaration.

If you've marked something transactional in an interface and then refer to one of its implementing classes elsewhere in spring, it's not quite obvious that the object that spring creates will not respect the @Transactional annotation.

In practice it looks something like this:

public class MyClass implements MyInterface { 

    private int x;

    public void doSomethingNonTx() {}

    @Transactional
    public void toSomethingTx() {}

}
zmf