I have the following web service written in Java using Spring, I have wrapped it with the @Transactional annotation to ensure we can roll back if required.
It works fine, except for the scenario where the service is called twice, with the second call happening before the first call finishes.
In that scenario, because the first transaction is still running and hasn't yet committed to the DB, the second transaction will go through the full method, inserting a duplicate row, updating the status again, and calling sendAlert().
Here's the pseudo code.
@Transactional
public ServiceResponse update(ServiceRequest serviceRequest) {
....
if (myDao.getStatus() == "COMPLETE") {
return serviceError;
}
myDao.insertRow();
myDao.updateStatus("COMPLETE");
sendAlert();
}
How can I prevent the second transaction from going through before the first? Setting the isolation level as read uncommitted is not an option as the database doesn't support it.

Both the calls will open different threads and hence have different transactions. Unless you have something at some place else that's independent of the database that can tell you a thread is using this resource (like a file with a flag), the only other way is to synchronize your block of code in my opinion.