I have a table of Orders that will be processed by different services. So assume a service starts processing a batch of orders and before it is completed another service comes and starts processing the same batch because the first batch is not done yet and the Orders table is not updated to reflect that.
How do we make sure this doesn't happen? So if one service is processing this batch then they should be "locked" for the other service.
What's the best way to do this?
Thanks.
EDIT: Basically I am looking for something like this
public void ProcessOrders(){
synchronized (this) {
if(IS_Process_RUNNING){
logger.error("ProcessOrders() is already running.");
return;
}
IS_Process_RUNNING = TRUE;
}
try{
DoSomeWork();
}
catch(Exception e){
LogFatalError.logFatalError("Error processing orders.....", e);
}
finally{
IS_Process_RUNNING = FALSE;
}
}
So if a process (first one) calls this method then is_process_running flag is set to false and it does what its supposed to do. And also sets the flag to true so a diff process when it comes here will exit immediately.