【发布时间】:2017-10-20 03:49:12
【问题描述】:
如何在 Spring Boot(使用 Spring Data)在运行方法时强制提交事务并且在方法之后不?
我在这里读到过,@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"));
}
}
【问题讨论】:
-
不让您的测试事务化,那么您的软件就会像在您的环境中那样运行。如果您的事务完全不起作用,那么您的配置可能有问题。
标签: java spring-boot transactions spring-data spring-data-jpa