SDN升级到4.2.0.BUILD-SNAPSHOT后如何获得当前的neo4j会话?

时间:2016-10-26 15:08:38

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

将Spring Data Neo4j更新为4.2.0.BUILD-SNAPSHOT后出现了一些困难。特别是,我需要获取当前Session实例来执行一些自定义查询。我一直在用

这样做
private Neo4jOperations getNeo4jOperations() {
    return applicationContext.getBean(Neo4jOperations.class);
}

Neo4jOperations现已弃用,所以我尝试了

private Session Neo4jSession() {
    return applicationContext.getBean(SessionFactory.class).openSession();
}

但有例外:

org.neo4j.ogm.exception.TransactionManagerException: Transaction is not current for this thread
    org.neo4j.ogm.session.transaction.DefaultTransactionManager.rollback(DefaultTransactionManager.java:93)
    org.neo4j.ogm.transaction.AbstractTransaction.rollback(AbstractTransaction.java:67)
    org.neo4j.ogm.drivers.bolt.transaction.BoltTransaction.rollback(BoltTransaction.java:64)
    org.neo4j.ogm.transaction.AbstractTransaction.close(AbstractTransaction.java:141)
    org.springframework.data.neo4j.transaction.Neo4jTransactionManager.doCleanupAfterCompletion(Neo4jTransactionManager.java:311)
    org.springframework.transaction.support.AbstractPlatformTransactionManager.cleanupAfterCompletion(AbstractPlatformTransactionManager.java:1016)
    org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:811)
    org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
    org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:487)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    com.sun.proxy.$Proxy110.delete(Unknown Source)

那么我怎样才能获得当前的会话实例?

1 个答案:

答案 0 :(得分:4)

你可以尝试一下吗? 请注意,此配置使用的是spring-data-neo4j 4.2.0.M1版本,但我认为它应该适用于4.2.0.BUILD-SNAPSHOT

@Bean
public SessionFactory getSessionFactory() {
    return new SessionFactory(getConfiguration(), "com.yourbasepackage") {
    };
}

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

还使用以下命令注释您的配置类:

@EnableTransactionManagement

然后你可以注入session bean并随意使用它:

@Inject
Session session;

您可以看到升级到SDN 4.2完整指南here

  

注意 :由于另一个问题,我最近创建了一个示例项目   github正确配置了此版本的   spring-data-neo4j。我认为它可以帮助你作为配置   样品

希望有所帮助