Answer the question
In order to leave comments, you need to log in
Spring transactions and spring data rest. Why are transactions not working?
Good day.
I am writing with this problem. There is a web project with spring. Frontend in angular, rests are displayed using spring-data-rest. Everything is convenient, good, great, but!
It's time to deal with distributed transactions. I will describe the case. There are objects with references to themselves. Example
public class SystemObject{
SystemObject parent;
List<AttributeInfo> attributes;
}
@Override
@Transactional(value = "transactionManager", readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor= Exception.class)
void delete(String s);
@Override
@Transactional(value = "transactionManager", readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor= Exception.class)
void delete(SystemObject paramT);
@Override
@RestResource(exported = false)
@Transactional(value = "transactionManager", readOnly = false, propagation = Propagation.MANDATORY, rollbackFor = Exception.class)
void delete(Iterable<? extends SystemObject> iterable);
@Modifying
@Transactional(value = "transactionManager", readOnly = false, propagation = Propagation.MANDATORY, rollbackFor = Exception.class)
@Query(value = "UPDATE SystemObject i SET i.parent = null WHERE i.parent = :parent AND i.objectType.isTableType = false")
@RestResource(exported = false)
int releaseChildren(@Param("parent") SystemObject parent);
@Override
@Transactional(value = "transactionManager", readOnly = false, propagation = Propagation.MANDATORY, rollbackFor = Exception.class)
protected void onBeforeDelete(SystemObject entity) {
//Зануляет объекты которые могут жить без родителя
objectRepository.releaseChildren(entity);
//Смотрю есть ли смертники и удаляю их
List<SystemObject> relatedObjects = objectRepository.getRelatedObjects(entity.getId());
if(relatedObjects != null){
LOG.info("Deleting related objects for entity {}",entity.getId());
objectRepository.delete(relatedObjects);
LOG.info("Deleting successful");
} else {
LOG.info("No related objects to delete. Entity {}",entity.getId() );
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question