Question

I am trying to create a transactional method which calls several other transactional methods in order to save some interdependent db entities. I want the transaction to rollback completely if any call fails. However, this is not the observed behavior. Here's my code:

@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public void save(EntityToBeSaved entity) {
    try{
            for(SubEntity sub: entity.getSubEntities()) //specifics omitted
                  saveSubEntity(sub); //this is transactional
    }
    catch (DataIntegrityViolationException e){
        throw new BusinessException("Duplicate Name");
    }
}

saveSubEntity also has Propagation.REQUIRED and rollobackFor = Throwable.class , yet when the transaction fails at the 2nd saveSubEntity call , the first subEntity is commited.

Était-ce utile?

La solution 2

Apparently, the issue was generated due to faulty Spring injection, as the service was not instantiated on start-up, causing the annotations to not work properly. Thank you for the help.

Autres conseils

Maybe the problem is Hibernate can't open a transaction for the save method.

Assume that if Hibernate doesn't create any transaction for the save method, then every saveSubEntity invocation will be in a different transaction due to its Propagation.REQUIRED. As a result, changes of each saveSubEntity call will be committed to the database.

To check whether a transaction is created for save method or not, could you help me to remove the Tranactional annotation on the saveSubEnitty method. You will see an error will occur if no transaction is created for save method.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top