我有一个spring junit测试,由两个连续的事务组成,这些事务传播为REQUIRES_NEW:
public class ContractServiceTest extends AbstractIntegrationTest {
@Autowired
private PersistenceManagerHibernate persistenceManagerHibernate;
@Autowired
private ContractService contractService;
@Autowired
private EntityChangeService entityChangeService;
@Resource
private AddServiceService addService;
@Autowired
private ReferenceBookService refService;
@Autowired
private PropertyService propertyService;
@Autowired
private HibernateTransactionManager transactionManager;
@Test
public void testContractDeletes() {
Long contractId = 1L;
final Contract contract = createTestDetachedContract(contractId, PropertyServiceTest.createManaged(propertyService, refService), refService);
ensureContractCreated(contract);
deleteTransactional(contract);
Assert.assertEquals(1, entityChangeService.findByPaginationOrderByUpdateDate(Contract.class.getName(), contract.getId().toString(), null, 0, 30).size());
}
@Test
@Ignore
public void testContractCreates() {
Long contractId = 1L;
final Contract contract = createTestDetachedContract(contractId, PropertyServiceTest.createManaged(propertyService, refService), refService);
ensureContractDeleted(contract);
createContractTransactional(contract);
Assert.assertEquals(1, entityChangeService.findByPaginationOrderByUpdateDate(Contract.class.getName(), contract.getId().toString(), null, 0, 30).size());
}
private void ensureContractCreated(Contract contract) {
if (persistenceManagerHibernate.isCreated(Contract.class, contract.getId())) {
return;
}
createContractTransactional(contract);
}
private void deleteTransactional(final Contract contract) {
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.setPropagationBehavior(Propagation.REQUIRES_NEW.value());
transactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
try {
contractService.delete(contract);
} catch (Exception e) {
toString();
}
return null;
}
});
}
private void createContractTransactional(final Contract contract) {
TransactionTemplate transactionTemplate2 = new TransactionTemplate(transactionManager);
transactionTemplate2.setPropagationBehavior(Propagation.REQUIRES_NEW.value());
transactionTemplate2.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
contractService.create(contract);
return null;
}
});
}
private void ensureContractDeleted(final Contract contract) {
if (!persistenceManagerHibernate.isCreated(Contract.class, contract.getId())) {
return;
}
deleteTransactional(contract);
}
public static Contract createTestDetachedContract(Long contractId, Property property, ReferenceBookService refService) {
Contract contract1 = new Contract();
contract1.setId(contractId);
contract1.setName("test name");
contract1.setProperty(property);
contract1.setNumber("10");
contract1.setType(refService.get(ContractType.class, 1L));
contract1.setStatus(refService.get(ContractStatus.class, 1L));
contract1.setCreated(new Date());
contract1.setCurrencyRate(new BigDecimal(10));
contract1.setInitialSum(new BigDecimal(10));
contract1.setSum(new BigDecimal(10));
return contract1;
}
}
测试在使用insert sql语句提交事务时冻结,即:
private void createContractTransactional(final Contract contract) {
TransactionTemplate transactionTemplate2 = new TransactionTemplate(transactionManager);
transactionTemplate2.setPropagationBehavior(Propagation.REQUIRES_NEW.value());
transactionTemplate2.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
contractService.create(contract);
return null;
}
});
}
为什么会发生这种情况(调试器在没有提供源代码的某些oracle代码处停止)以及如何正确编写带有两个连续事务的spring junit测试?
答案 0 :(得分:0)
听起来测试是在数据库中的Contract表上创建死锁。根本原因很可能是使用REQUIRES_NEW
传播级别,详见this question。重要的是这个:
PROPAGATION_REQUIRES_NEW为给定范围启动新的独立“内部”事务。此事务将完全独立于外部事务提交或回滚,具有自己的隔离范围,自己的锁定等。外部事务将在内部事务开始时暂停,并在内部事务开始时恢复完成
createContractTransactional
方法试图插入到Contract表中,但测试中较早的东西必须对其进行锁定,我猜这是对persistenceManagerHibernate.isCreated(Contract.class, contract.getId())
的调用。无论原因是什么,您有两个锁定在同一个表上的独立事务,即死锁。
尝试将测试中的事务的传播级别设置为REQUIRED
,这是默认设置。如果没有新事务,则创建新事务,否则使用当前事务。这应该使您的测试在单个事务中执行,因此您不应该陷入僵局。一旦有效,您可能需要阅读spring documentation的传播级别,以确保REQUIRED
符合您的需求。