I have multiple instances of Windows services each running in different server. Each one of the windows service is started when it finds a record in a table.
Now the Expected functionality is that
If one of the windows service has picked the record, then the other windows services should not be picking up the same record.
The Actual Functionality:
Each of the windows service is supposed to pick a different record.
All the instances are picking up the same record and processing at the same time, which is creating a problem.
Can anyone suggest a solution to the above?
Additional Details:
I have added the code in which we are checking the dataset contains the record(which we inserted earlier for the windows service to process) then we perform some business, and send a mail. The problem is since there are multiple instances of windows services, I am getting multiple mails which is not desirable.
DataSet reportsDs = new DataSet();
int ReportID = 0;
int MastReptID = 0;
try
{
reportsDs = stored procedure to get the dataset in which the record is inserted
if (reportsDs != null && reportsDs.Tables[0].Rows.Count > 0)
{
ServiceName.isProcCompleted = false;
for (int rptcnt = 0; rptcnt < reportsDs.Tables[0].Rows.Count; rptcnt++)
{
//some business functions
//send a mail after finishing the above business process
}
}
}
Use a unique identifier column in the database.
Have each service generate it’s own guid on startup.
Fetch a row using modification of following SQL, assuming primary key is id and guid column is LOCKGUID
Now update the row with the following SQL, passing in the parameters @MYGUID and @ID
Now get the record
If you receive zero results you need to loop from step 1 again as another service claimed this record first.
You could write a stored procedure to do all this processing for you and thus remove the need for multiple round trips to the server.