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

前端 未结 3 1368
感情败类
感情败类 2020-12-10 16:03

I\'m trying to make simple application using Spring, JPA and embedded H2 database. Recently I\'ve come across this strange issue with declarative transactions. They just doe

3条回答
  •  孤街浪徒
    2020-12-10 16:22

    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 in my spring-servlet.xml was taking care of only scanning @Controller classes... but no :-(

提交回复
热议问题