Given:
- I have a database with a table full of tasks.
- I have a program that processes these tasks. The database is abstracted using NHibernate.
- The program is run multiple times.
Now, I want to make sure that each task is processed by exactly one process.
One possibility is described here but I fail to see how I could transfer this to domain objects mapped to the database with NHibernate.
So, the question is:
How to implement a task processing system with a central data store and multiple concurrent processes that is not accessed directly but through NHibernate?
Possible Solution 1
One thing that we do is to create a centralized process that hands out work. We currently have a Wcf service that runs in
InstanceContextMode.Single… a singleton service basically.When the work has been assigned it will update it in the database to reflect that it has been assigned with the requestor’s id or just a value that says it’s assigned if you don’t care about who’s getting it.
Possible Solution 2
You could use optimistic concurrency in NHibernate on this table that stores the tasks. You would need a column to show that it has been assigned. You would only look for tasks in this table that haven’t been assigned. If you go to update the task in the database and someone has already grabbed this task and updated it, you will get a StaleObjectException on the update.
I’ve never used optimistic concurrency in NHibernate but it seems like that solution could work. I’m not so sure though that certain processes wouldn’t get starved for work and I think you would have to reinitialize your session when that object is thrown.
In my opinion option 1 or something similar to that model works the best.