Declarative transactions (@Transactional) doesn't work with @Repository in Spring Declarative transactions (@Transactional) doesn't work with @Repository in Spring spring spring

Declarative transactions (@Transactional) doesn't work with @Repository in Spring


Probably because the component-scan in your spring-servlet.xml is also including your DAO classes in its scanning and therefore creating instances for them in its application context (not the "database" one)... so that when your web accesses these DAOs from web controllers, it is accessing non-transactional versions of them (unless you add that tx:annotation-driven tag).

Therefore, adding that tag is in fact a bad solution because it still creates your DAO instances in the wrong application context: better create a more specific base-packageconfiguration for your web layer component creation.

I had this same problem because I thought a <context:include-filter> in my spring-servlet.xml was taking care of only scanning @Controller classes... but no :-(


Just a guess, but you don't need to register your own PersistenceAnnotationBeanPostProcessor, since <context:component-scan> registers one automatically. It's possible that the two are interfering with one another.

Like I said, though, just a hunch.


The @Transactional annotation may be placed before an interface definition, a method on an interface, a class definition, or a public method on a class. However, please note that the mere presence of the @Transactional annotation is not enough to actually turn on the transactional behavior - the @Transactional annotation is simply metadata that can be consumed by something that is @Transactional-aware and that can use the metadata to configure the appropriate beans with transactional behavior. In the case of the above example, it is the presence of the <tx:annotation-driven/> element that switches on the transactional behavior.

from spring doc http://static.springsource.org/spring/docs/2.0.8/reference/transaction.html