@Transactional方法上的Spring SDN 4.1和@Async

时间:2016-06-09 12:38:24

标签: spring neo4j spring-data-neo4j spring-data-neo4j-4 neo4j-ogm

我在A类中有一个方法a,它调用B类中的方法b

@Service
public class A {
    private B classB;
    ...

    @Async
    @Transactional
    public void a() {
        classB.b();
    }
}

@Service
public class B {
    ...

    @Transactional
    public void b() {
        // load some nodes from neo4j db by GraphRepository
    }
}

这是我的neo4j配置

@Override
@Bean
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)    
public Session getSession() throws Exception {
    return super.getSession();
}

根据Async注释,我将范围值设置为原型,但它仍然不起作用。

  

org.neo4j.ogm.exception.TransactionManagerException:此线程的事务不是最新的           在org.neo4j.ogm.session.transaction.DefaultTransactionManager.commit(DefaultTransactionManager.java:100)〜[neo4j-ogm-core-2.0.1.jar:?]           在org.neo4j.ogm.transaction.AbstractTransaction.commit(AbstractTransaction.java:83)〜[neo4j-ogm-api-2.0.2.jar:?]           在org.neo4j.ogm.drivers.bolt.transaction.BoltTransaction.commit(BoltTransaction.java:80)〜[neo4j-ogm-bolt-driver-2.0.2.jar:?]           在org.springframework.data.neo4j.transaction.Neo4jTransactionManager.commit(Neo4jTransactionManager.java:50)〜[spring-data-neo4j-4.1.1.RELEASE.jar:?]           在org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485)〜[spring-tx-4.2.5.RELEASE.jar:4.2.5.RELEASE]           at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)[spring-tx-4.2.5.RELEASE.jar:4.2.5.RELEASE]           at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)〜[spring-tx-4.2.5.RELEASE.jar:4.2.5.RELEASE]           在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]           在org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)〜[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]

我使用与SDN 4.0.0的另一个项目相同的技术,这是有效的。

可能是SDN 4.1.1和Ogm 2.0.3中的错误吗?

1 个答案:

答案 0 :(得分:1)

首先,您是否已使用@EnableAsync课程启用@Configuration

其次,除非您使用Spring Web-MVC或Spring感知Web应用程序,否则您无需更改Session的范围行为,因此您只需删除该行。

最后,由于您将@Async@Transactional一起使用,因此您需要确保EnableTransactionManagement(proxyTargetClass=true)课程@Configuration。{/ p>

如果您发现仍然无法解决问题,请尝试升级(完成以上所有操作后)至SDN 4.2.0-SNAPSHOT-BUILD

@Async in Spring