• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

B&S Threads wating on a deleted Record

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic