0

I have a service which has to perform the following steps in sequence.

1) insert database records
2) commit
3) call external service (This service need to see the inserts in step 1)
4) more inserts
5) commit

Currently the external service is not able to see the inserted rows. Please suggest how to make the commit happen before the external call. I am using Spring JPA/Hibernate.

Thanks

AG Thomas
  • 13
  • 3

1 Answers1

0

You need to ensure both operations run in their own transaction and that T1 is committed before T2 executes. You also need to be aware of this discussion:

Spring @Transaction method call by the method within the same class, does not work?

Given the above something like this should work:

@Service
public class ClientService{

    @Autowired
    private RecordsService recordsService;

    @Autowired 
    private ExternalService externalService;

    public void insert(){
        recordsService.insertRecords();
        externalService.insertRecords();
    }
}

@Service
public class RecordsService{

    @Transactional
    public void insertRecords(){

    }
}

@Service
public class ExternalService{

    @Transactional
    public void insertRecords(){

    }
}
Community
  • 1
  • 1
Alan Hay
  • 22,665
  • 4
  • 56
  • 110