Spring junit test with two sleeping transactions

I have a spring junit test consisting of two sequential transactions that are propagated as 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;
}
}

      

The test hangs when committing a transaction with an sql statement insert, which:

 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;
        }
    });
}

      

Why is this happening (the debugger stops at some kind of oracle codec with no source provided) and how to write a spring junit test with two sequential transactions correctly?

+3


source to share


1 answer


It looks like the test is creating a deadlock on the Contract table in your database. The root cause for this is most likely the use of a propagation layer REQUIRES_NEW

as described in this question . The important part is this:

PROPAGATION_REQUIRES_NEW starts a new independent "internal" transaction for this area. This transaction will be completely or completely rejected from the external transaction, having its own isolation scope, its own set of locks, etc. The outer transaction will be suspended at the beginning of the inner one and resume as soon as the inner one is completed



The method is createContractTransactional

trying to insert into the Contract table, but something earlier in the test must contain a lock, I assume this is a call persistenceManagerHibernate.isCreated(Contract.class, contract.getId())

. Whatever the reason, you have two independent transactions that are locked on the same table, i.e. a dead end.

Try setting the transaction propagation level in your test to REQUIRED

, which is the default setting. This creates a new transaction if it doesn't already exist, otherwise the current transaction is used. This should make your test run in a single transaction, and you shouldn't be deadlocked. After that, you can read the spring documentation at your distribution levels to make sure that REQUIRED

is the right level for your needs.

0


source







All Articles