Spring Data Transaction Control

The question is about using multiple transaction in crudrepository, jparepository ext. In my project, there are two entities. RequestEntity and SendingMailEntity. WorkFlow in my method:

1) save RequestEntity ,

2) send informationService(it is an rest service that purchased by us. we can't control its any exception.)

3) save SendingMailEntity.

When we have an exception on number 2 or 3, we lost requestEntity because of rollback that is controlled by spring jpa.

The records of requestEntity are never to be lost.

@Transactional
public RequestEntity create(RequestEntity entity) {

    entity=requestRepository.save(entity);
    sendMail(entity);
}

@Transactional(propagation=Propagation.REQUIRES_NEW)
public SendingMailEntity sendMail(RequestEntity entity) {

         /*
          *
          */
   informationService(entity.*,*,*);
         /*
          *
          */

     sendingMailRepository.save(sendingMailEntity);
}

This code block is not working. When sendMail has an error, RequestEntity is not saving.

1 answer

  • answered 2018-11-08 05:33 manoj

    Handle all exceptions inside sendMail, don't let it throw back to the calling function.

    also you can try moving sendMail into a new public class with override if the exception handling alone doesn't work