如何在 Spring Boot 中强制事务提交(使用 Spring Data)运行方法时 and not方法之后?
我读过这里应该可以@Transactional(propagation = Propagation.REQUIRES_NEW)
在另一个班级,但对我不起作用。
有什么提示吗?我正在使用 Spring Boot v1.5.2.RELEASE。
@RunWith(SpringRunner.class)
@SpringBootTest
public class CommitTest {
@Autowired
TestRepo repo;
@Transactional
@Commit
@Test
public void testCommit() {
repo.createPerson();
System.out.println("I want a commit here!");
// ...
System.out.println("Something after the commit...");
}
}
@Repository
public class TestRepo {
@Autowired
private PersonRepository personRepo;
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void createPerson() {
personRepo.save(new Person("test"));
}
}
使用辅助类org.springframework.test.context.transaction.TestTransaction
(自 Spring 4.1 起)。
默认情况下测试会回滚。真正承诺一个人需要做的事情
// do something before the commit
TestTransaction.flagForCommit(); // need this, otherwise the next line does a rollback
TestTransaction.end();
TestTransaction.start();
// do something in new transaction
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)