【发布时间】:2015-06-19 01:39:38
【问题描述】:
我试图了解为什么我在测试中抛出异常时无法回滚事务?
我正在使用 Spring 4.1.5,并且正在尝试测试我的交易。 我已经注释了@Transactional 我的存储库和事务已经 如果存储库抛出异常,则回滚。我也注释了 @Transactional 我的测试方法并从 存储库在一个事务中工作。 但是,当我自己在测试中抛出异常时,事务不会回滚。为什么? 看起来是出于某种目的还是我做错了什么?
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@ContextHierarchy({
@ContextConfiguration(locations = {
"classpath:/META-INF/spring/jpa-persistence-context.xml"})
})
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = RuntimeException.class)
public class FeaturedGroupRepositoryTest2 {
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = RuntimeException.class)
@Test
public void testFeaturedGroupDao() {
FeaturedGroupEntity newFeaturedGroupEntity = new FeaturedGroupEntity();
FeaturedGroupEntity savedFeaturedGroupEntity = featuredGroupRepository.save(newFeaturedGroupEntity);
FeaturedGroupEntity foundFeaturedGroupEntity = featuredGroupRepository.findOne(savedFeaturedGroupEntity.getId());
throw new RuntimeException("test rollback");
}
}
【问题讨论】: