我有三个类,AccountController、AccountManager和AccountDao(它们按顺序注入,即controller<-manager<-dao)。当我在accountDao上添加@Transactional注释时,我有一个DB会话,一切都很好。当我把@Transactional放在管理器上时,它不起作用。我得到:
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
我看到accountManager由于某种原因(在调试时)没有使用代理进行包装。你知道为什么会这样吗?
在我的应用程序上下文的XML中,我有:
<context:component-scan base-package="com.mypackage" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<tx:annotation-driven transaction-manager="hibernateTransactionManager" proxy-target-class="true"/>
在AccountManager上,我添加了以下注释:
@Repository
@Transactional
public class AccountManager { ... }
更奇怪的是,AccountManager中注入了一个"DeviceManager",带有与AccountManager相同的注释,但DeviceManager确实被代理了!它完全是事务性的。
我使用的是org.springframework.transaction.annation.Transactional注释。
我在AccountManager 中没有任何最终方法
编辑以回应@Enigo的回答:
AccountManager
不是存储库,因此应使用@Service
对其进行注释,而AccountDao
应使用@Repository
进行注释AccountManager
应该实现一个接口,并且该接口应该被注入Controller类- 确保您在
AccountManager
中调用的方法是公共的,并重写接口中的方法声明