【问题标题】:Spring Data Neo4j - Unit Test - Transaction rollbacked but data not deletedSpring Data Neo4j - 单元测试 - 事务回滚但数据未删除
【发布时间】:2014-12-20 22:39:10
【问题描述】:

我正在使用 spring-boot (1.1.8.RELEASE)、spring-data-neo4j (3.2.0.RELEASE) 构建应用程序,以便通过 rest api 连接到独立的 neo4j 服务器。我正在使用 spring-test 来测试应用程序我已经实现了一个单元测试来创建一个节点并检索它。 运行良好,但测试完成后新节点仍在数据库中,但我希望事务回滚并删除节点

但是在控制台中我可以看到以下语句。

"Rolled back transaction after test execution for test context...

** 我不明白为什么基于控制台似乎发生了回滚,但事务已提交到数据库。 **

如果有人能帮助我找出问题的根源,我将不胜感激。

在我的 spring 配置下查找

@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableAutoConfiguration
public class AppConfig extends Neo4jConfiguration {

public AppConfig() {
    setBasePackage("demo");
}

@Bean
public GraphDatabaseService graphDatabaseService(Environment environment) {
    return new SpringRestGraphDatabase("http://localhost:7474/db/data");
}

}

在我的测试类下面找到

@SuppressWarnings("deprecation")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AppConfig.class)
@Transactional
public class AppTests {

@Autowired
private Neo4jTemplate template;

@Test
public void templateTest() {

    Person person = new Person();
    person.setName("Benoit");
    person.setBorn(1986);

    Person newPerson = template.save(person);

    Person retrievedPerson = template.findOne(newPerson.getNodeId(),Person.class);

    Assert.assertEquals("Benoit", retrievedPerson.getName());
}

}

我尝试在我的单元测试类中添加以下注释,但它没有改变任何东西:

@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)

我还尝试根据我在其他帖子中看到的内容在我的单元测试中添加以下内容

implements ApplicationContextAware

感谢您的帮助

问候

【问题讨论】:

  • 仅供参考,我用嵌入式数据库尝试了相同的场景,它运行良好。我想知道独立 neo4j 服务器的 spring-data-neo4j 事务支持是否存在问题。基于以下帖子 [link] stackoverflow.com/questions/26381842/… 框架的下一个版本(3.3.0)将利用新的 neo4j 休息事务端点,我希望它会有所作为。如果我在正确的轨道上,请告诉我?

标签: rollback spring-data-neo4j spring-transactions spring-test


【解决方案1】:

您遇到的行为是意料之中的:在这方面,Spring TestContext Framework (TCF) 中的事务支持没有任何问题。

TCF 通过配置的transactionManager 管理事务。

因此,当您切换到嵌入式数据库并使用该嵌入式数据库的数据源配置事务管理器时,效果非常好。问题是 Neo4J-REST 中的事务支持与 Spring 的事务管理设施不相关。正如 Michael Hunger 在您引用的另一个线程中所说,即将发布的 Neo4J-REST API 版本应该可以解决这个问题。

请注意,使用 @TransactionConfiguration 注释您的测试类的效果为零,因为您只是用默认值覆盖默认值,而没有任何效果。此外,在测试类中实现ApplicationContextAware 对事务管理没有影响。

问候,

Sam (spring-test 组件负责人)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    相关资源
    最近更新 更多