• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

B&S Threads wating on a deleted Record

 
Jim Petersson
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I would like to get som input on how to handle the following scenario:

- Thread 1 locks record 5
- Thread 2 tries to lock record 5 (goes into waiting state)
- Thread 3 tries to lock record 5 (goes into waiting state)
- Thread 1 deletes record 5

Even if the delete-method would unlock and notify on record 5, only one of Thread 2/3 would wake up, and the other one would be waiting for ever.

How have you handled this?

Thanks, Jim
 
Joshua Fix
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tested this portion of my code yet, but using traditional wait and notify methods, this is how I plan to do it:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#wait(long)
 
Joe O'Keefe
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way you can solve this is to have a List/ArrayList that holds references to all of the records that have been deleted. Then before you start to wait on the record (when locking it), do a check to see if it's in this list. Also, once you do get the lock for that record (and finish waiting), check again to see if it's deleted. In both cases you can throw a RecordNotFoundException if it's deleted.
That way if one thread deletes a record, and another thread is still waiting to get the lock on it, the other thread will see it's deleted and just throw an exception. This solution would go together with reusing deleted records though.
 
Zeng Wei Chu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, the way i implemented my solution for this problem is that i always do a check after locking on a particular record. if the record happens to be deleted, i unlock the record and throws a RecordNotFoundException.
 
Jim Petersson
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I decided to implement it is to have the delete method unlock the record after it has been deleted (and call notifyAll on it). And my lock method checks so that the record still exists after it has received a lock for it. (If it doesn't exist, I throw a RecordNotFoundException)

Thanks for you input
 
Soroj Margun
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With my B&S exam, I use the same concept as post in replies above. When user delete record, data manager also set flag status on the lock and notify all waiting thread. With this concept, I wont have any deleted list to maintain. But on the others hand, if you maintain the deleted record, it may be easier to create a new record because you will know which record is ready to reuse
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic