Search code examples
javaspringhibernateannotationstransactional

Hibernate transactional method call within transactional method rollback


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.


Solution

  • 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.