title: Queue management in the database
body: <p>To begin with i am looking out for different ways to solve this problem. </p>
  
  <p>I have a queue within our application which is maintained in a database table. 
  There is a scheduled processor which will look at the queue and pull out records based on a STATUS field on the record. 
  It processes those records and on success deletes the record from the table. </p>
  
  <p>The problem is my application is clustered. So, there will be several instances of the scheduled processor which will pull out the same records and then process them... </p>
  
  <p>To solve this, the approach that i followed is 
  I update the status of the record before it is processed(say from PENDING to WORKING) and also added a version on the Entity mapping of the table, so the sequence of actions would be </p>
  
  <p>1) Query the table for PENDING records. 
  2) update the status as WORKING. (If another instance of the Processor tries to update it when someone has already updated the record it will give an exception, and hence will move on to the next record)
  3) On Success. Delete the record, else update it back to PENDING. </p>
  
  <p>Now, by doing this it would fairly solve the problem but dont quite like this idea... </p>
  
  <p>Wanted to find out how people who face a similar issue have resolved it. </p>
  
  <p>I had another way to solve this, since the same application populates the table, assign it with the host who populated it and the scheduled processor on that particular tomcat only lookup for the records of that host. Basically trying to minimize the thrashing that will go on with the first solution. </p>
  
  <p>it is a  Spring 3.0.5 and Hibernate application</p>