我正试图在我的Neo4j应用程序中从手动管理的事务转移到基于注释的事务。
我已经准备好了基于注释的Spring配置文件:
@Configuration
@EnableNeo4jRepositories("xxx.yyy.neo4jplanetspersistence.repositories")
@ComponentScan(basePackages = "xxx.yyy")
@EnableTransactionManagement
public class SpringDataConfiguration extends Neo4jConfiguration
implements TransactionManagementConfigurer{
public SpringDataConfiguration() {
super();
setBasePackage(new String[] {"xxx.yyy.neo4jplanetspojos"});
}
@Bean
public GraphDBFactory graphDBFactory(){
GraphDBFactory graphDBFactory = new GraphDBFactory();
return graphDBFactory;
}
@Bean
public GraphDatabaseService graphDatabaseService() {
return graphDBFactory().getTestGraphDB(); //new GraphDatabaseFactory().newEmbeddedDatabase inside
}
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return neo4jTransactionManager(graphDatabaseService());
}
}
我用@Transactional标记了我的存储库:
@Transactional
public interface AstronomicalObjectRepo extends
GraphRepository<AstronomicalObject>{
}
我用@Transactional标记了我的单元测试类和测试方法,并注释了用于手动管理事务的旧代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringDataConfiguration.class},
loader = AnnotationConfigContextLoader.class)
@Transactional
public class AstronomicalObjectRepoTest {
@Autowired
private AstronomicalObjectRepo repo;
@Autowired
private Neo4jTemplate neo4jTemplate;
(...)
@Test @Transactional
public void testSaveAndGet() {
//try (Transaction tx =
//neo4jTemplate.getGraphDatabaseService().beginTx()) {
AstronomicalObject ceres = new AstronomicalObject("Ceres",
1.8986e27, 142984000, 9.925);
repo.save(ceres); //<- BANG! Exception here
(...)
//tx.success();
//}
}
更改后,测试无法通过。 我收到: org.springframework.dao.InvalidDataAccessApiUsageException:嵌套异常是org.neo4j.graphdb.NotInTransactionException
我尝试了很多不同的东西(在@Transactional注释中明确命名事务管理器,在@EnableTransactionManagment中更改模式......),没有任何帮助。
对于我做错了什么的线索非常感激。
提前致谢!
答案 0 :(得分:1)
我找到了原因...... SDN在交易方面不支持最新的Neo4j。 我相信这是因为neo4j-kernel中的SpringTransactionManager已经进入2.2+版本,但不是100%肯定。
在github上,我们可以看到7小时前进行了修改: https://github.com/spring-projects/spring-data-neo4j/blob/master/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/config/JtaTransactionManagerFactoryBean.java
对我有用的快速修复方法是使用Neo4jEmbeddedTransactionManager类从配置中的Neo4jConfiguration覆盖neo4jTransactionManager方法:
@Override
public PlatformTransactionManager neo4jTransactionManager(GraphDatabaseService graphDatabaseService) {
Neo4jEmbeddedTransactionManager newTxMgr = new Neo4jEmbeddedTransactionManager(graphDatabaseService());
UserTransaction userTransaction = new UserTransactionAdapter( newTxMgr );
return new JtaTransactionManager( userTransaction, newTxMgr );
}