【发布时间】:2016-01-22 17:39:01
【问题描述】:
我的数据库有触发器。它们在创建实体后触发。我想测试一个条件,这取决于触发器执行的结果。所以,我需要能够在我的测试中看到这些结果。但是触发器会在提交后触发,此时事务实际上会回滚。
看来,我需要使用transactionTemplate 和PROPAGATION_REQUIRES_NEW 创建一个新事务。我尝试过这种方式,并且能够看到触发器执行的结果(太棒了!)。但是这种方式又产生了一个问题:看代码,我试图通过 cmets 来解释一个奇怪的行为。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfiguration.class})
@Transactional
public class ServiceTest {
@Inject
private PlatformTransactionManager txManager;
<...>
@Before
public final void setUp() {
clearDatabase();
}
@Test
public final void testFilter() {
// Note: here database is empty - was cleared by setUp() method
TransactionTemplate tt = new TransactionTemplate(txManager);
tt.setPropagationBehavior(PROPAGATION_REQUIRES_NEW);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus s) {
createAndSaveEntity();
}
});
// Here I can see created entity and results of the trigger execution
// But also I see all entities, which was in the database before setUp()
// That is very strange! Why they appear if I deleted them?
<...>
}
}
PS:我使用 Spring Data Neo4j 4.0.0.RELEASE 和 Neo4j 2.3.1
【问题讨论】:
标签: spring neo4j spring-data spring-data-neo4j spring-data-neo4j-4