我有一个问题,理解@Transactional和@TransactionConfiguration(defaultRollback = true)注释。
我有一个将userAccount插入数据库的测试代码,然后应该插入另一个具有相同名称的帐户,这会导致DataIntegrityViolationException,因为AccountName被标记为Unique。如果在TestClass级别没有分析@Transactional和@TransactionConfiguration(defaultRollback = true),这样可以正常工作。但是如果启用了Rollback,我就不会得到Exception,因为即使在相同的方法中,数据也不会插入到数据库中。如果我在插入第一个帐户后设置了断点,则数据库仍为空。
这是我的代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/applicationContext.xml"})
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class DaoTests {
@Autowired
private Repository repo;
@Autowired
private AccountService userService;
@Before
public void orgInstAccount() {
Organization o = new Organization();
o.setName("Organisation 1");
repo.saveEntity(o);
Institution i1 = new Institution();
i1.setName("xyz");
i1.setOwningOrganization(o);
repo.saveEntity(i1);
}
@Test(expected = DataIntegrityViolationException.class)
public void saveUserFail() {
Account user = new Account();
user.setAccountname("chz");
user.setPassword(userService.calcMD5Hash("123"));
user.setOwningInstitution(repo.getInstitutionByName("xyz"));
repo.saveEntity(user);
Assert.assertNotNull(repo.getAccountByName("chz"));
Account userNew = new Account();
userNew.setAccountname("chz");
userNew.setPassword(userService.calcMD5Hash("123"));
userNew.setOwningInstitution(repo.getInstitutionByName("xyz"));
repo.saveEntity(userNew);
//Here the Exception should be thrown but everything works fine.
}
}
存储库实现是:
@Repository
@Transactional
@SuppressWarnings("unchecked")
public class RepositoryHibernateImpl implements Repository {
@Autowired
private SessionFactory factory;
@Override
public void saveEntity(Entity hce) {
factory.getCurrentSession().save(hce);
}
}
问题可能是因为Repository和TestClass标有@Transactional?
先谢谢你。
答案 0 :(得分:4)
调用flush()
,它会尝试立即调用sql而不是将其推迟到事务边界
factory.getCurrentSession().flush()