简体   繁体   English

Bean在Spring 4.2.5中没有在ContextRefreshedEvent上获得事务代理

[英]Bean gets no transactional proxy on ContextRefreshedEvent in Spring 4.2.5

I have a bean (SettingService) which is decorated with the @Transactional annotation and injected into another bean where this bean is invoked on the context refreshed event. 我有一个bean(SettingService),它使用@Transactional注释进行修饰,并注入另一个bean,在上面刷新的事件中调用此bean。

public class DefaultConfigManager
    implements ApplicationListener<ContextRefreshedEvent>, ConfigManager {

    @Autowired
    private SettingService service;

    @Override
    public void onApplicationEvent( ContextRefreshedEvent event ) {
        System.out.println( "Proxy: " + AopUtils.isJdkDynamicProxy( service ) );
        String key = service.getSystemSetting( "KEY" );
    }

Transactions work generally well and the method above works as expected in Spring 4.1.9, where the println indicates that the SettingService bean is a dynamic JDK proxy (for transaction handling). 事务通常工作良好,上面的方法在Spring 4.1.9中按预期工作,其中println指示SettingService bean是动态JDK代理(用于事务处理)。

Now after upgrading to Spring 4.2.5 this suddenly starts to throw the following error: 升级到Spring 4.2.5之后,这突然开始抛出以下错误:

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread org.hibernate.HibernateException:无法获取当前线程的事务同步会话

at org.springframework.orm.hibernate4.SpringSessionContext. 在org.springframework.orm.hibernate4.SpringSessionContext。 currentSession(SpringSessionContext.java:134) currentSession(SpringSessionContext.java:134)

at org.hibernate.internal.SessionFactoryImpl. 在org.hibernate.internal.SessionFactoryImpl。 getCurrentSession(SessionFactoryImpl.java:993) 的getCurrentSession(SessionFactoryImpl.java:993)

and the println indicates that the SettingService is no longer a proxy / has been decorated, meaning that no transactions will be initiated. 并且println表示SettingService不再是代理/已经过装饰,这意味着不会启动任何事务。

According to the Spring documentation, at the time of ContextRefreshedEvent being published, all beans and post-processors should be finished. 根据Spring文档,在发布ContextRefreshedEvent时,应该完成所有bean和后处理器。

A Hibernate transaction manager is configured in the app context, the tx:annotation-driven element is in place, the @Transactional annotation is put on the implementation (not the interface), there are no circular dependencies in the system. 在应用程序上下文中配置了Hibernate事务管理器,tx:annotation-driven元素就位,@Transactional注释放在实现(而不是接口)上,系统中没有循环依赖。 Hibernate version: 4.2.20.Final. Hibernate版本:4.2.20.Final。

Does this ring a bell with anyone? 这对任何人都响了吗? Are there any typical reasons for why beans no longer should be proxied with transactions on ContextRefreshedEvent in Spring 4.2? 是否有任何典型的原因导致为什么bean不再应该在Spring 4.2中的ContextRefreshedEvent上用事务代理? Any common mistakes or changes between Spring 4.1 and 4.2 to be aware of? 春季4.1和4.2之间的任何常见错误或变化都要注意?

I got same problem as you have. 我遇到了同样的问题。 My workaround is, put @Transactional annotation on class level rather than method level. 我的解决方法是,在类级别而不是方法级别上放置@Transactional注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM