【问题标题】:Spring SDN 4.1 and @Async on @Transactional methodSpring SDN 4.1 和 @Async on @Transactional 方法
【发布时间】:2016-10-10 02:57:32
【问题描述】:

我在类 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] 在 org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291) [spring-tx-4.2.5.RELEASE.jar:4.2.5.RELEASE] 在 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 中的错误?

【问题讨论】:

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


    【解决方案1】:

    首先,您是否在 @Configuration 类中启用了 @EnableAsync

    其次,除非您使用 Spring Web-MVC 或 Spring 感知 Web 应用程序,否则您不需要更改 Session 的作用域行为,因此您可以删除该行。

    最后,由于您将@Async@Transactional 一起使用,因此您需要确保您的@Configuration 类中的EnableTransactionManagement(proxyTargetClass=true) 也是如此。

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

    @Async in Spring

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 2023-03-10
      • 2015-02-23
      • 2020-08-21
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 2015-05-29
      • 2018-11-09
      相关资源
      最近更新 更多