有人可以解释为什么第一个单元测试类有效,而第二个测试类因锁定等待超时错误而失败?
第一个测试班:
public class Test1 extends AbstractTransactionalJUnit4SpringContextTests {
@Before
public void setUp() {
// do stuff through hibernate to populate database with test data
}
@Test
@Transactional(propagation = Propagation.NEVER)
public void testDeleteObject() {
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
// execute sql that deletes data populated in setUp() [i.e. this will require locks on the objects].
}
});
}
}
第二个测试类【获取锁等待超时错误】:
public class Test2 extends AbstractTransactionalJUnit4SpringContextTests {
@Before
public void setUp() {
// do stuff through hibernate to populate database with test data
}
@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testObject() {
// execute sql that deletes data populated in setUp() [i.e. this will require locks on the objects].
}
}
我知道第二个测试类失败了,因为这两个事务正在争夺相同的锁,但由于其 in_progress 事务状态,两者都不能放弃锁。我很困惑的是为什么第一个测试类成功执行了 sql。我可能理解错了,但是当 transactionTemplate 执行事务回调时,不会创建新事务吗?在那种情况下,不应该发生同样的事情(锁定等待超时)吗?