【发布时间】:2017-09-11 15:42:36
【问题描述】:
我不明白为什么 Spring @Transactional 测试方法 Hibernate 不回滚在 @Before 中所做的更改?有information@Before 和@Test 在一个事务中被调用。可以考虑配置所有上下文。
@Test
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional(defaultRollback = true, transactionManager = "transactionManager")
public class TestClass {
@Autowired
private SessionFactory sessionFactory;
@Autowired
private EntityDao entityDao;
@Before
public void before() {
// create arbitrary entity
Entity one = Utils.createEntity();
// save with HibernateDao to table_1
entityDao.save(one);
}
@Test
public void test() {
sessionFactory.getCurrentSession().createSQLQuery("TRUNCATE table_2").executeUpdate();
}
}
如果没有createSQLQuery().executeUpdate,@Before 中的所有更改都会根据需要回滚。
【问题讨论】:
-
您使用的是哪个 DBMS?并非所有人都支持事务性 TRUNCATE
-
我用的是mysql,据说
Truncate operations cause an implicit commit, and so cannot be rolled back。您能否通过手势解释为什么某些 dbms 不支持事务截断的问题?我想知道在这种情况下会发生什么。
标签: mysql spring hibernate junit transactions