这与Unable to use two Neo4j Instances with Spring boot/Spring data neo4j中讨论的内容类似,但是我没有两个数据库。我已经从git repo下载了spring-data neo4j示例Java应用程序,想要执行动态查询而不是通过存储库接口执行静态查询。
我正面临一个空交易管理器的问题。
这是我的界面:
public interface SearchRepositoryCustom {
Iterable<Movie> searchByCriteria();
}
这是我的自定义回购提示:
@Repository
@Transactional
public class SearchRepositoryImpl implements SearchRepositoryCustom {
@Autowired
private SessionFactory sessionFactory;
@Override
public Iterable<Movie> searchByCriteria() {
String query = "MATCH (m:Movie)<-[r:ACTED_IN]-(a:Person) RETURN m,r,a LIMIT 10";
return sessionFactory.openSession().query(Movie.class, query, Collections.emptyMap());
}
}
这是我的配置:
@Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories(basePackages = "movies.spring.data.neo4j.repositories")
public class Neo4jPersistenceConfig {
@Bean
@ConfigurationProperties("spring.data.neo4j")
public Neo4jProperties neo4jProperties() {
return new Neo4jProperties();
}
@Bean
public org.neo4j.ogm.config.Configuration userConfiguration() {
return neo4jProperties().createConfiguration();
}
@Bean
public SessionFactory getSessionFactory() {
return new SessionFactory(userConfiguration(), "movies.spring.data.neo4j.domain");
}
@Bean
public Neo4jTransactionManager transactionManager() {
return new Neo4jTransactionManager(getSessionFactory());
}
}
由于我只有一个TransactionManager和一个SessionFactory(因为我只有一个Neo4j实例),所以不需要分别命名bean。
我看到以下异常:
org.neo4j.ogm.exception.core.TransactionManagerException: Transaction is not current for this thread
at org.neo4j.ogm.session.transaction.DefaultTransactionManager.rollback(DefaultTransactionManager.java:86) ~[neo4j-ogm-core-3.1.0.jar:3.1.0]
at org.neo4j.ogm.transaction.AbstractTransaction.rollback(AbstractTransaction.java:65) ~[neo4j-ogm-api-3.1.0.jar:3.1.0]
at org.neo4j.ogm.drivers.bolt.transaction.BoltTransaction.rollback(BoltTransaction.java:61) ~[neo4j-ogm-bolt-driver-3.1.0.jar:3.1.0]
at org.neo4j.ogm.transaction.AbstractTransaction.close(AbstractTransaction.java:144) ~[neo4j-ogm-api-3.1.0.jar:3.1.0]
at org.springframework.data.neo4j.transaction.Neo4jTransactionManager.doCleanupAfterCompletion(Neo4jTransactionManager.java:379) ~[spring-data-neo4j-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.cleanupAfterCompletion(AbstractPlatformTransactionManager.java:1007) ~[spring-tx-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:793) ~[spring-tx-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714) ~[spring-tx-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:532) ~[spring-tx-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:304) ~[spring-tx-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689) ~[spring-aop-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at movies.spring.data.neo4j.repositories.SearchRepositoryImpl$$EnhancerBySpringCGLIB$$d2631bcd.searchByCriteria(<generated>) ~[classes/:na]
at movies.spring.data.neo4j.controller.MovieController.advGlobal(MovieController.java:54) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
即使我实际上继续声明bean的名称并通过指定transactionManager的名称将方法标记为事务性方法,我仍然始终如一地得到相同的错误。
Java版本:1.8
我想念什么?
答案 0 :(得分:1)
您正在混用Neo4j-OGM的SessionFactory
/ Session
和Spring的@Transactional
支持(Data Neo4j)。后者将创建OGM代码不知道的新事务,并尝试创建新事务。
如果您使用Spring Data Neo4j,则还可以使用带有@Query
批注的方法在实体存储库中定义查询。
另一种解决方案是在服务层中删除@Transactional
批注,并在您打算执行多个操作时手动创建批注(不需要这样做,因为OGM将创建隐式事务(如果不存在的话))
答案 1 :(得分:1)
Gerrit是正确的。我想添加我们在这里拥有的两个选项。我们提供了绑定到当前线程并与Springs事务集成的可注入Session
。只需自动接线即可代替SessionFactory
,因此您可以采用自己的解决方案。请注意,我在所有Spring项目中都建议使用构造函数注入:
@Repository
@Transactional
class SearchRepositoryImpl implements SearchRepositoryCustom {
private final Session session;
public SearchRepositoryImpl(Session session) {
this.session = session;
}
@Override
public Iterable<ThingEntity> searchByCriteria() {
String query = "MATCH (t:ThingEntity) RETURN t LIMIT 10";
return session.query(ThingEntity.class, query, Map.of());
}
}
我已经使用另一个域来创建一个简洁的示例项目,但是想法保持不变。
对于像这样的简单用例,我完全同意Gerrit的看法,并将在声明性Spring Data Neo4j存储库中使用@Query
注释,如下所示:
interface ThingRepository extends Neo4jRepository<ThingEntity, Long> {
@Query("MATCH (t:ThingEntity) RETURN t LIMIT 10")
public Iterable<ThingEntity> searchByCriteria();
}
用法相同,如下所示:
@Component
class ExampleUsage implements CommandLineRunner {
private final ThingRepository thingRepository;
private final SearchRepositoryCustom searchRepositoryCustom;
public ExampleUsage(ThingRepository thingRepository, SearchRepositoryCustom searchRepositoryCustom) {
this.thingRepository = thingRepository;
this.searchRepositoryCustom = searchRepositoryCustom;
}
@Override
public void run(String... args) {
this.thingRepository.save(new ThingEntity(1));
this.thingRepository.save(new ThingEntity(2));
var things = this.searchRepositoryCustom.searchByCriteria();
things.forEach(System.out::println);
things = this.thingRepository.searchByCriteria();
things.forEach(System.out::println);
}
}
您将发现完整的应用程序要旨:Use Spring Data Neo4js injectable OGM Session。在接近Java 8的EOL时,我已经使用Java 10而不是8了,但这并没有改变存储库的实现。除此之外,还使用Spring Boot 2.0.4,Spring Data Kay和OGM 3.1.0进行了测试。
编辑:关于注释:可注入会话是代理。该字段本身是最终字段,但是代理根据需要打开会话,然后委托给它。