admin管理员组

文章数量:1194371

We save information in a database using JPA repository functions. In our case we use saveAndFlush to write our Entity into the database. After that we are performing an HTTP request to another service. The response arrives asynchronously which means the service we called will call an endpoint of our service to return the response. After the response arrived we load our entity from the database to update its informations with data from the response. This works.

Hoever, as soon as we have more than one instance of our service running at the same time and have a proper load balancing it won't work any more. If the same instance which performed the HTTP request gets the response it is still working. But if instance A saves the data into the database and performs the HTTP request and instance B gets the callbabck and tries to load the data from the datababse, very often, the data isn't there. We have some kind of race condition. If the callback would arrive like 300 milliseconds later the data would be there.

We have tried any @Transactional combination regarding isolation and propagation existing without making the result any better or any worse.

How can we make sure that the data is actually written into the database before we perform our HTTP request?

本文标签: springHow to actually force a write into the databaseStack Overflow